Amazon cloudformation 如何使用CloudFormation定义ECR生命周期策略

Amazon cloudformation 如何使用CloudFormation定义ECR生命周期策略,amazon-cloudformation,amazon-ecr,Amazon Cloudformation,Amazon Ecr,为了限制存储库中图像的数量,我想定义一个生命周期策略。由于所有堆栈都是用CloudFormation定义的,所以我也要定义这个策略 例如,我的策略可以是“仅保留最近的8张图像,无论是否标记” 解决方案非常简单,但由于我找不到任何示例或类似问题(我知道ECR不是主流),让我在这里发布我找到的简单解决方案,它只需要将策略作为JSON插入到CloudFormation定义中: MyRepository: Type: AWS::ECR::Repository Properties: Li

为了限制存储库中图像的数量,我想定义一个生命周期策略。由于所有堆栈都是用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的引用,以及稍后对您的参数的引用。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"
    }
  ]