Creating a lifecycle policy for a repository in Amazon ECR - Amazon ECR
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Creating a lifecycle policy for a repository in Amazon ECR

Use a lifecycle policy to create a set of rules that expire unused repository images. After creating a lifecycle policy, the affected images are expired within 24 hours.

Note

If you are using Amazon ECR replication to make copies of a repository across different Regions or accounts, note that a lifecycle policy can only take an action on repositories in the Region it was created in. Therefore, if you have replication turned on you may want to create a lifecycle policy in each Region and account you are replicating your repositories to.

Prerequisite

Best practice: Create a lifecycle policy preview to verify that the images expired by your lifecycle policy rules are what you intend. For instructions, see Creating a lifecycle policy preview in Amazon ECR.

  1. Open the Amazon ECR console at https://console.amazonaws.cn/ecr/repositories.

  2. From the navigation bar, choose the Region that contains the repository for which to create a lifecycle policy.

  3. In the navigation pane, under Private registry, choose Repositories.

  4. On the Private repositories page, select a repository and that use the Actions drop down to choose Lifecycle policies.

  5. On the lifecycle policy rules page for the repository, choose Create rule.

  6. Enter the following details for your lifecycle policy rule.

    1. For Rule priority, type a number for the rule priority. The rule priority determines in what order the lifecycle policy rules are applied.

    2. For Rule description, type a description for the lifecycle policy rule.

    3. For Image status, choose Tagged (wildcard matching), Tagged (prefix matching), Untagged, or Any.

    4. If you chose Tagged (wildcard matching) for Image status, then for Specify tags for wildcard matching, you can specify a list of image tags with a wildcard (*) on which to take action with your lifecycle policy. For example, if your images are tagged as prod, prod1, prod2, and so on, you would specify prod* to take action on all of them. If you specify multiple tags, only the images with all specified tags are selected.

      Important

      There is a maximum limit of four wildcards (*) per string. For example, ["*test*1*2*3", "test*1*2*3*"] is valid but ["test*1*2*3*4*5*6"] is invalid.

    5. If you chose Tagged (prefix matching)for Image status, then for Specify tags for prefix matching, you can specify a list of image tags on which to take action with your lifecycle policy.

    6. For Match criteria, choose either Since image pushed or Image count more than and then specify a value.

    7. Choose Save.

  7. Create additional lifecycle policy rules by repeating steps 5–7.

  1. Obtain the name of the repository for which to create the lifecycle policy.

    aws ecr describe-repositories
  2. Create a local file named policy.json with the contents of the lifecycle policy. For lifecycle policy examples, see Examples of lifecycle policies in Amazon ECR.

  3. 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