9

I'm using CDK for a while and I'm not sure yet which is the best way to launch a stack that create an ECR repository, building and sending a docker image to ECR.

My last try was something like that:

    taskDefinition.addContainer("container", {
      image: new AssetImage('./', {
        repositoryName: "name"
      })
    });

But there is an issue on this approach, the repositoryName is deprecated at AssetImage class and it looks deprecated everywhere.

Can someone tells how can we launch this kind of stuff?

Robson Andrade
  • 101
  • 1
  • 1
  • 5

3 Answers3

9

The answer from Pedreiro shows the right way to build and upload an image. CDK will automatically create an ECR repository for you in that case.

I highly recommend to use the aws-ecs-patterns module to get stared with ECS on CDK. It provides already some common use cases where you can deploy an ECS service with very little code. The overview page is a good starting point: https://docs.aws.amazon.com/cdk/api/latest/docs/aws-ecs-patterns-readme.html

But in the unlikely case you wanted to just create an ECR repository using CDK you can do it like this:

const repository = new ecr.Repository(this, 'Repository');

Check out the corresponding docs: https://docs.aws.amazon.com/cdk/api/latest/docs/aws-ecr-readme.html

Karsten Planz
  • 304
  • 1
  • 5
6

The code you are showing is not building an ECR and instead, deploying an ECS task definition. If you are trying to build the docker image at deployment time, CDK has a handy funcionality to do both at the same time with assets:

image: ecs.ContainerImage.fromAsset('./image') // build and upload an image directly from a Dockerfile in your source directory.

Otherwise, please check the Image options available in the docs: https://docs.aws.amazon.com/cdk/api/latest/docs/aws-ecs-readme.html#images

Pedreiro
  • 1,641
  • 2
  • 18
  • 28
2

Notice there's an issue with

image: ecs.ContainerImage.fromAsset('./image')

https://github.com/aws/aws-cdk/issues/2663 where you can not specify a tag, as a result in the ECS task it by default go for the latest, and the pull img will fail

Yang
  • 81
  • 1
  • 7