amazon-web-services - 有没有办法有条件地将过期策略添加到 cloudformation 中的 S3 存储桶

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

我在 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”) - 因此稍后很难在其他资源中引用正确的存储桶,因为您必须这样做确定创建了哪个存储桶。

最佳答案

我能够将 LifecycleConfiguration 设置为有条件的。它对我来说是这样的:

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

关于amazon-web-services - 有没有办法有条件地将过期策略添加到 cloudformation 中的 S3 存储桶,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62692857/

相关文章:

amazon-web-services - 将大型高清视频文件上传到 Amazon Web Services S3

python - 多线程 S3 下载不会终止

loops - ansible 遍历文件列表并检查文件是否存在,如果不存在则下载

kubernetes - 帮助升级问题:spec.template.spec.containers [0] .volumeMounts [2]。名称:找不到: “NAME”

mysql - (MySQL) 复制有 bin-log 吗?

amazon-web-services - 来自 S3 的亚马逊 emr spark 提交不起作用

ios - 包含 AWS 框架时无法运行应用程序

java - Snakeyaml - 如何对流程样式进行自定义控制

ios - 如何将 BFTask 与 Swift 3 结合使用?

mysql - 如何重启mysql服务器?