我正在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定义中具有不同的名称(“存储桶”和“ BucketWithRetention”)-因此稍后将很难!在其他资源中引用正确的存储桶确定创建哪个存储桶。
答案 0 :(得分:1)
我认为以下使用Fn::If可以工作:
LifecycleConfiguration:
Rules:
!If
- RetentionDays
- - ExpirationInDays: !Ref RetentionDays
- !Ref 'No::Value'
答案 1 :(得分:1)
我能够使 LifecycleConfiguration 有条件。它对我来说是这样的:
LifecycleConfiguration:
Fn::If:
- HasRetention
- Rules:
- ExpirationInDays: !Ref RetentionDays
Status: Enabled
- Ref: AWS::NoValue