How to use variables in bucket names AWS Cloudformation

Viewed 1843

I am trying to create a Cloudformation template that creates an S3 bucket. I want to configure it so that the bucket is named using one of the parameters passed into the template. I am able to substitute the param in other places in the template, but the bucket creation fails when I try and sub it in here.

Resources:
  S3Bucket:
    Type: 'AWS::S3::Bucket'
    Properties:
      BucketName: !Sub '${STRING_PARAM}-example-bucketname'

This gives a Bad Request (Service: Amazon S3; Status Code: 400; Error Code: 400 Bad Request when the cloudformation stack is created. I've tried hard coding in the name which has worked, so I'm pretty sure it's a valid S3 bucket name.

Per S3 Creation CloudFormation results in 400 Bad Request referring (! Ref instead of ! Sub) to the param/resource directly works, but I'm thinking I should be able to sub the param in here like everywhere else.

1 Answers

Your value for BucketName is fine. This is the correct syntax for using !Sub and should work.

The problem is that the names of parameters must be alpha numeric so STRING_PARAM is invalid since it has an underscore character in it.

Update your parameter name to be something like StringParam and it should get you past the Parameter name STRING_PARAM is non alphanumeric. error message.

Related