Cloudformation - Give a property to an S3 Bucket Resource if it meets a condition

Viewed 275

I basically want to check if the environment is env or dev, and if it is then enable versioning. If not, do nothing. I used the example for the AWS documentation:

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-conditions.html

I am getting an error though in my stack

Value of property VersioningConfiguration must be an object

My Condition Cloudformation

Conditions:
    EnvCheck: !Or [!Equals [!Ref environment, test], !Equals [!Ref environment, dev]]

Bucket Cloudformation

Resources:
  SecurityS3Bucket:
    Type: AWS::S3::Bucket
    Properties:
        ...
        VersioningConfiguration:
            !If
                - EnvCheck
                - 
                  Status: Enabled
                - !Ref "AWS::NoValue"
1 Answers

It's actually pretty straightforward. As soon as you specify the VersioningConfiguration property for the bucket, you have to specify the value for it, which has to be an object with a property Status according to the documentation.

So either you'll have to pass in the other value for Status (i.e., Suspended) or you'll have to place your !If statement differently so the VersioningConfiguration property key is not set when you don't want versioning enabled on the bucket.

Related