我写了一个powershell EC2 userdata脚本。我想知道如何使用cfn-init.exe将成功/失败告知CloudFormation?请提供示例和语法,因为我是CloudFormation的新手。
答案 0 :(得分:2)
您需要使用cfn-signal命令将信号传递给cloudformation。 请查看http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-signal.html
上的文档我添加了使用cfn-signal传递信号的cloudformation片段。
脚本确保,在将故障发回到云形成信号之前,cloudformation将在创建的实例上等待最多300秒。
{
"AWSTemplateFormatVersion": "2010-09-09",
"Resources": {
"EC2Instance": {
"Type": "AWS::EC2::Instance",
"Properties": {
"ImageId": "<AMI>",
"InstanceType": "<Instance Type>",
"KeyName": "<Key_pair>",
"Monitoring": "false",
"UserData": {
"Fn::Base64": {
"Fn::Join": [
"",
[
"#!/bin/bash -e\n",
"yum update -y aws-cfn-bootstrap\n",
"/opt/aws/bin/cfn-signal -e 0 -r \"Failed to create Instance\" ",
{
"Ref": "WaitHandle"
},
"'\n"
]
]
}
}
}
},
"WaitHandle": {
"Type": "AWS::CloudFormation::WaitConditionHandle"
},
"WaitCondition": {
"Type": "AWS::CloudFormation::WaitCondition",
"DependsOn": "EC2Instance",
"Properties": {
"Handle": {
"Ref": "WaitHandle"
},
"Timeout": "300"
}
}
}
}