如何将参数输入数据传递给 AWS cloudformation 中的用户数据。
示例:我有一个参数 EnvType,在运行 CFT 时我将传递“qa”作为该参数的输入。我希望读取此参数值“qa”并将其传递给用户数据,以便我可以将其写入实例磁盘。
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"
}
]
]
}
}
...
}
}
...
这可能是一项乏味的任务,我们在内部开发了一个工具来处理 UserData 的生成,但我知道有一些开源工具可以提供帮助(例如:https://github.com/cloudtools/troposphere https://github.com/cloudtools/troposphere).
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)