我在AWS网站上发现了一个很棒的文档,说明如何在第二个实例开始出现之前完成一个实例并进行配置。它位于https://aws.amazon.com/blogs/devops/tag/cloudformation/page/2/
该文档使用CreationPolicy属性和cfn-signal通知AWS Cloudformation Instance1已经出现。我试过了代码。它运行。但问题是Instance2首先出现而不是等待Instance1出现。这是代码,我在实例出现时附上了屏幕截图。
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "EC2 Head Node Instance ",
"Resources": {
"EC2Instance1": {
"Type": "AWS::EC2::Instance",
"CreationPolicy" : {
"ResourceSignal" : {
"Timeout": "PT10M",
"Count" : "1"
}
},
"Properties": {
"ImageId": "ami-3723c04f",
"UserData":{
"Fn::Base64":{
"Fn::Join": ["", [
"<script>\n",
"cfn-signal.exe -e 0 ",
" --stack ", {"Ref" : "AWS::StackName"},
" --resource EC2Instance1",
" --region ", {"Ref" : "AWS::Region"}, "\n" ,
"</script>\n"
]
]
}
},
"Tags" : [
{"Key" : "Name", "Value" : "QRM Head Node"}
]
}
},
"EC2Instance2": {
"Type": "AWS::EC2::Instance",
"Properties": {
"ImageId": "ami-3723c04f",
"Tags" : [{"Key" : "Dependency", "Value" : "EC2Instance1"}
]
}
}
}
答案 0 :(得分:2)
您必须指定DependsOn属性。
"EC2Instance2": {
"DependsOn": "EC2Instance1",
"Type": "AWS::EC2::Instance",
"Properties": {
"ImageId": "ami-3723c04f",
"Tags" : [{"Key" : "Dependency", "Value" : "EC2Instance1"}
]
}
}
以上代码段应解决您的问题。
Edit-1 :cfn-signal + DependsOn
创建一个cfn信号,以便在完成Ec2Instance1上的安装后发送信号。请参阅cfn-signal documentation。
现在,创建一个AWS::CloudFormation::WaitCondition
并在Ec2Instance2中引用它。以下是有关如何在模板cfn-wait-condition中集成WaitConditions的文档。