Creating a lifecycle policy
A lifecycle policy allows you to create a set of rules that expire unused repository images. The following procedure shows you how to create a lifecycle policy. You should expect that after creating a lifecycle policy, the affected images are expired within 24 hours.
Important
It is considered best practice to create a lifecycle policy preview to ensure that the images affected by your lifecycle policy rules are what you intend. For more information, see Creating a lifecycle policy preview.
To create a lifecycle policy using the console
Open the Amazon ECR console at https://console.aws.amazon.com/ecr/repositories
. -
From the navigation bar, choose the Region that contains the repository for which to create a lifecycle policy.
-
In the navigation pane, choose Repositories.
-
On the Repositories page, on the Private tab, select a repository to view the repository image list.
-
On the repository image list view, in the left navigation pane, choose Lifecycle Policy.
Note
If you don't see the Lifecycle Policy option in the navigation pane, ensure that you are in the repository image list view.
-
On the repository lifecycle policy page, choose Create rule.
-
Enter the following details for your lifecycle policy rule.
-
For Rule priority, type a number for the rule priority.
-
For Rule description, type a description for the lifecycle policy rule.
-
For Image status, choose Tagged, Untagged, or Any.
-
If you specified
Tagged
for Image status, then for Tag prefixes, you can optionally specify a list of image tags on which to take action with your lifecycle policy. If you specifiedUntagged
, this field must be empty. -
For Match criteria, choose values for Since image pushed or Image count more than (if applicable).
-
Choose Save.
-
-
Create additional lifecycle policy rules by repeating steps 5–7.
To create a lifecycle policy using the AWS CLI
-
Obtain the name of the repository for which to create the lifecycle policy.
aws ecr describe-repositories
-
Create a local file named
policy.json
with the contents of the lifecycle policy. For lifecycle policy examples, see Examples of lifecycle policies. -
Create a lifecycle policy by specifying the repository name and reference the lifecycle policy JSON file you created.
aws ecr put-lifecycle-policy \ --repository-name
repository-name
\ --lifecycle-policy-text file://policy.json