【问题标题】:Is there a way to conditionally add expiration policies to an S3 bucket in cloudformation有没有办法有条件地将过期策略添加到 cloudformation 中的 S3 存储桶
【发布时间】:2020-10-22 20:19:39
【问题描述】:

我在 CloufFormation 模板中定义了一个 S3 存储桶:

Resources:
  Bucket:
    Type: AWS::S3::Bucket
    Properties:
      AccessControl: Private
      BucketName: !Ref BucketName

我想选择性地向存储桶添加保留政策,因此:

Resources:
  Bucket:
    Type: AWS::S3::Bucket
    Properties:
      AccessControl: Private
      BucketName: !Ref BucketName
      LifecycleConfiguration:
        Rules:
          - ExpirationInDays: !Ref RetentionDays

我不需要在 CF 模板的所有部署中使用 LifecycleConfiguration/ExpirationInDays,因为某些存储桶会无限期地保留其对象。我有looked at the documentation,似乎没有无限期保留/永不过期的值。 我曾想过拥有两个存储桶——一个带有 LifecycleConfiguration,一个没有,然后我可以使用一个条件(例如,如果 RetentionDays 参数 = -1)来确定创建哪个存储桶:

Conditions:
  HasNoRetention: !Equals [!Ref RetentionDays, -1]
  HasRetention: !Not [Condition: HasNoRetention]
Resources:
  Bucket:
    Type: AWS::S3::Bucket
    Condition: HasNoRetention
    Properties:
      AccessControl: Private
      BucketName: !Ref BucketName
  BucketWithRetention:
    Type: AWS::S3::Bucket
    Condition: HasRetention
    Properties:
      AccessControl: Private
      BucketName: !Ref BucketName
      LifecycleConfiguration:
        Rules:
          - ExpirationInDays: !Ref RetentionDays

这里的问题是存储桶在 YAML 定义中具有不同的名称(“Bucket”和“BucketWithRetention”) - 所以以后很难在其他资源中引用正确的存储桶,因为您必须这样做确定创建了哪个存储桶。

【问题讨论】:

    标签: amazon-web-services amazon-s3 yaml amazon-cloudformation


    【解决方案1】:

    我能够使 LifecycleConfiguration 有条件。它对我有用:

     LifecycleConfiguration:
       Fn::If:
         - HasRetention
         - Rules:
             - ExpirationInDays: !Ref RetentionDays
               Status: Enabled
         - Ref: AWS::NoValue
    

    【讨论】:

      【解决方案2】:

      我认为以下应该可以使用Fn::If

      LifecycleConfiguration:
        Rules:
          !If 
            - RetentionDays
            - - ExpirationInDays: !Ref RetentionDays
            - !Ref 'No::Value'
      

      【讨论】:

      猜你喜欢
      • 2021-12-13
      • 1970-01-01
      • 2022-11-08
      • 2020-11-04
      • 2017-10-14
      • 1970-01-01
      • 2021-09-17
      • 2022-01-24
      • 1970-01-01
      相关资源
      最近更新 更多