Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aws_ecs: Issues with FargateTaskDestination #29748

Open
tomvisions opened this issue Apr 6, 2024 · 3 comments
Open

aws_ecs: Issues with FargateTaskDestination #29748

tomvisions opened this issue Apr 6, 2024 · 3 comments
Labels
@aws-cdk/aws-ecs Related to Amazon Elastic Container effort/medium Medium work item – several days of effort feature-request A feature should be added or improved. p2

Comments

@tomvisions
Copy link

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

@tomvisions tomvisions added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Apr 6, 2024
@github-actions github-actions bot added the @aws-cdk/aws-ecs Related to Amazon Elastic Container label Apr 6, 2024
@pahud
Copy link
Contributor

pahud commented Apr 8, 2024

Yes, at this moment it only supports FargateTaskDefinition

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

const container = this.taskDefinition.addContainer(containerName, {

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

@pahud pahud added response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. p2 feature-request A feature should be added or improved. effort/medium Medium work item – several days of effort and removed bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Apr 8, 2024
Copy link

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.

@github-actions github-actions bot added the closing-soon This issue will automatically close in 4 days unless further comments are made. label Apr 10, 2024
@tomvisions
Copy link
Author

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

@github-actions github-actions bot removed closing-soon This issue will automatically close in 4 days unless further comments are made. response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. labels Apr 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-ecs Related to Amazon Elastic Container effort/medium Medium work item – several days of effort feature-request A feature should be added or improved. p2
Projects
None yet
Development

No branches or pull requests

2 participants