CloudFormation cfn-init信号用户数据脚本成功/失败

时间:2016-05-22 10:22:37

标签: amazon-web-services amazon-ec2 amazon-cloudformation

我写了一个powershell EC2 userdata脚本。我想知道如何使用cfn-init.exe将成功/失败告知CloudFormation?请提供示例和语法,因为我是CloudFormation的新手。

1 个答案:

答案 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"
      }
    }
  }
}