【问题标题】:How to define an ECR Lifecycle Policy with CloudFormation如何使用 CloudFormation 定义 ECR 生命周期策略
【发布时间】:2019-07-06 02:03:32
【问题描述】:

为了限制存储库中的图像数量,我想定义一个生命周期策略。由于所有堆栈都是使用 CloudFormation 定义的,因此我也想定义此策略。

例如,我的政策可能是“只保留最近的 8 张图片,无论是否加标签”。

【问题讨论】:

    标签: amazon-cloudformation amazon-ecr


    【解决方案1】:

    解决方案非常简单,但由于我找不到任何示例或类似问题(我知道 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" }
              }]
            }
    

    当然这很简单,但这是我一直在寻找的起点

    【讨论】:

      【解决方案2】:

      您还可以定义对 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"
          }
        ]
         
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-12-24
        • 2021-08-16
        • 2022-11-10
        • 2023-01-02
        • 2021-11-25
        • 2021-06-29
        • 1970-01-01
        • 2022-10-14
        相关资源
        最近更新 更多