aws / aws-cdk

The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code
https://aws.amazon.com/cdk
Apache License 2.0
11.71k stars 3.93k forks source link

aws_ecs: Issues with FargateTaskDestination #29748

Open tomvisions opened 7 months ago

tomvisions commented 7 months ago

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

pahud commented 7 months ago

Yes, at this moment it only supports FargateTaskDefinition

https://github.com/aws/aws-cdk/blob/bf2cf51bf1e8458097b23e7f580ad0978b7e4cca/packages/aws-cdk-lib/aws-ecs-patterns/lib/fargate/application-load-balanced-fargate-service.ts#L58

This is because it will addContainer which IFargateTaskDefinition does not have such method.

https://github.com/aws/aws-cdk/blob/bf2cf51bf1e8458097b23e7f580ad0978b7e4cca/packages/aws-cdk-lib/aws-ecs-patterns/lib/fargate/application-load-balanced-fargate-service.ts#L91

If you look at the TaskDefinition from CFN, the ContainerDefinitions is a prop of it, so when you import an existing TaskDefintion in CDK, CDK would not modify that TaskDefinition to render new ContainerDefinitions and that might be the reason. https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ecs-taskdefinition.html

github-actions[bot] commented 7 months ago

This issue has not received a response in a while. If you want to keep this issue open, please leave a comment below and auto-close will be canceled.

tomvisions commented 7 months ago

Thank you for the feedback. You placed this as a feature-request. Is there a place online to officially make that a feature request?