aws-cdk icon indicating copy to clipboard operation
aws-cdk copied to clipboard

CDK Hotswap Feature : AWS::CloudFront::Distribution Attribute Support

Open bellomal opened this issue 1 year ago • 4 comments

Describe the bug

When doing a hotswap deployment of an ECS instance, the following error was thrown:

Could not perform a hotswap deployment, because the CloudFormation template could not be resolved: We don't support attributes of the 'AWS::CloudFront::Distribution' resource. This is a CDK limitation. Please report it at https://github.com/aws/aws-cdk/issues/new/choose

Looking at the cdk synth output (check current behavior), we can see that one of the ECS instance environment variables is a Distribution DomainName.

Opening this ticket as instructed by the error message. If this isn't the correct place for this ticket, let me know and I can reroute it to the appropriate location.

Expected Behavior

The hotswap deployment to succeed, since the Distribution DomainName has not changed.

Current Behavior

The hotswap deployment fails. As far as I can tel, this is because the DomainName attribute of the Distribution resource is not supported. See the below snippet of the cdk synth result for more context.

  ServiceClusterTaskDefF486EB2A:
    Type: AWS::ECS::TaskDefinition
    Properties:
      ContainerDefinitions:
        - Cpu: 4096
          Environment:
            - Name: STAGE
              Value: beta
            - Name: APPLICATION_NAME
              Value: Service
            - Name: ENDPOINT
              Value:
                Fn::GetAtt:
                  - ASVSelectionServiceCuratedChannelscuratedChannelDistribution0D56EA6F
                  - DomainName

Reproduction Steps

Run a hotswap deployment for an ECS instance that contains a Distribution attribute in it's definition.

Possible Solution

Similar to https://github.com/aws/aws-cdk/issues/21320, allow hotswap to tolerate the use of this attribute if it's value hasn't been changed.

Additional Information/Context

No response

CDK CLI Version

2.127.0

Framework Version

No response

Node.js Version

14.x

OS

Linux

Language

TypeScript

Language Version

No response

Other information

No response

bellomal avatar Feb 22 '24 18:02 bellomal

I guess your CDK application might contain the cloudfront distribution and you are referencing the distribution ID in the container env var. The hotswap would not be able to resolve the ENDPOINT locally from the DomainName attribute. But this is just my guess. Would you be able to provide a tiny minimal reproducible and self-contained code snippet that we can copy/paste to deploy in our environment?

pahud avatar Feb 23 '24 17:02 pahud

Thanks for taking a look! That's right - the CDK code creates a cloudfront distribution and exports the DomainName. Later, the DomainName is added to the environment variables for the ECS container.

I'll see if I can get together a code snippet - it might take a while to put something together since I can't publish the code directly.

bellomal avatar Feb 23 '24 20:02 bellomal

Created a public Github repo that you can use to reproduce the issue:

https://github.com/bellomal/HelloWorldCDKApp/tree/main

Steps to reproduce are in the README. I'll paste them here just in case:

  1. cdk deploy
  2. Update the container image registry on line 45 of hello_world_cdk_app-stack.ts to something else (eg amazon/amazon-ecs-sample or public.ecr.aws/ecs-sample-image/amazon-ecs-sample:latest)
  3. cdk deploy --hotswap

bellomal avatar Feb 23 '24 21:02 bellomal

Side question - is there some way I can least pull the value of distributionName out and pass it as a string instead of using Fn::GetAtt: for that environment variable in the CF template?

EDIT: Looked into this further, and as a hacky solution I tried adding the following line of code: const distributionEndpoint = new CfnOutput(this, "DistributionEndpoint", { value: bucketEndpoint.distributionDomainName }) and referenced distributionEndpoint.value in the environment variables instead. The hotswap still failed with the same error.

bellomal avatar Feb 23 '24 22:02 bellomal