Manages an ECR repository lifecycle policy.
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
}
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
}
This resource supports the following arguments:
repository
- (Required) Name of the repository to apply the policy.policy
- (Required) The policy document. This is a JSON formatted string. See more details about Policy Parameters in the official AWS docs. Consider using the aws_ecr_lifecycle_policy_document
data_source to generate/manage the JSON document used for the policy
argument.This resource exports the following attributes in addition to the arguments above:
repository
- The name of the repository.registry_id
- The registry ID where the repository was created.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