CDK ECR Deployment

4.2.0 · active · verified Wed Apr 15

cdk-ecr-deployment is a CDK construct that facilitates the deployment and synchronization of Docker images to Amazon ECR. It enables copying images from various sources, including Docker Hub, other ECR repositories, and S3 archive tarballs, to a specified ECR destination. The library is actively maintained, with version 4.2.0 currently available, and receives frequent minor and patch releases.

Warnings

Install

Imports

Quickstart

To get started, create a CDK app (`cdk init app --language python`), then replace the content of your main stack file (e.g., `your_stack.py`) and your `app.py` with the code above. Remember to install `aws-cdk-lib`, `constructs`, and `cdk-ecr-deployment`. Ensure your AWS CLI is configured and your AWS environment is bootstrapped (`cdk bootstrap`) before deploying with `cdk deploy`.

import os
from aws_cdk import (
    App,
    Stack,
    Environment,
    aws_ecr as ecr,
    Aws,
)
from constructs import Construct
from cdk_ecr_deployment import ECRDeployment, DockerImageName

class MyEcrDeploymentStack(Stack):
    def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
        super().__init__(scope, construct_id, **kwargs)

        # 1. Define a destination ECR repository
        destination_repo = ecr.Repository(self, "MyDestinationEcrRepo",
            repository_name="my-app-image-destination",
            image_scan_on_push=True,
            image_tag_mutability=ecr.TagMutability.MUTABLE
        )

        # 2. Deploy a Docker image from Docker Hub (e.g., 'nginx:latest') to the ECR repository.
        #    Ensure your AWS credentials are configured (e.g., via AWS CLI) and your
        #    CDK environment is bootstrapped (run 'cdk bootstrap' once per account/region).
        ECRDeployment(self, "DeployPublicNginxImage",
            src=DockerImageName("nginx:latest"),
            dest=DockerImageName(f"{Aws.ACCOUNT_ID}.dkr.ecr.{Aws.REGION}.amazonaws.com/{destination_repo.repository_name}:latest"),
        )

app = App()
MyEcrDeploymentStack(app, "CdkEcrDeploymentExampleStack",
    env=Environment(account=os.getenv('CDK_DEFAULT_ACCOUNT'), region=os.getenv('CDK_DEFAULT_REGION')),
)

app.synth()

view raw JSON →