Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing userdata file to AWS Cloudformation stack

I have a shell script(userdata file) and wondering is there a CLI command parameter that allows user to launch Cloudformation stack with userdata file?

like image 520
tset Avatar asked Jul 05 '16 04:07

tset


Video Answer


2 Answers

Inside your template, use a CloudFormation parameter for the instance userdata:

{
  "Parameters": {
    "UserData": {
      "Type": "String"
    }
  },
  "Resources": {
    "Instance": {
      "Type" : "AWS::EC2::Instance",
      "Properties" : {
        "UserData" : { "Ref" : "UserData" },
        ...
      }
    },
    ...
  }
}

Assuming you're using a Unix-like command line environment, create your stack like this:

aws cloudformation create-stack --stack-name myStack \
    --template-body file://myStack.json \
    --parameters ParameterKey=UserData,ParameterValue=$(base64 -w0 userdata.sh)
like image 130
ataylor Avatar answered Oct 13 '22 06:10

ataylor


Your user-data must exist in the CloudFormation template when you create the stack. You can write a script to read in your user-data from the file and insert it into the CloudFormation stack prior to creating the stack. Note that you may need to make formatting changes to the userdata (see http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-instance.html#cfn-ec2-instance-userdata).

like image 1
jzonthemtn Avatar answered Oct 13 '22 05:10

jzonthemtn