question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using custom-named ECS / Fargate services: CloudFormation cannot update a stack when a custom-named resource requires replacing

See original GitHub issue

❓ General Issue

Hi! I think we need some advice how the AWS CDK is meant to be used when generating Fargate services. Lately, after CDK updates we have been seen a lot of following errors:

CloudFormation cannot update a stack when a custom-named resource requires replacing. Rename cluster-name\app-name and update the stack again.
--
  | Physical ID:arn:aws:ecs:eu-central-1:account-id:service/cluster-name/app-name

So far, we have manually named all of our services with simplified names. For example, the mentioned app-name.

If we use CDK generated names we see env-name-app-nameService70147683-W8VL0MQANBBH.

new ecs.FargateService(stack, `${prefix}-${service.name}`, {
      cluster,
      taskDefinition,
      // uncomment to fix the name
      // serviceName: service.name,
      desiredCount: service.desiredCount,
      enableECSManagedTags: true,
      propagateTags: ecs.PropagatedTagSource.SERVICE,
    })

The problem comes from our deployment pipeline which uses fixed names for services since the service name is the only proper identifier provided by AWS in this case:

# Repeat for each service and environment
      - Name: UpdateDev
        Actions:
        - Name: DeployImage
          InputArtifacts:
            - Name: BuildOutput
          ActionTypeId: {Category: Deploy, Owner: AWS, Version: 1, Provider: ECS}
          RoleArn: !Sub arn:aws:iam::${AccountNumber}:role/deploy-role
          Configuration:
            ClusterName: !Sub cluster-name
            ServiceName: !Ref ServiceName
            FileName: imagedefinitions.json

It seems difficult to me to find a workaround for this issue since the service names for CodePipeline deployment are not known (?) before the stacks have been initialized or deployed. We have one account for deployment tools and separate accounts for testing and production environments.

The Question

What would be the “official” way to proceed without fixed names and still be able to use the CDK generated service names in deployment pipeline which is deployed in different account before any other stacks are deployed?

Environment

  • CDK CLI Version: 1.10.0
  • Module Version: 1.10.0
  • OS: MacOS
  • Language: TypeScript

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:10 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
piradeepkcommented, Oct 4, 2019

@markusl, this is because CloudFormation attempts to bring up the new version of your ECS/Fargate service prior to deleting the old service (this only happens on replacement). If the new service creation fails, your old service will be used to rollback.

Since you’re using a custom named resource, it is unable to create the new service with the same resource names (due to collisions). Would it make sense to have a version/revision/unique identifier appended to the end of your resource name?

0reactions
markuslcommented, Nov 7, 2019

Let’s close the issue if there is nothing CDK team can do to help the situation.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Update a CloudFormation stack when a custom-named ...
This error typically occurs when a stack update tries to replace resources that have properties with custom names. AWS CloudFormation doesn't replace a...
Read more >
CloudFormation cannot update a stack when a custom-named ...
Problem. You've made some changes to a resource, and CloudFormation needs to replace the resource by removing and recreating it. However, CloudFormation ......
Read more >
Best practice guidance to avoid "CloudFormation cannot ...
CloudFormation cannot update a stack when a custom-named resource requires replacing. Rename <name> and update the stack again. How should we best overcome ......
Read more >
awslabs/aws-cdk - Gitter
Seems nice until I've got to adjust something, and I run into: CloudFormation cannot update a stack when a custom-named resource requires replacing....
Read more >
How to Update AWS::ECS::Service to Fargate launch type
After updating my cloudformation template, and attempting to update the stack, I am getting an error that the service already exists. Resource ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found