aws / aws/aws-cdk

aws_ecs: Issues with FargateTaskDestination

Open
#29,748 3 comments 0 reactions 0 assignees View on GitHub
@aws-cdk/aws-ecs effort/medium feature-request p2
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
2d 3h
Merged PRs (30d)
83

Description

### Describe the bug

When I created a Task Definition with the class: new ecs.FargateTaskDefinition, the object is of type cdk.aws_ecs.FargateTaskDefinition.

If I use the function ecs.FargateTaskDefinition.fromFargateTaskDefinitionAttributes to import a task definition already created, the object is of type IFargateTaskDefinition.

In some cases this might be a non issue, however, if I try to import the task definition into aws_ecs_patterns.ApplicationLoadBalancedFargateService, the taskDefinition key is looking for a value of type cdk.aws_ecs.FargateTaskDefinition, not type IFargateTaskDefinition. My IDE highlights this issue.

There might be a workaround for it, but I'm actively trying to keep this modular. In the service class I'm building (using ApplicationLoadBalancedFargateService), i want to import the already made task definition created in the task deifnition class.

### Expected Behavior

When calling ApplicationLoadBalancedFargateService and applying the taskDefinition value, I should be able to call fromFargateTaskDefinitionAttributes to import a task definition already created from soomewhere else.

### Current Behavior

It is described in "Describe the Bug", unless you want more details?

### Reproduction Steps

Created this class.

`import {Stack, StackProps, CfnOutput, aws_iam, Size} from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as cdk from "aws-cdk-lib";
import * as ecs from "aws-cdk-lib/aws-ecs";
import {EcsTaskDefinitionRoleStack} from "./ecs-task-definition-role-stack"
import {LogGroupStack} from "./log-group-stack";
import {EcrStack} from "./ecr-stack";

const stage = process.env.stage ?? "production"
const region = process.env.region ?? "ca-central-1"
const containerName = `api-${stage}`;

const taskDefinitionArn = cdk.Fn.importValue('taskDefinitionArn')

export class EcsTaskDefinitionStack extends Stack {
private _taskDefinition: cdk.aws_ecs.FargateTaskDefinition;

constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);
this.initialize();
}

static getECSTaskDefinition(construct: Construct) :cdk.aws_ecs.FargateTaskDefinition {
return ecs.FargateTaskDefinition.fromFargateTaskDefinitionAttributes(construct, 'task-definition', {

taskDefinitionArn: taskDefinitionArn})

}

generateECSTaskDefinition(id:string) {
this._taskDefinition = new ecs.FargateTaskDefinition(this, id, {
executionRole: EcsTaskDefinitionRoleStack.getECSTaskDefinitionRole(this),
});

this._taskDefinition.addContainer(`taskDefinition-addContainer`, {
image: ecs.ContainerImage.fromRegistry(`${EcrStack.getECRepository(this).repositoryUri}:${stage}`),
logging: ecs.LogDrivers.awsLogs({
streamPrefix: 'EventDemo',
mode: ecs.AwsLogDriverMode.NON_BLOCKING,
maxBufferSize: Size.mebibytes(25),
logGroup: LogGroupStack.getLogGroup(this),

}),
containerName: containerName,
portMappings: [{ containerPort: 8000, hostPort: 8000 }],
command: ["node", "index.js"],
healthCheck: { command :[ "CMD-SHELL", "curl -f http://localhost:8000/health || exit 1" ]}
});

}
generateOutputs() {
new CfnOutput(this, 'taskDefinitionArn', { value: this._taskDefinition.taskDefinitionArn, exportName: "taskDefinitionArn" })
}
initialize() {
this.generateECSTaskDefinition('task-definition');
this.generateOutputs();
}
}`

When calling getECSTaskDefinition in a different class, it highlights the issue

TS2740: Type IFargateTaskDefinition is missing the following properties from type FargateTaskDefinition:
family, containers, volumes, placementConstraints
, and 33 more.

### Possible Solution

When calling ApplicationLoadBalancedFargateService, have taskDefinition accept either FargateTaskDefinition or IFargateTaskDefinition.

### Additional Information/Context

_No response_

### CDK CLI Version

2.117.0

### Framework Version

_No response_

### Node.js Version

20.8.1

### OS

MacOS

### Language

TypeScript

### Language Version

5.3.3

### Other information

_No response_

Contributor guide

Open the contributing guide

Research direction

Start by tracing the TypeScript definitions for ApplicationLoadBalancedFargateService.taskDefinition and ecs.FargateTaskDefinition.fromFargateTaskDefinitionAttributes, then reproduce the reported TS2740 error with the example. Done means the intended imported task definition can be passed to the service without the type mismatch, with coverage for both imported and newly constructed task definitions.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, typescript
Domain
cloud, infrastructure
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.