Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aws_ecr_assets.DockerImageAsset: cannot deploy more than one stack simultaneously because of the image tag #30278

Closed
ukrainiansteak opened this issue May 20, 2024 · 5 comments
Labels
@aws-cdk/aws-ecr-assets Related to AWS CDK Docker Image Assets bug This issue is a bug. closed-for-staleness This issue was automatically closed because it hadn't received any attention in a while. effort/medium Medium work item – several days of effort p2 response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days.

Comments

@ukrainiansteak
Copy link

ukrainiansteak commented May 20, 2024

Describe the bug

In my aws-cdk stack deployed in eu-north-1 region, I define DockerImageAsset as follows:

        asset = DockerImageAsset(
            self,
            f"{config.stack_name}-docker-image",
            asset_name=f"{config.stack_name}-docker-image",
            directory=os.path.join("infrastructure", "dockerimage"),
        )

The asset is then referenced as an image for a CodeBuild project later in the code.

The problem arises when I deploy two or more stacks using the same template (obviously, the stack names are different) simultaneously. Image tag is most likely generated using the datetime info, which leads to the tags being the same for several images generated in the same timeframe (the same minute?).

Here's the error I receive when the stack fails:

4736 | stack-name-dev:  fail: 
docker push 528688307418.dkr.ecr.eu-north-1.amazonaws.com/cdk-hnb659fds-container-assets-account-id-eu-north-1:51687ba0f9469a37e6d0d900166b328d730f9c58751a61e8ecc7d25ec7dc3640 
exited with error code 1: tag invalid: The image tag '51687ba0f9469a37e6d0d900166b328d730f9c58751a61e8ecc7d25ec7dc3640' 
already exists in the 'cdk-hnb659fds-container-assets-account-id-eu-north-1' repository 
and cannot be overwritten because the repository is immutable.

If I wait at least one minute before I start another deploy, it all works fine. However, it is not realistic in the production environment, where several deploys can be started on the same account in the same minute.

The only customizable thing I see is the asset_name that doesn't seem to influence anything.

Expected Behavior

I expected several stacks to deploy fine and several images to be created with different tags.

Current Behavior

4736 | stack-name-dev:  fail: 
docker push 528688307418.dkr.ecr.eu-north-1.amazonaws.com/cdk-hnb659fds-container-assets-account-id-eu-north-1:51687ba0f9469a37e6d0d900166b328d730f9c58751a61e8ecc7d25ec7dc3640 
exited with error code 1: tag invalid: The image tag '51687ba0f9469a37e6d0d900166b328d730f9c58751a61e8ecc7d25ec7dc3640' 
already exists in the 'cdk-hnb659fds-container-assets-account-id-eu-north-1' repository 
and cannot be overwritten because the repository is immutable.

Reproduction Steps

Create a cdk template with a DockerImageAsset:

        asset = DockerImageAsset(
            self,
            f"{config.stack_name}-docker-image",
            asset_name=f"{config.stack_name}-docker-image",
            directory=os.path.join("infrastructure", "dockerimage"),
        )

Deploy several stacks using the template at the same time (same minute).

Possible Solution

Image tag is probably generated based on the time. Change the generation method.

Additional Information/Context

The cdk deploy command is run inside a Docker container (a separate build for every stack).

CDK CLI Version

2.133.0

Framework Version

No response

Node.js Version

18

OS

ubuntu:20.04

Language

Python

Language Version

Python 3.9.19

Other information

No response

@ukrainiansteak ukrainiansteak added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels May 20, 2024
@github-actions github-actions bot added the @aws-cdk/aws-ecr-assets Related to AWS CDK Docker Image Assets label May 20, 2024
@pahud pahud self-assigned this May 20, 2024
@pahud
Copy link
Contributor

pahud commented May 20, 2024

I'd like to know more about how you deploy them. Are you deploying multiple stacks using the same DockerImageAsset in the same CDK app/stack for the same region using npx cdk deploy --all ?

@pahud pahud added p2 response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. effort/medium Medium work item – several days of effort and removed needs-triage This issue or PR still needs to be triaged. labels May 20, 2024
@pahud pahud removed their assignment May 21, 2024
@ukrainiansteak
Copy link
Author

No, what I meant by simultaneous deployment is that we have a UI system where the user can click "Update" on several applications=stacks (any that have an update available). These stacks are deployed using the same cdk template, but the environment variables are different for each of them (e.g., config.stack_name from my code snippet).

So the region is the same but the environment variables used in the stacks are obviously different.

You can think of it in the following way:

We have stack A with the config.stack_name variable set to stack-a. So the DockerImageAsset code will technically be:

        asset = DockerImageAsset(
            self,
            f"stack-a-docker-image",
            asset_name=f"stack-a-docker-image",
            directory=os.path.join("infrastructure", "dockerimage"),
        )

And if we have stack B with the stack-b name, then this is the code that gets synthed into a cloudformation template:

        asset = DockerImageAsset(
            self,
            f"stack-b-docker-image",
            asset_name=f"stack-b-docker-image",
            directory=os.path.join("infrastructure", "dockerimage"),
        )

When the user clicks "Update", we start a build inside of a CodeBuild project. So for several "updates", several builds of the same CodeBuild project are run. So deploy of each stack is isolated, has its own environment variables and happens at the user click.

@github-actions github-actions bot removed the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label May 21, 2024
@pahud
Copy link
Contributor

pahud commented May 31, 2024

OK this sounds like a race condition when two isolate builds doing cdk deploy at the same time. Both bundle up the same docker image assets with the same tag and push at the same time.

According to this

* The tag of this asset when it is uploaded to ECR. The tag may differ from the assetHash if a stack synthesizer adds a dockerTagPrefix.

I guess you will need to define dockerTagPrefix in your synthesizer and generate unique prefix for it to avoid that. Let me know if it works for you.

@pahud pahud added the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label May 31, 2024
Copy link

github-actions bot commented Jun 2, 2024

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.

@github-actions github-actions bot added closing-soon This issue will automatically close in 4 days unless further comments are made. closed-for-staleness This issue was automatically closed because it hadn't received any attention in a while. and removed closing-soon This issue will automatically close in 4 days unless further comments are made. labels Jun 2, 2024
@github-actions github-actions bot closed this as completed Jun 7, 2024
@aws-cdk-automation
Copy link
Collaborator

Comments on closed issues and PRs are hard for our team to see. If you need help, please open a new issue that references this one.

@aws aws locked as resolved and limited conversation to collaborators Jul 25, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
@aws-cdk/aws-ecr-assets Related to AWS CDK Docker Image Assets bug This issue is a bug. closed-for-staleness This issue was automatically closed because it hadn't received any attention in a while. effort/medium Medium work item – several days of effort p2 response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days.
Projects
None yet
Development

No branches or pull requests

3 participants