AWS Amazon ECR Lifecycle Policy

This page shows how to write Terraform and CloudFormation for Amazon ECR Lifecycle Policy and write them securely.

aws_ecr_lifecycle_policy (Terraform)

The Lifecycle Policy in Amazon ECR can be configured in Terraform with the resource name aws_ecr_lifecycle_policy. The following sections describe 4 examples of how to use the resource and its parameters.

Example Usage from GitHub

aws_ecr_lifecycle_policy.tf#L1
resource "aws_ecr_lifecycle_policy" "fascia_last" {
  repository = aws_ecr_repository.fascia.name

  policy = file("aws_ecr_lifecycle_policy/last.json")
}

ecr.tf#L19
resource "aws_ecr_lifecycle_policy" "app" {
  repository = aws_ecr_repository.app.name

  policy = <<EOF
  {
    "rules": [
ecr.tf#L19
resource "aws_ecr_lifecycle_policy" "app" {
  repository = aws_ecr_repository.app.name

  policy = <<EOF
  {
    "rules": [
repositories.tf#L10
resource "aws_ecr_lifecycle_policy" "repo_apiserver" {
  repository = aws_ecr_repository.repo_apiserver.name
  policy     = local.repo_lifecycle_policy
}


Review your Terraform file for AWS best practices

Shisho Cloud, our free checker to make sure your Terraform configuration follows best practices, is available (beta).

Parameters

Explanation in Terraform Registry

Manages an ECR repository lifecycle policy.

NOTE: Only one aws_ecr_lifecycle_policy resource can be used with the same ECR repository. To apply multiple rules, they must be combined in the policy JSON. NOTE: The AWS ECR API seems to reorder rules based on rulePriority. If you define multiple rules that are not sorted in ascending rulePriority order in the Terraform code, the resource will be flagged for recreation every terraform plan.

Tips: Best Practices for The Other AWS Amazon ECR Resources

In addition to the aws_ecr_repository, AWS Amazon ECR has the other resources that should be configured for security reasons. Please check some examples of those resources and precautions.

risk-label

aws_ecr_repository

Ensure to enable ECR image scan

It is better to enbale ECR image scan. AWS ECR provides a feature to scan container images and discover vulnerable software.

Review your AWS Amazon ECR settings

In addition to the above, there are other security points you should be aware of making sure that your .tf files are protected in Shisho Cloud.

AWS::ECR::Repository LifecyclePolicy (CloudFormation)

The Repository LifecyclePolicy in ECR can be configured in CloudFormation with the resource name AWS::ECR::Repository LifecyclePolicy. The following sections describe how to use the resource and its parameters.

Example Usage from GitHub

An example could not be found in GitHub.

Parameters

LifecyclePolicyText The JSON repository policy text to apply to the repository.
Required: No
Type: String
Minimum: 100
Maximum: 30720
Update requires: No interruption

RegistryId The AWS account ID associated with the registry that contains the repository. If you do
 not specify a registry, the default registry is assumed.
Required: No
Type: String
Pattern: [0-9]{12}
Update requires: No interruption

Explanation in CloudFormation Registry

The LifecyclePolicy property type specifies a lifecycle policy. For information about lifecycle policy syntax, see Lifecycle policy template in the Amazon ECR User Guide.

Frequently asked questions

What is AWS Amazon ECR Lifecycle Policy?

AWS Amazon ECR Lifecycle Policy is a resource for Amazon ECR of Amazon Web Service. Settings can be wrote in Terraform and CloudFormation.

Where can I find the example code for the AWS Amazon ECR Lifecycle Policy?

For Terraform, the h3poteto/h3poteto-terraform-aws, kshina76/centos-backup and kshina76/centos-backup source code examples are useful. See the Terraform Example section for further details.