AWS CloudFormation templates and a bash init script

amazon-cloudformationamazon-web-serviceschef-solo

Up until now I've been launching EC2 instances manually, copying over a bash script that downloads my chef + chef artifacts, and calls chef-solo to provision an instance.

A former AWS engineer in the company had at one point created CF templates; which I barely know how to use.

Is there a way to integrate my bash script into the CF template, so that upon CF EC2 instance launch, my bash script is called and the instance is auto-provisioned?

Best Answer

Yes, you can use the UserData attribute of your AWS::EC2::Instance object.

http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-instance.html#cfn-ec2-instance-userdata

This attribute takes as input the base64-encoded version of your shell script. You can, however, provide the script inline with the help of a Cloudformation Base64 function:

"UserData" : { "Fn::Base64" : { "Fn::Join" : ["", [
    "#!/bin/bash -v\n",
    "# Script goes here\n"
]]}}