Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CDK ECR: How to set repository name and tag

How to set repository name and tag on DockerImageAsset of CDK?

This is my code: (groovy)

private uploadImage(String name, File directory, File jarFile) {
    def asset = DockerImageAsset.Builder.create(scope, cdkId("$name-image"))
            .directory(directory.toString())
            .buildArgs([
                    "jarFile"    : jarFile.name,
                    "environment": config.environment
            ])
            .build()
    println "ImageURL: $asset.imageUri"
}

This is the image url printed:

ImageURL: 9999999999999.dkr.ecr.us-west-2.${Token[AWS.URLSuffix.1]}/aws-cdk/assets:89ae89e0b3f7652a4ac70a4e18d6b2acec2abff96920e81e6487da58c8b820f3

I guess this is meant to be this way for CI/CD, where it doesn't matter the repository name/tag.

But if you need to use this image outside of CI/CD environment, it turns into a mess as we have many different projects and versions in the same repository (aws-cdk/assets).

I see a method called "repositoryName" but it is deprecated and I couldn't find an alternative or an explanation of why it is deprecated.

like image 223
bonuspack Avatar asked Oct 28 '25 10:10

bonuspack


2 Answers

This isnt a direct answer to your question, as this solution uses NodeJS rather than Groovy, but it may help others that end up here.

You could check out the cdk-ecr-deployment library in the cdklabs repository, which gives a construct allowing you to deploy docker images to ECR.

This is done in three steps:

  1. Create the repo
const repo = new ecr.Repository(this, 'NginxRepo', {
  repositoryName: 'nginx',
  removalPolicy: RemovalPolicy.DESTROY,
});
  1. Create the image
const image = new DockerImageAsset(this, 'CDKDockerImage', {
  directory: path.join(__dirname, 'docker'),
});
  1. Tag and deploy the image to the repo
new ecrDeploy.ECRDeployment(this, 'DeployDockerImage', {
  src: new ecrDeploy.DockerImageName(image.imageUri),
  dest: new ecrDeploy.DockerImageName(`${repo.repositoryUri}:latest`),
});

See the example for more context on how this is used.

like image 109
devklick Avatar answered Oct 30 '25 01:10

devklick


From CDK Docs :

DockerImageAsset is designed for seamless build & consumption of image assets by CDK code deployed to multiple environments through the CDK CLI or through CI/CD workflows. To that end, the ECR repository behind this construct is controlled by the AWS CDK. The mechanics of where these images are published and how are intentionally kept as an implementation detail, and the construct does not support customizations such as specifying the ECR repository name or tags.

like image 27
Mandar K Avatar answered Oct 30 '25 01:10

Mandar K