Resource: aws_ecr_lifecycle_policy

Manages an ECR repository lifecycle policy.

Example Usage

Policy on untagged image

resource "aws_ecr_repository" "example" {
  name = "example-repo"
}

resource "aws_ecr_lifecycle_policy" "example" {
  repository = aws_ecr_repository.example.name

  policy = <<EOF
{
    "rules": [
        {
            "rulePriority": 1,
            "description": "Expire images older than 14 days",
            "selection": {
                "tagStatus": "untagged",
                "countType": "sinceImagePushed",
                "countUnit": "days",
                "countNumber": 14
            },
            "action": {
                "type": "expire"
            }
        }
    ]
}
EOF
}

Policy on tagged image

resource "aws_ecr_repository" "example" {
  name = "example-repo"
}

resource "aws_ecr_lifecycle_policy" "example" {
  repository = aws_ecr_repository.example.name

  policy = <<EOF
{
    "rules": [
        {
            "rulePriority": 1,
            "description": "Keep last 30 images",
            "selection": {
                "tagStatus": "tagged",
                "tagPrefixList": ["v"],
                "countType": "imageCountMoreThan",
                "countNumber": 30
            },
            "action": {
                "type": "expire"
            }
        }
    ]
}
EOF
}

Argument Reference

This resource supports the following arguments:

Attribute Reference

This resource exports the following attributes in addition to the arguments above:

Import

In Terraform v1.5.0 and later, use an import block to import ECR Lifecycle Policy using the name of the repository. For example:

import {
  to = aws_ecr_lifecycle_policy.example
  id = "tf-example"
}

Using terraform import, import ECR Lifecycle Policy using the name of the repository. For example:

% terraform import aws_ecr_lifecycle_policy.example tf-example