Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[ECR] Fails to deploy if ECR repository already exists

See original GitHub issue

Reproduction Steps

import cdk = require('@aws-cdk/core');

import ecr = require('@aws-cdk/aws-ecr');

export class EcrStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    const ecrRepo = new ecr.Repository(this, 'MyECRRepo', {
      repositoryName: 'my-ecr-repo'

    new cdk.CfnOutput(this, 'MyECRRepoURI', {
      value: ecrRepo.repositoryUri,

Error Log

I see an error state the ECR Repository with name my-ecr-repo already exists and the CFN deployment cancels and rolls back.


  • CLI Version :1.16.3
  • Framework Version: 1.16.3
  • OS : macOS
  • **Language : typescript **


This is 🐛 Bug Report

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:9
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

J11522commented, Aug 3, 2020

@rix0rrr Could you provide a best-practice on how to approach a situation like this?

E.g. we want to setup a pipeline that pushes to ECR and have our deployment pull from ECR. Of course we can’t delete the repository when the pipeline is removed nor use dynamic names for the ECR as our deployment depends on this. Hence we would choose static names and only create a new ECR repository in the pipeline if one with the name doesn’t exist.

One common workaround found online is to query the ecr for existing repos, however this approach is of course not safe due to the async nature of synth and deploy. Furthermore this would introduce an anti-pattern as referenced here:

Would this be addresses by the context provider framework?

Judging from the comments here and elsewhere, this seems like a common use case.

I’d be very curious to hear how this should ideally be approached.

cmckni3commented, Nov 21, 2019

Correct, this is the only place I specify a name.

I would imagine resource update would be skipped if the name is the same as the existing resource especially when nothing on the resource has changed in the updated template.

That seems like a CloudFormation issue in that case.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Troubleshooting Amazon ECR error messages
HTTP 404: "Repository Does Not Exist " error​​ For more information about creating repositories, see Amazon ECR private repositories. To use the Amazon...
Read more >
Create AWS ECR repository if it doesn't exist - Stack Overflow
One liner to create a repo if it doesn't exist (or the describe command fails for any other reason): aws ecr describe-repositories ......
Read more >
Found out that CDK throws an error if the resource already ...
Lo and behold AWS starts screaming at me in caps that ECR repo with a matching name already exists. It then rolls everything...
Read more >
Should the repo be created if it does not exist? ... Whether to skip image building if all specified tags already exist in...
Read more >
"Already exists in stack" error when attempting to re-deploy a ...
I had the same issue. It was caused by an earlier development stage where I'd created my s3 bucket manually. Removing the s3...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Post

No results found

github_iconTop Related Hashnode Post

No results found