Skip to content

pgarbe/cdk-ecr-sync

Repository files navigation

CDK ECR Sync

An CDK Construct to synchronizes Docker images from Docker Hub to ECR.

Why should you use it?

  1. Avoid DockerHub throttling
  2. Better availability (ECR: 99.9%, DockerHub: "as is")
  3. Possible less data transfer costs (ECS pulls from ECR in same region are free)

See also my blog post: https://garbe.io/blog/2020/04/22/cdk-ecr-sync/

How to use it

In your CDK application, run npm install @pgarbe/cdk-ecr-sync and add the following construct:

const ecrSync = new EcrSync(this, 'ecrSync', {
  repoPrefix: 'dockerhub-mirror', // optional prefix
  dockerImages: [
    {
      imageName: 'datadog/agent',
      excludeTags: [  // Use RegEx expressions to exclude specific tags
        'latest',
        '^1.0'
      ],
    }
  ],
  lifcecyleRule: {...} // Optional lifecycle rule for ECR repos
});

// Allow all accounts in your organization to pull
ecrSync.grantPull(new iam.OrganizationalPrincipal('o-123456'));

See also examples folder.

Trigger the sync manually

Look up for a lambda called *ecrSync*%*, add an empty test event, and execute it. The lambda will check which images needs to be synced and trigger a CodePipeline.