CloudFormation 模板已安装但没有指示脚本何时完成

CloudFormation template installs but no indication of when script finishes

我有以下 CloudFormation 脚本,可以正确设置所有基础架构。当我从 CloudFormation 收到 CREATE_COMPLETE 消息时,EC2 可通过 SSH 访问,但应用程序尚未安装。如果我等 3 分钟,它们就会出现。

有没有办法让 CloudFormation 等待所有元数据(git 和 htop)安装完成?

脚本如下:

{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "My first CloudFormation template",

"Resources": {
    "WebServerInstance": {
        "Type": "AWS::EC2::Instance",
        "Properties": {
            "ImageId": "ami-abeb9e91",
            "InstanceType": "t2.micro",
            "KeyName": "test-key-pair",
            "SecurityGroups" : [ {"Ref" : "WebServerSecurityGroup"} ],
            "UserData": {
                "Fn::Base64": { "Fn::Join":["", [
                    "#!/bin/bash -ex\n",
                    "apt-get update\n",
                    "apt-get -y install python-setuptools\n",
                    "mkdir aws-cfn-bootstrap-latest\n",
                    "curl https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gz | tar xz -C aws-cfn-bootstrap-latest --strip-components 1\n",
                    "easy_install aws-cfn-bootstrap-latest\n",
                    "sudo /usr/local/bin/cfn-init --stack ", { "Ref":"AWS::StackName" }, " --resource WebServerInstance", " --region ", { "Ref": "AWS::Region" }, "\n",
                    "\n",
                    "/usr/local/bin/cfn-signal --exit-code $?"
                ]]}
            }
        },
        "Metadata": {
            "AWS::CloudFormation::Init": {
                "config": {
                    "packages": {
                        "apt": {
                            "htop": [],
                            "git": []
                        }
                    }
                }
            }
        }
    }
}

}

CreationPolicy Attribute就是你所需要的。