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 build command, 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 using docker push, Docker, under Overlay2, pushes layer-by-layer and ends with an update to the remote image’s manifest.

The containerd-snapshotter feature, 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 multiple PutImage calls 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 to PutImage for 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 call PutImage while 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-snapshotter feature are incompatible with ECR repositories configured to use immutable image tags, because once the first ecr:PutImage call is made (and presumably succeeds), every subsequent call to ecr:PutImage will fail on that same tag. Additionally, despite the push failing locally, we still see the image appear in ECR, because the first call to ecr:PutImage contains 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

  1. Enable containerd in Docker as per https://docs.docker.com/engine/storage/containerd/#enable-containerd-image-store-on-docker-engine
  2. Run cdk bootstrap
  3. Call cdk deploy with any Docker image (ECR repository, Lambda function)
  4. The docker push process will fail with a 400 BadRequest

Possible Solution

  1. Tag immutability for cdk bootstrap should be optional
  2. 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

Most upvoted comments

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=1 environment variable gets rid of the layers that ECR treats as duplicate (at least for our use case) when using docker buildx ... (and consequently when using CDK’s DockerImageAsset, which does actually use docker build, but most people have that set as a pass through to docker 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.

$ docker version
Client: Docker Engine - Community
 Version:           27.2.1
 API version:       1.47
 Go version:        go1.22.7
 Git commit:        9e34c9b
 Built:             Fri Sep  6 12:08:10 2024
 OS/Arch:           linux/amd64
 Context:           default

Server: Docker Engine - Community
 Engine:
  Version:          27.2.1
  API version:      1.47 (minimum version 1.24)
  Go version:       go1.22.7
  Git commit:       8b539b8
  Built:            Fri Sep  6 12:08:10 2024
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.7.22
  GitCommit:        7f7fdf5fed64eb6a7caf99b3e12efcf9d60e311c
 runc:
  Version:          1.1.14
  GitCommit:        v1.1.14-0-g2c9f560
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0
$ docker buildx version
github.com/docker/buildx v0.16.2 99dea6d
$ cat /etc/docker/daemon.json
{
        "features": {
                "containerd-snapshotter": true
        }
}
$ node --version
v20.18.0
$ npx cdk --version
2.165.0 (build 00f70f1)

node_modules/aws-cdk-lib/package.json’s version field: 2.165.0


EDIT: 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 use DockerImageAsset and then feed it into an ECS task definition. That task will fail to pull the image… The issue is then quite dangerous. You run npx cdk deploy ... for the first time, it builds the image, then fails pushing it. The second time, you run npx 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=1 env 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 -

  1. the immutable ECR repo design, can’t be removed as this ensures all image assets CDK CLI publishes to ECR is immutable with a unique tag. Looks like this is mandatory and would have reprecussions so not feasible to change it.
  2. The reason AssetImageCodeProps has cacheFrom and cacheTo is 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)