Amazon web services AWS云用户数据传递
如何在AWS cloudformation中将参数输入数据传递给userdata。 示例:我有一个参数EnvType,在运行CFT时将“qa”作为输入传递给该参数。我希望读取这个参数值“qa”并将其传递给userdata,以便将其写入实例磁盘Amazon web services AWS云用户数据传递,amazon-web-services,amazon-cloudformation,Amazon Web Services,Amazon Cloudformation,如何在AWS cloudformation中将参数输入数据传递给userdata。 示例:我有一个参数EnvType,在运行CFT时将“qa”作为输入传递给该参数。我希望读取这个参数值“qa”并将其传递给userdata,以便将其写入实例磁盘 Parameters: { "EnvType": { "Description": "Environment type", "Type": "String", "AllowedValues": [
Parameters: {
"EnvType": {
"Description": "Environment type",
"Type": "String",
"AllowedValues": [
"prod",
"qa"
]
}
我尝试在用户数据中使用此选项:
export STACK_TYPE='",
{
"Ref": "EnvType"
},
"'\n",
"echo \"$STACK_TYPE\" > stacktypes\n
我想在实例中将这个EnvType输入附加到名为stacktypes的文件中 您必须使用
Fn::Join
将用户数据字符串与来自CloudFormation的其他内部函数(如Ref
)的结果实际“连接”。下面是一个如何完成的示例:
...
"MyInstance": {
"Type": "AWS::EC2::Instance",
"Properties": {
"UserData": {
"Fn::Base64": {
"Fn::Join": [
"",
[
"#cloud-config\n\nrepo_upgrade: all\n\n\nwrite_files:\n- path: /root/init.sh\n owner: root:root\n permissions: '0755'\n content: |\n #!/bin/bash\n\n EC2_INSTANCE_ID=`wget -q -O - http://169.254.169.254/latest/meta-data/instance-id`\n aws cloudformation signal-resource --stack-name ",
{
"Ref": "AWS::StackName"
},
" --status SUCCESS --logical-resource-id AutoScalingGroup --unique-id $EC2_INSTANCE_ID --region ",
{
"Ref": "AWS::Region"
}
]
]
}
}
...
}
}
...
这可能是一项乏味的任务,我们在内部开发了一个工具来处理用户数据的生成,但我知道有一些开源工具可以提供帮助(例如:)。您可以将堆栈参数传递/附加到实例中的文件中。 如果有这样的参数
Parameters: {
"EnvType": {
...
}
您可以尝试将下面的UserData添加到实例属性中
"Properties": {
...
"UserData": {
"Fn::Base64": {
"Fn::Join": [
"",
[
"#!/bin/bash -xe\",
"echo ",
{
"Ref": "EnvType"
},
" >> /path/yourfile\n"
]
]
}
}
}
这将把EnvType
参数附加到实例中的文件/path/yourfile
中