# LifecycleRule

An ECR life cycle rule.

Used as argument in: [Repository.addLifecycleRule](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_ecr.Repository.html#addlifecyclerule)

- Source: [`aws-ecr/lib/lifecycle.ts`](https://github.com/aws/aws-cdk/blob/v2.278.0/packages/aws-cdk-lib/aws-ecr/lib/lifecycle.ts#L6)
- Type last changed: v2.116.0 · 22 Dec 2023

## Example

TypeScript:

```ts
declare const repository: ecr.Repository;
repository.addLifecycleRule({ tagPrefixList: ['prod'], maxImageCount: 9999 });
repository.addLifecycleRule({ maxImageAge: Duration.days(30) });
```

Python:

```python
# repository: ecr.Repository

repository.add_lifecycle_rule(tag_prefix_list=["prod"], max_image_count=9999)
repository.add_lifecycle_rule(max_image_age=Duration.days(30))
```

Java:

```java
Repository repository;

repository.addLifecycleRule(LifecycleRule.builder().tagPrefixList(List.of("prod")).maxImageCount(9999).build());
repository.addLifecycleRule(LifecycleRule.builder().maxImageAge(Duration.days(30)).build());
```

C# / .NET:

```csharp
// Install: dotnet add package Amazon.CDK.Lib
Repository repository;

repository.AddLifecycleRule(new LifecycleRule { TagPrefixList = new [] { "prod" }, MaxImageCount = 9999 });
repository.AddLifecycleRule(new LifecycleRule { MaxImageAge = Duration.Days(30) });
```

Golang:

```go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
var repository Repository

repository.AddLifecycleRule(&LifecycleRule{
	TagPrefixList: []*string{
		jsii.String("prod"),
	},
	MaxImageCount: jsii.Number(9999),
})
repository.AddLifecycleRule(&LifecycleRule{
	MaxImageAge: awscdk.Duration_Days(jsii.Number(30)),
})
```

## Properties

- `description`: string — Describes the purpose of the rule.

  **Default:** No description

- `maxImageAge`: [Duration](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.Duration.html) — The maximum age of images to retain. The value must represent a number of days.

  Specify exactly one of maxImageCount and maxImageAge.

- `maxImageCount`: number — The maximum number of images to retain.

  Specify exactly one of maxImageCount and maxImageAge.

- `rulePriority`: number — Controls the order in which rules are evaluated (low to high).

  **Default:** Automatically assigned

  All rules must have a unique priority, where lower numbers have
  higher precedence. The first rule that matches is applied to an image.

  There can only be one rule with a tagStatus of Any, and it must have
  the highest rulePriority.

  All rules without a specified priority will have incrementing priorities
  automatically assigned to them, higher than any rules that DO have priorities.

- `tagPatternList`: string[] — Select images that have ALL the given patterns in their tag.

  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.

  Both tagPrefixList and tagPatternList cannot be specified
  together in a rule.

  Only if tagStatus == TagStatus.Tagged

- `tagPrefixList`: string[] — Select images that have ALL the given prefixes in their tag.

  Both tagPrefixList and tagPatternList cannot be specified
  together in a rule.

  Only if tagStatus == TagStatus.Tagged

- `tagStatus`: [TagStatus](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_ecr.TagStatus.html) — Select images based on tags.

  **Default:** TagStatus.Tagged if tagPrefixList or tagPatternList is given, TagStatus.Any otherwise

  Only one rule is allowed to select untagged images, and it must
  have the highest rulePriority.
