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

Create ECS Service with existing task definition ARN #6240

Closed
iamhopaul123 opened this issue Feb 11, 2020 · 9 comments
Closed

Create ECS Service with existing task definition ARN #6240

iamhopaul123 opened this issue Feb 11, 2020 · 9 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. needs-reproduction This issue needs reproduction. p1

Comments

@iamhopaul123
Copy link
Contributor

Reproduction Steps

Now our customers cannot create an ECS service using an existing task definition ARN although we have fromFargateTaskDefinitionARN() and fromEc2TaskDefinitionARN().

Related to aws-samples/aws-cdk-examples#112

Error Log

Environment

  • CLI Version :
  • Framework Version:
  • OS :
  • Language :

Other


This is 🐛 Bug Report

@iamhopaul123 iamhopaul123 added bug This issue is a bug. p1 @aws-cdk/aws-ecs Related to Amazon Elastic Container labels Feb 11, 2020
@SoManyHs SoManyHs added needs-reproduction This issue needs reproduction. effort/medium Medium work item – several days of effort labels Apr 20, 2020
@SoManyHs SoManyHs assigned efekarakus and SoManyHs and unassigned piradeepk May 4, 2020
@machielg
Copy link

machielg commented May 7, 2020

Since CDK and Cloudformation are lagging behind in the ECS-EFS support, I created a custom resource which creates a TaskDefinition with the new EFS mounts in it. Trying to use this task definition results in above error. Perhaps there should be a separate property taskDefinitionArn to specify an external task definition, to avoid confusion with creating one on the spot.

@SoManyHs SoManyHs added feature-request A feature should be added or improved. bug This issue is a bug. and removed bug This issue is a bug. feature-request A feature should be added or improved. labels May 18, 2020
@wenisman
Copy link

this is a show stopper for anyone trying to use EFS with Fargate, if someone is able to provide us with a workaround that would be appreciated.

@cajames
Copy link

cajames commented May 29, 2020

For anyone still trying to create a service with a task definition ARN, here's a temporary workaround. It can be done with "Raw Overrides". Documented here: https://docs.aws.amazon.com/cdk/latest/guide/cfn_layer.html

You'll need to create a service with a temporary task definition (can be done easily with CDK), and then overwrite that Task Definition with a new Task Definition.

Our reasons for using this is similar to @machielg, to mount an EFS volume to a Fargate task, which is not currently supported by CloudFormation.

I found this technique in @michaelmoussa's example.

So in this case:

fargateService.addPropertyOverride('TaskDefinition', customResourceTaskDefinitionArn);

Here's a gist showing how to create a Fargate + EFS service currently, adapted from @michaelmoussa's examples. Hope this can help anyone, while the main issue is being fixed.

https://gist.github.com/cajames/3daec680b1101c8358e2ff30dfadd52a

@allisaurus
Copy link
Contributor

(potential fix update/request for comment)

So, I've been working on how to fix this by accepting an ITaskDefinition instead of the concrete TaskDefinition, similar to this previous fix. However, I'm having a problem providing a way to specify the expected networkMode, since the Ec2Service construct requires the taskDefinition.networkMode to correctly configure security groups.

Accepting an optional param in .fromEc2TaskDefinitionArn() results in an awslint error b/c fromXxx methods can't have more than 3 params.

Given that, AFAICT we've got the following options:

  • add an Ec2TaskDefinition.fromAttributes method to accept an ARN and a network mode; however this does not solve the problem of letting us use the results of .fromEc2TaskDefinitionArn() in an Ec2Service.
  • add optional expectedNetworkMode param to Ec2Service constructor; however this is really a task def field, so it's inclusion here is strange
  • don't set it for imported Ec2 task defs, these cannot be used to make Ec2Service constructs (just BaseServices)
  • add some additional helper method? e.g., .withNetworkMode()(not sure this could live on the interface)

Or: don't change the type of the taskDefinition, and we go another route (add'l field or method on service constructs to accomplish this instead).

(CC @MrArnoldPalmer , @SoManyHs , @pkandasamy91 who have discussed previously)

@IlyaEremin
Copy link

@cajames hey
I followed your gist, but I get Maximum call stack size exceeded. error after I run cdk synth.
Here is my code:

   const apiStagingTaskDefinition = FargateTaskDefinition.fromFargateTaskDefinitionArn(this, `${ENVIRONMENT_NAME}-api-task-definition`, "arn:aws:ecs:us-east-1:363852383723:task-definition/api-services:955");

        const workAroundTaskDefinition = new FargateTaskDefinition(this, `taskdef-${ENVIRONMENT_NAME}`);

        const container = workAroundTaskDefinition.addContainer("AppContainer", {
            image: ContainerImage.fromRegistry("amazon/amazon-ecs-sample"),
        })

        container.addPortMappings({
            containerPort: 3000,
            hostPort: 3000
        });

        const apiFargateService = new FargateService(this, "api-service", {
            cluster: cluster,
            taskDefinition: workAroundTaskDefinition,
            desiredCount: 2,
            serviceName: "api-service"
        });

        // console.log(apiFargateService.node.children);

        (apiFargateService.node.findNode("Service") as CfnService)?.addPropertyOverride("TaskDefinition", apiStagingTaskDefinition);

@keirw2022
Copy link

Any update on this bug. I can't see why cdk would force you to create a task definition; rather than import an existing one via the class methods available and use that.

@b3n3d1k7
Copy link

For anyone still trying to create a service with a task definition ARN, here's a temporary workaround. It can be done with "Raw Overrides". Documented here: https://docs.aws.amazon.com/cdk/latest/guide/cfn_layer.html

You'll need to create a service with a temporary task definition (can be done easily with CDK), and then overwrite that Task Definition with a new Task Definition.

Our reasons for using this is similar to @machielg, to mount an EFS volume to a Fargate task, which is not currently supported by CloudFormation.

I found this technique in @michaelmoussa's example.

So in this case:

fargateService.addPropertyOverride('TaskDefinition', customResourceTaskDefinitionArn);

Here's a gist showing how to create a Fargate + EFS service currently, adapted from @michaelmoussa's examples. Hope this can help anyone, while the main issue is being fixed.

https://gist.github.com/cajames/3daec680b1101c8358e2ff30dfadd52a

Because I just spent a few hours trying to figure out why using addPropertyOverride didn't work and resulted in strange error messages about invalid parameter types during deployment, maybe this is useful for someone else.
Set the task-definition using the member instead of using addPropertyOverride:

# instead of this
(service.service.node.tryFindChild('Service') as CfnService)?.addPropertyOverride(
  'TaskDefinition',
  customTaskDefinition.getResponseField('taskDefinition.taskDefinitionArn'),
);

# use this
(service.service.node.tryFindChild('Service') as CfnService).taskDefinition = customTaskDefinition.getResponseField('taskDefinition.taskDefinitionArn');

Note: Using addPropertyOverride resulted in both, the old and the new task-definition attached to the service in the cloudformation template for me.

@madeline-k
Copy link
Contributor

Closing as duplicate of #7863

@github-actions
Copy link

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

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. needs-reproduction This issue needs reproduction. p1
Projects
None yet
Development

No branches or pull requests