Aws-cdk: Create ECS Service with existing task definition ARN

Created on 11 Feb 2020  路  5Comments  路  Source: aws/aws-cdk

Reproduction Steps


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

Related to https://github.com/aws-samples/aws-cdk-examples/issues/112

Error Log

Environment

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

Other


This is :bug: Bug Report

@aws-cdaws-ecs bug efformedium needs-reproduction p1

Most helpful comment

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

All 5 comments

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.

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.

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

(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)

@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);
Was this page helpful?
0 / 5 - 0 ratings