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.41k stars 3.8k forks source link

ecs: create Ec2Service or FargateService with imported TaskDefinition #7863

Open pahud opened 4 years ago

pahud commented 4 years ago

FargateService requires the TaskDefinition type and using existing task definition is not allowed.

https://github.com/aws/aws-cdk/blob/c304d24d313ef61d8cc6b110df9ba4f3f441d133/packages/%40aws-cdk/aws-ecs/lib/fargate/fargate-service.ts#L17

Use Case

Becasue cloudformation is missing some features such as TaskDefinition with EFS support, we may create our own task definition with CLI or console and allow CDK to create Fargate service by importing the existing task definition.

Proposed Solution

Maybe we can simply use ITaskDefinition instead?

The drawback is we will not be able to check the defaultContainer here.

https://github.com/aws/aws-cdk/blob/c304d24d313ef61d8cc6b110df9ba4f3f441d133/packages/%40aws-cdk/aws-ecs/lib/fargate/fargate-service.ts#L167-L169

Other


This is a :rocket: Feature Request

pahud commented 4 years ago

related to https://github.com/aws/aws-cdk/issues/6240

kennu commented 4 years ago

I also need this feature, to be able to define the TaskDefinition externally to support EFS volumes for Fargate.

mittalyashu commented 1 year ago

This will help me to use CDK to provision the infra and use Github Actions with amazon-ecs-deploy-task-definition for automated deployments.

madeline-k commented 1 year ago

Looks like the PR to fix this went stale. If anyone is interested to revive it, you are welcome to 😄

vcatalano commented 5 months ago

I am still running into this issue and it would be great if we we could resolve it without having to do much hacking. The following is the workaround that I'm current using, it's a modified hack from https://github.com/aws/aws-cdk/issues/25777.

const defaultTaskDefinition = new FargateTaskDefinition(this, 'DefaultTask', {
  family: 'DefaultTask',
  taskRole: taskRole,
  executionRole: taskExecutionRole,
});
defaultTaskDefinition.addContainer(`${serviceName}Container`, {
  containerName: 'DefaultContainer',
  image: ContainerImage.fromRegistry('registry.hub.docker.com/ealen/echo-server'),
});

const myService = new FargateService(this, 'MyService', {
  cluster: this.cluster,
  taskDefinition: defaultTaskDefinition,
  desiredCount: 1,
  securityGroups: [securityGroup],
  serviceName: 'MyService',
  assignPublicIp: false
});

// Lookup the FargateTaskDefinition from the ARN and override the service defnition
const existingTaskDefinition = FargateTaskDefinition.fromFargateTaskDefinitionArn(this, 'ExistingTaskDefinition', 'Existing Task ARN');

(myService.node.tryFindChild('Service') as CfnService).taskDefinition =
  existingTaskDefinition.taskDefinitionArn;

It seems that the bigger issue is that CDK does not allow us to make updates to the FargateService without reverting the task definition to the initial task definition defined by the FargateService construct.