< Home

First of all, yes this is a bug that is being tracked on Github. But until that is fixed you’ll need to use a workaround.

The workaround works as following

  1. Create a new temp TaskDefinition
  2. Create the service with this new temp TaskDefinition
  3. Edit the service with the help of CfnService
  4. Override the TaskDefinition property with the ARN of your existing TaskDefintion

Or like following code

const tempTaskDefinition = new Ec2TaskDefinition(this, 'TempTask');
taskDefinition.addContainer('TheContainer', {
    image: ContainerImage.fromRegistry('amazon/amazon-ecs-sample'),
    memoryLimitMiB: 256
});

That is the minimal needed for a task definition

const service = new Ec2Service(this, "ServiceId", {
    cluster,
    taskDefinition: tempTaskDefinition,
});

Ok, that is enough for our service, and now we’ll replace the property TaskDefinition with our own arn

(service.node.defaultChild as CfnService)?.addPropertyOverride("TaskDefinition", "arn:aws:ecs:{region}:{account-id}:task-definition/{taskdefinition-name}");

Now the LATEST task-definition will be selected (if you want a specific version you can always add :{versionid})

And that takes care of the error that you can’t import an exisiting TaskDefinition!

Is this a nice solution? No 😪 but until they get .fromEc2TaskDefinitionArn, fromTaskDefinitionArn, fromFargateTaskDefinitionArn working with a new service, this is the closest you will get.

< Home