1. Answers
  2. Configuring an AWS ECR Lifecycle Policy with Pulumi

How do I configure an AWS ECR lifecycle policy with Pulumi?

In this guide, we’ll walk through setting up an AWS Elastic Container Registry (ECR) and configuring a lifecycle policy to manage the lifecycle of images within that repository. Lifecycle policies help reduce clutter and manage the costs by specifying rules for cleaning up old images.

First, we need to define an ECR repository. Then, we’ll create a lifecycle policy JSON document and apply this policy to the repository. Here’s how you can do it:

import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";

const myRepo = new aws.ecr.Repository("my_repo", {name: "my-ecr-repo"});
const current = aws.getCallerIdentityOutput({});
const ecrLifecyclePolicy = `  {
    "rules": [
      {
        "rulePriority": 1,
        "description": "Expire images older than 14 days",
        "selection": {
          "tagStatus": "any",
          "countType": "sinceImagePushed",
          "countUnit": "days",
          "countNumber": 14,
          "tagPrefixList": [""],
          "tagStatus": "untagged"
        },
        "action": {
          "type": "expire"
        }
      }
    ]
  }
`;
const myLifecyclePolicy = new aws.ecr.LifecyclePolicy("my_lifecycle_policy", {
    repository: myRepo.name,
    policy: ecrLifecyclePolicy,
});
export const repositoryUrl = myRepo.repositoryUrl;
export const accountId = current.apply(current => current.accountId);

In summary, we’ve created an AWS ECR repository and defined a lifecycle policy using a JSON document. This policy will delete any images older than 14 days. Finally, we applied the lifecycle policy to the repository and exported the repository URL and AWS account ID for reference.

Deploy this code

Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.

Sign up

New to Pulumi?

Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.

Sign up