aws-cdk: (cdk bootstrap): immutable tag immutability on ECR repository created during bootstrap cannot work alongside `containerd`
Describe the bug
As per the docs, when calling cdk bootstrap, it creates an Amazon Elastic Container Registry (Amazon ECR) repository – Used primarily to store Docker images.
As per bootstrap-template.yml - This repository has tag immutability set to immutable.
CDK provides the ability to utilize Docker cache storage backends as per the docs.
In order to make use of cache storage backends such as a local directory or external registry, it is a requirement to enable containerd image store in Docker.
Once containerd has been enabled, you are able to populate the build cache parameters within CDK, and subsequent builds on different CI runners would all utilise the same backend cache, drastically improving the CDK build and deployment process.
Once containerd has been enabled, you are unable to push any Docker images to the ECR repository created during the bootstrap phase, or encounter the following error:
failed commit on ref “manifest-sha256:…”: unexpected status from PUT request to https://…dkr.ecr.us-east-2.amazonaws.com/v2/foo/manifests/bar: 400 Bad Request
This is because containerd creates 2 ecr:PutImage calls to the same layer name as per CloudTrail.
The explanation from AWS Support:
Containerd uses different storage configurations to Docker’s Overlay2 for storing image and container data. Overlay2, which is the default storage driver for Docker, exposes images as a series of layers, up to 128 in a single image. Docker, by default, adopts a layer-related approach during the
docker buildcommand, looking for various OverlayFS layers comprising the base image and the built image. When starting a container under Docker running Overlay2, Docker merges the image’s top layer (“upperdir”) and a new container directory into the “merged layer”, which is writable. When pushing an image usingdocker push, Docker, under Overlay2, pushes layer-by-layer and ends with an update to the remote image’s manifest.
The
containerd-snapshotterfeature, instead, stores images as a series of snapshots. Each snapshot loosely corresponds to an Overlay2 image layer, but unlike under Overlay2, each snapshot contains the contents of that particular “layer”, plus the files from the snapshot before it. Essentially, a given snapshot contains its own contents, plus the information in its parent snapshot.
So why does pushing a
containerd-snapshotter-built image to ECR results in multiplePutImagecalls being made? It’s likely that each snapshot, containing all the information of the snapshot before it, is being treated as being equivalent to a full Overlay2-structured image by ECR, meaning a call toPutImagefor each snapshot at the end of the string of layer upload calls to update the image metadata for every snapshot. As mentioned before, any individual snapshot contains more information than the corresponding Overlay2 image layer, but ECR can’t make this distinction. ECR does not callPutImagewhile there are still image layers to be uploaded, meaning it queues the calls to PutImage after all the layers have finished uploading.
Ultimately, this does mean that images built with the
containerd-snapshotterfeature are incompatible with ECR repositories configured to use immutable image tags, because once the firstecr:PutImagecall is made (and presumably succeeds), every subsequent call toecr:PutImagewill fail on that same tag. Additionally, despite the push failing locally, we still see the image appear in ECR, because the first call toecr:PutImagecontains the manifest which only corresponds to the base snapshot.
Regression Issue
- Select this option if this issue appears to be a regression.
Last Known Working CDK Version
n/a
Expected Behavior
I would expect to be able to make use of the documented CDK feature to use different cache storage backends with CDK. There is documentation and examples on enabling caching, but no mention that once containerd has been enabled, that you will encounter a 400 BadRequest error without much context as to why.
Current Behavior
Once containerd has been enabled to make use of the documented CDK feature to use different cache storage backends with CDK, any attempt to call cdk deploy is met with a 400 BadRequest error when pushing any Docker images to ECR. The issue does not provide any useful context, and AWS Support had to be involved to provide assistance.
Reproduction Steps
- Enable containerd in Docker as per https://docs.docker.com/engine/storage/containerd/#enable-containerd-image-store-on-docker-engine
- Run
cdk bootstrap - Call
cdk deploywith any Docker image (ECR repository, Lambda function) - The
docker pushprocess will fail with a400 BadRequest
Possible Solution
- Tag immutability for
cdk bootstrapshould be optional - The documentation for docker caching should reflect this issue.
Additional Information/Context
No response
CDK CLI Version
2.160.0 (build 7a8ae02)
Framework Version
No response
Node.js Version
v18.17.1
OS
MacOS 14.6
Language
TypeScript
Language Version
No response
Other information
No response
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 16
- Comments: 16 (3 by maintainers)
Commits related to this issue
- fix: docker build attestations break cdk-assets (400 Bad Request) (#342) There are various issues in cdk that can be traced back to attestations in docker: https://github.com/aws/aws-cdk/issues/3025... — committed to cdklabs/cdk-assets by kaizencc 2 years ago
- fix: docker build attestations break cdk-assets (400 Bad Request) (#342) There are various issues in cdk that can be traced back to attestations in docker: https://github.com/aws/aws-cdk/issues/3025... — committed to cdklabs/cdk-assets by kaizencc 2 years ago
- chore(deps): update cdk-assets (#33415) The lastest cdk-assets is required in cdk to mitigate a ECR upload issue. It includes the following fix: https://github.com/cdklabs/cdk-assets/pull/342. The fo... — committed to aws/aws-cdk by kaizencc 2 years ago
- chore(deps): update cdk-assets (#33415) The lastest cdk-assets is required in cdk to mitigate a ECR upload issue. It includes the following fix: https://github.com/cdklabs/cdk-assets/pull/342. The fo... — committed to yashkh-amzn/aws-cdk by kaizencc 2 years ago
- chore(deps): update cdk-assets (#33415) The lastest cdk-assets is required in cdk to mitigate a ECR upload issue. It includes the following fix: https://github.com/cdklabs/cdk-assets/pull/342. The fo... — committed to shikha372/aws-cdk by kaizencc 2 years ago
New Macbook user here: disabling containerd fixed it for me as well. What an absolute waste of time it was debugging this error.
I found that setting
BUILDX_NO_DEFAULT_ATTESTATIONS=1environment variable gets rid of the layers that ECR treats as duplicate (at least for our use case) when usingdocker buildx ...(and consequently when using CDK’sDockerImageAsset, which does actually usedocker build, but most people have that set as a pass through todocker buildx).With this environment variable set, we are able to use containerd image store (which is now the default in Docker Desktop) and the current CDK (with tag immutability enabled on the bootstrapped ECR CDK repository).
E.g.
BUILDX_NO_DEFAULT_ATTESTATIONS=1 npx cdk deploy ...For me, this builds the image and always succeeds when pushing it.
Reference: https://docs.docker.com/build/metadata/attestations/
Some runtime information from my setup, in case people are interested in replicating this.
node_modules/aws-cdk-lib/package.json’s version field:2.165.0EDIT: Just in case you were hit by this when using
DockerImageAsset. We noticed that while the image push “seems” to fail, the image with the tag exists in the ECR repo afterwards (it makes sense – that’s why we get the tag immutability violation error). The problem then is, CDK only checks whether an image with a given tag exists in the repo. As long as there is an image, it won’t try to push the image again. However, the image is useless (ECR reports it having size of 0 MB). So … if for example you useDockerImageAssetand then feed it into an ECS task definition. That task will fail to pull the image… The issue is then quite dangerous. You runnpx cdk deploy ...for the first time, it builds the image, then fails pushing it. The second time, you runnpx cdk deploy ...and it doesn’t even try to build and push the image, because there is already an image in the repo, failing horribly some time after (when the image actually gets pulled).So, my suggestion is: if you are hit by this, do not try using the
BUILDX_NO_DEFAULT_ATTESTATIONS=1env var right away, but first change something in your image’s sources (or manually delete all images with 0 MB in size from the repo – this might actually be a better tactic). Otherwise, you run the risk of CDK not even attempting to rebuild the image 😃Disabling containerd image store in Docker for Mac resolved this issue for me.
I had recently moved to a new Macbook and was having the same image push problems outlined above.
This is a terrible developer experience. This issue means a default configuration, following all the docs, does not work out of the box. One needs to be AWS-savy to find the issue description here and to understand it.
Can we implement a fix or a workaround that allows the default developer experience to just work ™ ?
Is there a workaround? I’m currently stuck.
Hi @neilferreira , thanks for reaching out.
After discussion with the team,sharing the findings here -
AssetImageCodePropshascacheFromandcacheTois because it derives from DockerImageAssetsOptions from aws-ecr-assets from this PR, which essentially passes the parameters to docker CLI. Unfortunately that PR does not mention containerd compatibility. I believe the original intention is to use the cache storage backend with BuildKit and you need to use docker buildx for that. CDK, however, is not fully tested with buildx at this moment and we are still exploring how to replace the docker command with docker buildx. related issues: https://github.com/aws/aws-cdk/issues/28517 https://github.com/aws/aws-cdk/issues/24685#issuecomment-1499487688 I believe using BuildX might be a solution with immutable images unchanged and leverage the cache storage backend feature.Marking issue as P2 which means it might not be immediately addressed by the team but would be on their radar.
Also, I will reach out to team for their input if this is something we are actively investigating and share our thoughts here if possible.
Hope that would be helpful!.
cc: @pahud
+1 on receiving this issue with existing CDK deployments when upgrading to a new PC (which means newest installs of versions of docker/other dependencies)