Fn::Join and Fn::Split
What you'll learn: How to use CloudFormation's Fn::Join to concatenate strings with delimiters and Fn::Split to break strings apart into lists.
The Core Idea
CloudFormation's Fn::Join and Fn::Split are complementary intrinsic functions for string manipulation. Fn::Join takes a list of strings and glues them together with a delimiter you specify (like adding commas between words). Fn::Split does the opposite—it breaks a single string into a list by splitting on a delimiter (like separating a CSV row into individual values).
Think of Fn::Join like threading beads on a string with knots between each bead. Fn::Split is cutting that string at each knot to get your beads back.
Fn::Join: Building Strings
Fn::Join takes two arguments: a delimiter and a list of values to join.
!Join
- ','
- - 'apple'
- 'banana'
- 'cherry'
# Result: "apple,banana,cherry"
You can combine this with other functions like Ref to build resource names, ARNs, or URLs dynamically:
!Join
- ''
- - 'arn:aws:s3:::'
- !Ref BucketName
- '/logs'
Fn::Split: Breaking Strings Apart
Fn::Split takes a delimiter and a string to split, returning a list:
!Split
- ','
- 'us-east-1,us-west-2,eu-west-1'
# Result: ['us-east-1', 'us-west-2', 'eu-west-1']
This is useful when you receive delimited data (like from parameters) and need individual values.
Practical Use Case
You can split a comma-separated parameter into a list, manipulate it, then join it back:
Parameters:
RegionList:
Type: String
Default: "us-east-1,us-west-2"
Key Takeaway: Use Fn::Join to concatenate strings with delimiters and Fn::Split to parse delimited strings into lists—they're inverse operations for flexible string manipulation in CloudFormation templates.