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.52k stars 3.86k forks source link

ECS: Error when using ECS service_name #27178

Closed andreprawira closed 11 months ago

andreprawira commented 1 year ago

Describe the bug

I'm using AWS CDK Python to create an ECS and i specified the cluster's service name. When i run the code in codepipeline first run works perfectly (as in fresh run, no cluster and no service ever created), it creates the cluster and the service using the correct name as it should. But when i re run the pipeline without any code change for the second time, i would get an error Resource handler returned message: "Resource of type 'AWS::ECS::Service' with identifier 'dev-my-ecs-service' already exists." (RequestToken: 106a2ca3-6e6e-6715-8f20-0bb93b5ee61e, HandlerErrorCode: AlreadyExists)

Expected Behavior

i expect it would just leave it as is without trying to re-create the ecs cluster because there was no code change on the second run

Current Behavior

im guessing CDK thinks that there is no cluster with that name and that it should create a new one but when its trying to do so, there is already a service created using that name which was created on the previous run, im guessing so because the error message says 'dev-my-ecs-service' already exists.

Reproduction Steps

load_balanced_fargate_service = ecs_patterns.ApplicationLoadBalancedFargateService(self, "Service",
            cluster=cluster,
            memory_limit_mi_b=1024,
            service_name=f"{props.prefix}-my-ecs-service",
            desired_count=1,
            cpu=512,
            task_image_options=ecsPatterns.ApplicationLoadBalancedTaskImageOptions(
                image=ecs.ContainerImage.from_registry("amazon/amazon-ecs-sample")
            ),
            task_subnets=ec2.SubnetSelection(
                subnets=[ec2.Subnet.from_subnet_id(self, "subnet", "VpcISOLATEDSubnet1Subnet80F07FA0")]
            ),
            load_balancer_name="application-lb-name"
        )

Possible Solution

No response

Additional Information/Context

No response

CDK CLI Version

2.51.0 (build a87259f)

Framework Version

No response

Node.js Version

16.18.0

OS

Windows

Language

Python

Language Version

No response

Other information

No response

peterwoodworth commented 1 year ago

i expect it would just leave it as is without trying to re-create the ecs cluster because there was no code change on the second run

This is how it would work if you're creating one service per account + region, and you never make any changes that cause resource replacement to your resource. You're either attempting to deploy multiple stacks/constructs with this name, or there are changes the second time you run it that cause resource replacement, which is a well documented behavior with specifying the physical name. You haven't posted a diff or details into your pipeline setup, so I cannot say what exactly is going wrong.

github-actions[bot] commented 12 months ago

This issue has not received a response in a while. If you want to keep this issue open, please leave a comment below and auto-close will be canceled.