Skip to main content

CDK construct to deploy docker image to Amazon ECR

Project description

cdk-ecr-deployment

Release npm version PyPI npm PyPI - Downloads

CDK construct to synchronize single docker image between docker registries.

[!IMPORTANT]

Please use the latest version of this package, which is v4.

(Older versions are no longer supported).

Features

  • Copy image or multi-architecture image index from ECR/external registry to (another) ECR/external registry
  • Copy an archive tarball image from s3 to ECR/external registry

Examples

from aws_cdk.aws_ecr_assets import DockerImageAsset


image = DockerImageAsset(self, "CDKDockerImage",
    directory=path.join(__dirname, "docker")
)

# Copy from cdk docker image asset to another ECR.
ecrdeploy.ECRDeployment(self, "DeployDockerImage1",
    src=ecrdeploy.DockerImageName(image.image_uri),
    dest=ecrdeploy.DockerImageName(f"{cdk.Aws.ACCOUNT_ID}.dkr.ecr.us-west-2.amazonaws.com/my-nginx:latest")
)

# Copy from docker registry to ECR.
ecrdeploy.ECRDeployment(self, "DeployDockerImage2",
    src=ecrdeploy.DockerImageName("nginx:latest"),
    dest=ecrdeploy.DockerImageName(f"{cdk.Aws.ACCOUNT_ID}.dkr.ecr.us-west-2.amazonaws.com/my-nginx2:latest")
)

# Copy from private docker registry to ECR.
# The format of secret in aws secrets manager must be either:
# - plain text in format <username>:<password>
# - json in format {"username":"<username>","password":"<password>"}
ecrdeploy.ECRDeployment(self, "DeployDockerImage3",
    src=ecrdeploy.DockerImageName("javacs3/nginx:latest", "username:password"),
    # src: new ecrdeploy.DockerImageName('javacs3/nginx:latest', 'aws-secrets-manager-secret-name'),
    # src: new ecrdeploy.DockerImageName('javacs3/nginx:latest', 'arn:aws:secretsmanager:us-west-2:000000000000:secret:id'),
    dest=ecrdeploy.DockerImageName(f"{cdk.Aws.ACCOUNT_ID}.dkr.ecr.us-west-2.amazonaws.com/my-nginx3:latest")
).add_to_principal_policy(iam.PolicyStatement(
    effect=iam.Effect.ALLOW,
    actions=["secretsmanager:GetSecretValue"
    ],
    resources=["*"]
))

# Copy multi-architecture image index (manifest) with all architectures.
ecrdeploy.ECRDeployment(self, "DeployDockerImage4",
    src=ecrdeploy.DockerImageName("public.ecr.aws/nginx/nginx:latest"),
    dest=ecrdeploy.DockerImageName(f"{cdk.Aws.ACCOUNT_ID}.dkr.ecr.us-west-2.amazonaws.com/my-nginx4:manifest"),
    copy_image_index=True,
    arch_image_tags={
        "amd64": "my-nginx-amd64",
        "arm64": "my-nginx-arm64"
    }
)

Sample: test/example.ecr-deployment.ts

After cloning the repository, install dependencies and run a full build:

yarn --frozen-lockfile --check-files
yarn build

Then run the example like this:

# Run the following command to try the sample.
npx cdk deploy -a "npx ts-node -P tsconfig.dev.json --prefer-ts-exts test/example.ecr-deployment.ts"

To run the DockerHub example you will first need to setup a Secret in AWS Secrets Manager to provide DockerHub credentials. Replace username:access-token with your credentials. Please note that Secrets will occur a cost.

aws secretsmanager create-secret --name DockerHubCredentials --secret-string "username:access-token"

From the output, copy the ARN of your new secret and export it as env variable

export DOCKERHUB_SECRET_ARN="<ARN>"

Finally run:

# Run the following command to try the sample.
npx cdk deploy -a "npx ts-node -P tsconfig.dev.json --prefer-ts-exts test/dockerhub-example.ecr-deployment.ts"

If your Secret is encrypted, you might have to adjust the example to also grant decrypt permissions.

API

Tech Details & Contribution

The core of this project relies on containers/image which is used by Skopeo. Please take a look at those projects before contribution.

To support a new docker image source(like docker tarball in s3), you need to implement image transport interface. You could take a look at docker-archive transport for a good start.

Any error in the custom resource provider will show up in the CloudFormation error log as Invalid PhysicalResourceId, because of this: https://github.com/aws/aws-lambda-go/issues/107. You need to go into the CloudWatch Log Group to find the real error.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cdk_ecr_deployment-4.1.4.tar.gz (21.4 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cdk_ecr_deployment-4.1.4-py3-none-any.whl (21.4 MB view details)

Uploaded Python 3

File details

Details for the file cdk_ecr_deployment-4.1.4.tar.gz.

File metadata

  • Download URL: cdk_ecr_deployment-4.1.4.tar.gz
  • Upload date:
  • Size: 21.4 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.14.3

File hashes

Hashes for cdk_ecr_deployment-4.1.4.tar.gz
Algorithm Hash digest
SHA256 99d46a0c25055d8fe1bf3107bbe7e62f6c954f18e0ff4489ee5cccd35e4c9ece
MD5 2a837374621466ea24488b4d9f5a6c35
BLAKE2b-256 cb9a66a8fd29eef29c0f7169c2a62c4c9e6f59756dd56b07ee7ec6f0b86c70aa

See more details on using hashes here.

Provenance

The following attestation bundles were made for cdk_ecr_deployment-4.1.4.tar.gz:

Publisher: release.yml on cdklabs/cdk-ecr-deployment

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cdk_ecr_deployment-4.1.4-py3-none-any.whl.

File metadata

File hashes

Hashes for cdk_ecr_deployment-4.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 3dd455198c47eddc0d4c47902545b58c8aadd9475f6e83205359fbbf373fa5ef
MD5 4ae3c1c63932493bcdc53db64acb6cc8
BLAKE2b-256 18cd6415203f5bba250da8988136b8c959565c239dd5d2c54d13487a16fe6254

See more details on using hashes here.

Provenance

The following attestation bundles were made for cdk_ecr_deployment-4.1.4-py3-none-any.whl:

Publisher: release.yml on cdklabs/cdk-ecr-deployment

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page