Passing userdata file to AWS Cloudformation stack

怎甘沉沦 提交于 2020-01-01 10:05:13

问题


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?


回答1:


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)



回答2:


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).



来源:https://stackoverflow.com/questions/38195137/passing-userdata-file-to-aws-cloudformation-stack

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!