为了限制存储库中的图像数量,我想定义一个生命周期策略。由于所有堆栈都是使用 CloudFormation 定义的,因此我也想定义此策略。
例如,我的策略可能是“只保留最近的 8 张图像,无论是否标记”。
为了限制存储库中的图像数量,我想定义一个生命周期策略。由于所有堆栈都是使用 CloudFormation 定义的,因此我也想定义此策略。
例如,我的策略可能是“只保留最近的 8 张图像,无论是否标记”。
该解决方案非常简单,但由于我找不到任何示例或类似问题(我知道 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" }
}]
}
当然这很简单,但这是我一直在寻找的起点
您还可以定义对 PolicyText 的引用,然后在您的 parameters.json 中对您的策略进行字符串化。
它看起来像这样:
模板.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
参数.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"
}
]