2
votes

I'm building a nested Cloudformation stack with a subtemplate used multiple times.

I would like the different resources (S3 buckets, target groups, etc.) to use the AWS::StackName as part of their name.

!Sub ${AWS::StackName}-s3bucket

The nested stack names usually include an AWS-generated string that is in uppercase like:

foobar-vpcstack-YN4842UYLUFL

However, some resources only allow a name that is all lowercase. Is there a way to ensure that the nested stack names are all lowercase?

Or is there a better way to handle the naming of the nested stacks and its resources?

1

1 Answers

0
votes

I use the following rules to set the BucketName for s3 bucket in the nested Cloudformation stacks. It makes sure the bucket name all lowercase, and unique.

"BucketName": {"Fn::Join": ["-", [
    "foo", {"Fn::Select": ["2", {"Fn::Split": ["/", {"Ref": "AWS::StackId"}]}]}
]]}

foo: is the prefix for the bucket name.

Fn::Select ...: The function gets the last part of the StackId.

Given the sample StackId: arn:aws:cloudformation:ap-northeast-1:888888:stack/ParentStack-ChildStack-11MKHI1KPKH7O/1f29c920-82d1-11eb-85b7-0ebec92bca7d

The code above returns the following string for BucketName: foo-1f29c920-82d1-11eb-85b7-0ebec92bca7d