Search code examples
amazon-web-servicesdockeraws-cdkamazon-ecr

aws cdk push image to ecr


I am trying to do something that seems fairly logical and straight forward.

I am using the AWS CDK to provision an ecr repo:

repository = ecr.Repository(
    self, 
    id="Repo", 
    repository_name=ecr_repo_name,
    removal_policy=core.RemovalPolicy.DESTROY
)

I then have a Dockerfile which lives at the root of my project that I am trying to push to the same ECR repo in the deployment.

I do this in the same service code with:

assets = DockerImageAsset(
    self, 
    "S3_text_image", 
    directory=str(Path(__file__).parent.parent),
    repository_name=ecr_repo_name
)

The deployment is fine and goes ahead and the ECR Repo is created, but the image is pushed to a default location aws-cdk/assets

How do I make the deployment send my Dockerfile to the exact ECR repo I want it to live in ?


Solution

  • AWS CDK depricated the repositoryName property on DockerImageAsset. There are a few issues on GitHub referencing the problem. See this comment from one of the developers:

    At the moment the CDK comes with 2 asset systems:

    The legacy one (currently still the default), where you get to specify a repositoryName per asset, and the CLI will create and push to whatever ECR repository you name.

    The new one (will become the default in the future), where a single ECR repository will be created by doing cdk bootstrap and all images will be pushed into it. The CLI will not create the repository any more, it must already exist. IIRC this was done to limit the permissions required for deployments. @eladb, can you help me remember why we chose to do it this way?

    There is a request for a new construct that will allow you to deploy to a custom ECR repository at (aws-ecr-assets) ecr-deployment #12597.

    Use Case

    I would like to use this feature to completely deploy my local image source code to ECR for me using an ECR repo that I have previously created in my CDK app or more importantly outside the app using an arn. The biggest problem is that the image cannot be completely abstracted into the assets repo because of auditing and semantic versioning.

    There is also a third party solution at https://github.com/wchaws/cdk-ecr-deployment if you do not want to wait for the CDK team to implement the new construct.