为了限制存储库中的图像数量,我想定义一个生命周期策略。由于所有堆栈都是使用CloudFormation定义的,因此我也想定义此策略。
例如,我的政策可能是“仅保留最近的8张图像,无论是否加了标签”。
答案 0 :(得分:3)
该解决方案非常简单,但是由于我找不到任何示例或类似问题(我知道ECR并不是主流),因此让我在此处发布我发现的简单解决方案,只需将策略插入为JSON进入CloudFormation定义:
MyRepository:
Type: AWS::ECR::Repository
Properties:
LifecyclePolicy:
LifecyclePolicyText: |
{
"rules": [
{
"rulePriority": 1,
"description": "Only keep 8 images",
"selection": {
"tagStatus": "any",
"countType": "imageCountMoreThan",
"countNumber": 8
},
"action": { "type": "expire" }
}]
}
这当然很简单,但这是我一直在寻找的起点
答案 1 :(得分:0)
您还可以定义对您的 PolicyText 的引用,然后在您的 parameters.json 中定义您的策略。
它看起来像这样:
template.yml
Parameters:
lifecyclePolicyText:
Description: Lifecycle policy content (JSON), the policy content the pre-fixes for the microservices and the kind of policy (CountMoreThan).
Type: String
repositoryName:
Description: ECR Repository Name to which we will apply the lifecycle policies.
Type: String
registryId:
Description: AWS account identification number (12 digits)
Type: String
Default: xxxxx
Resources:
Repository:
Type: AWS::ECR::Repository
Properties:
LifecyclePolicy:
LifecyclePolicyText: !Ref lifecyclePolicyText
RegistryId: !Ref registryId
RepositoryName: !Ref repositoryName
Outputs:
Arn:
Value: !GetAtt Repository.Arn
parameters.json
[
{
"ParameterKey": "lifecyclePolicyText",
"ParameterValue": "{'rules':[{'rulePriority':1,'description':'Only keep 8 images','selection':{'tagStatus':'any','countType':'imageCountMoreThan','countNumber':8},'action':{'type':'expire'}}]}"
},
{
"ParameterKey": "repositoryName",
"ParameterValue": "xxxx"
}
]