When creating an AWS EC2 instance, there is a User Data section where you can include scripts that will be run immediately upon instance creation. I was attempting to use AWS CloudFormation to define the infrastructure of my current project and ran into the issue of how to include the User Data section in the CloudFormation JSON definition.
After several false starts, I was able to come up with this:
The key was realizing that
Fn::Join, since it simply concatenates each array item, needed each item to end with
\n before the
Base64 encoding. Without the
\n, each line of the script is pushed right up next to it’s predecessor and you get
#!/bin/bash -exyum update -yyum -y install ruby... etc.
And here is the entire Auto Scaling Launch Configuration to put the User Data section in context. Each instance of the Auto Scaling Group will have the CodeDeploy service and Git installed on startup.