ECR repos with CDK

02-08-25

%

Self-hosting a docker image reg didn't work out for me, so I'm still using AWS's ECR. This month I got hit with a 10 EUR bill! Switched to managing the repos with CDK and a lifecycle policy that deletes images after two days to save money.

for (const repo of props?.ecrRepos || []) {
    const uuid = Names.uniqueId(this)
      .toLowerCase()
      .replace(/[^a-z0-9-_]/g, "");
    const uniqueName = `${repo}-${uuid}`;
    new ecr.Repository(this, `ECRRepo_${repo}`, {
      repositoryName: uniqueName,
      lifecycleRules: [
        {
          rulePriority: 1,
          description: "Delete images older than 48h",
          maxImageAge: Duration.days(2),
        },
      ],
    });
}

- [ awscdk]

all snippets