Skip to main content

More Info:

A Lifecycle policy should be defined for each Amazon ECR image repository in order to automatically remove untagged and old container images. A lifecycle policy is a set of one or more management rules, where each rule defines an action for Amazon ECR.

Risk Level

Low

Address

Cost, Operational Maturity, Security

Compliance Standards

  • APRA CPS 234 (Australia)
  • BSI C5 (Germany)
  • Brazil LGPD
  • CCPA / CPRA (California)
  • CIS Critical Security Controls v8
  • CMMC 2.0
  • CSA Cloud Controls Matrix v4
  • Cloudanix Best Practice
  • DPDPA
  • Digital Operational Resilience Act (EU)
  • Essential 8
  • ISO/IEC 27017
  • ISO/IEC 27018
  • ISO/IEC 27701
  • KSA PDPL
  • MAS Technology Risk Management (Singapore)
  • MITRE ATT&CK (Cloud)
  • NIS2 Directive
  • NIST SP 800-171
  • NYDFS 23 NYCRR 500
  • SWIFT Customer Security Controls Framework
  • Sarbanes-Oxley IT General Controls
  • UK NCSC Cyber Assessment Framework

Triage and Remediation

Remediation

Using Console

Sure, here are the steps to remediate this misconfiguration in AWS:
  1. Open the AWS Management Console and navigate to the Elastic Container Registry (ECR) service.
  2. From the ECR dashboard, select the repository for which you want to set the lifecycle policy.
  3. In the repository details page, click on the “Lifecycle policies” tab.
  4. Click on the “Create lifecycle policy” button.
  5. In the “Create lifecycle policy” page, enter a name for the policy and choose the tag status for which you want to apply the policy.
  6. Under “Rules”, choose the actions you want to perform on images that match the tag status you selected in the previous step. For example, you can choose to expire images after a certain number of days or after a certain number of image versions.
  7. Click on the “Create” button to create the lifecycle policy.
  8. The lifecycle policy will now be applied to the selected repository and will automatically perform the actions you specified on images that meet the criteria you set.
That’s it! You have successfully remediated the misconfiguration by setting a lifecycle policy for the ECR image repository.

To remediate the misconfiguration “ECR Image Repositories Should Have A Lifecycle Policy Attached” for AWS using AWS CLI, follow the below steps:Step 1: Open the AWS CLI on your local machine.Step 2: Run the below command to list all the ECR repositories in your AWS account.
Step 3: Identify the repository for which you want to attach a lifecycle policy.Step 4: Create a JSON file with the following contents:
In the above JSON file, you can modify the rule description and the count number as per your requirement.Step 5: Run the below command to attach the lifecycle policy to the repository.
In the above command, replace <repository-name> with the name of the repository you want to attach the lifecycle policy to and <path-to-json-file> with the path to the JSON file you created in step 4.Step 6: Verify that the lifecycle policy is attached to the repository by running the below command.
In the above command, replace <repository-name> with the name of the repository you attached the lifecycle policy to.By following the above steps, you can remediate the misconfiguration “ECR Image Repositories Should Have A Lifecycle Policy Attached” for AWS using AWS CLI.
To remediate the misconfiguration of ECR Image Repositories not having a Lifecycle Policy attached in AWS using Python, follow these steps:
  1. Import the necessary libraries. You will need boto3 library for AWS API calls.
  1. Connect to AWS using boto3 library. You will need to provide your AWS access key and secret access key along with the region name.
  1. Get a list of all the repositories in your AWS account.
  1. Loop through all the repositories and check if a Lifecycle Policy is attached. If not, attach a policy.
  1. The above code will attach a Lifecycle Policy to all the ECR Image Repositories in your AWS account that don’t have a policy attached. The policy will expire images older than 30 days.
Note: Make sure to replace YOUR_ACCESS_KEY, YOUR_SECRET_KEY, and YOUR_REGION_NAME with your AWS access key, secret access key, and region name respectively.
This change does not force replacement of the ECR repository; it updates or creates the lifecycle policy in place but will overwrite any existing policy.To verify, terraform plan should show one new aws_ecr_lifecycle_policy resource added (or updated) attached to the target aws_ecr_repository with the specified JSON policy.

Additional Reading: