Amazon CloudFormation 模板代码段
嵌套堆栈
在模板中嵌套堆栈
此示例模板包含一个称为 myStack
的嵌套堆栈资源。在 Amazon CloudFormation 通过模板创建堆栈时,它创建 myStack
,其模板是在 TemplateURL
属性中指定的。输出值 StackRef
返回 myStack
的堆栈 ID,值 OutputFromNestedStack
从 myStack
资源中返回输出值 BucketName
。Outputs.nestedstackoutputname
是预留格式,用于指定嵌套堆栈的输出值,可以在包含模板的任意位置中使用该格式。
有关更多信息,请参阅 AWS::CloudFormation::Stack。
JSON
{ "AWSTemplateFormatVersion" : "2010-09-09", "Resources" : { "myStack" : { "Type" : "AWS::CloudFormation::Stack", "Properties" : { "TemplateURL" : "https://s3.amazonaws.com/cloudformation-templates-us-east-1/S3_Bucket.template", "TimeoutInMinutes" : "60" } } }, "Outputs": { "StackRef": {"Value": { "Ref" : "myStack"}}, "OutputFromNestedStack" : { "Value" : { "Fn::GetAtt" : [ "myStack", "Outputs.BucketName" ] } } } }
YAML
AWSTemplateFormatVersion: '2010-09-09' Resources: myStack: Type: AWS::CloudFormation::Stack Properties: TemplateURL: https://s3.amazonaws.com/cloudformation-templates-us-east-1/S3_Bucket.template TimeoutInMinutes: '60' Outputs: StackRef: Value: !Ref myStack OutputFromNestedStack: Value: !GetAtt myStack.Outputs.BucketName
使用输入参数在模板中嵌套堆栈
此示例模板包含指定输入参数的堆栈资源。在 Amazon CloudFormation 通过该模板创建堆栈时,它将 Parameters 属性中声明的值对作为用于创建 myStackWithParams
堆栈的模板的输入参数。在此示例中,已指定 InstanceType
和 KeyName
参数。
有关更多信息,请参阅 AWS::CloudFormation::Stack。
JSON
{ "AWSTemplateFormatVersion" : "2010-09-09", "Resources" : { "myStackWithParams" : { "Type" : "AWS::CloudFormation::Stack", "Properties" : { "TemplateURL" : "https://s3.amazonaws.com/cloudformation-templates-us-east-1/EC2ChooseAMI.template", "Parameters" : { "InstanceType" : "t2.micro", "KeyName" : "mykey" } } } } }
YAML
AWSTemplateFormatVersion: '2010-09-09' Resources: myStackWithParams: Type: AWS::CloudFormation::Stack Properties: TemplateURL: https://s3.amazonaws.com/cloudformation-templates-us-east-1/EC2ChooseAMI.template Parameters: InstanceType: t2.micro KeyName: mykey
等待条件
将等待条件用于 Amazon EC2 实例
重要
对于 Amazon EC2 和 Auto Scaling 资源,我们建议您使用 CreationPolicy 属性而非等待条件。当实例创建过程成功完成后,将“CreationPolicy”属性添加到这些资源,并使用 cfn-signal 帮助程序脚本发送信号。
如果您无法使用创建策略,请查看以下示例模板,该模板声明了带等待条件的 Amazon EC2 实例。等待条件 myWaitCondition 使用 myWaitConditionHandle 发出信号,使用 DependsOn 属性指定等待条件将在创建 Amazon EC2 实例资源后触发,并使用 Timeout 属性将等待条件的持续时间指定为 4 500 秒。此外,向等待条件发出信号的预签名 URL 将通过 Ec2Instance 资源的 UserData 属性传输至 Amazon EC2 实例,以使应用程序或脚本可在该 Amazon EC2 实例上运行,从而检索预签名 URL 并用其发出成功或失败信号至等待条件。您需要使用 cfn-signal,或创建向等待条件发出信号的应用程序或脚本。输出值 ApplicationData 中包含从等候条件信号中传回的数据。
有关更多信息,请参阅 在模板中创建等待条件、AWS::CloudFormation::WaitCondition、AWS::CloudFormation::WaitConditionHandle 和 cfn-signal。
JSON
{ "AWSTemplateFormatVersion" : "2010-09-09", "Mappings" : { "RegionMap" : { "us-east-1" : { "AMI" : "ami-0ff8a91507f77f867" }, "us-west-1" : { "AMI" : "ami-0bdb828fd58c52235" }, "eu-west-1" : { "AMI" : "ami-047bb4163c506cd98" }, "ap-northeast-1" : { "AMI" : "ami-06cd52961ce9f0d85" }, "ap-southeast-1" : { "AMI" : "ami-08569b978cc4dfa10" } } }, "Resources" : { "Ec2Instance" : { "Type" : "AWS::EC2::Instance", "Properties" : { "UserData" : { "Fn::Base64" : {"Ref" : "myWaitHandle"}}, "ImageId" : { "Fn::FindInMap" : [ "RegionMap", { "Ref" : "AWS::Region" }, "AMI" ]} } }, "myWaitHandle" : { "Type" : "AWS::CloudFormation::WaitConditionHandle", "Properties" : { } }, "myWaitCondition" : { "Type" : "AWS::CloudFormation::WaitCondition", "DependsOn" : "Ec2Instance", "Properties" : { "Handle" : { "Ref" : "myWaitHandle" }, "Timeout" : "4500" } } }, "Outputs" : { "ApplicationData" : { "Value" : { "Fn::GetAtt" : [ "myWaitCondition", "Data" ]}, "Description" : "The data passed back as part of signalling the WaitCondition." } } }
YAML
AWSTemplateFormatVersion: '2010-09-09' Mappings: RegionMap: us-east-1: AMI: ami-0ff8a91507f77f867 us-west-1: AMI: ami-0bdb828fd58c52235 eu-west-1: AMI: ami-047bb4163c506cd98 ap-northeast-1: AMI: ami-06cd52961ce9f0d85 ap-southeast-1: AMI: ami-08569b978cc4dfa10 Resources: Ec2Instance: Type: AWS::EC2::Instance Properties: UserData: Fn::Base64: !Ref myWaitHandle ImageId: Fn::FindInMap: - RegionMap - Ref: AWS::Region - AMI myWaitHandle: Type: AWS::CloudFormation::WaitConditionHandle Properties: {} myWaitCondition: Type: AWS::CloudFormation::WaitCondition DependsOn: Ec2Instance Properties: Handle: !Ref myWaitHandle Timeout: '4500' Outputs: ApplicationData: Value: !GetAtt myWaitCondition.Data Description: The data passed back as part of signalling the WaitCondition.
使用 cfn-signal 帮助程序脚本向等待条件发出信号
该示例显示将成功信号发送到等待条件的 cfn-signal 命令行。您需要在 EC2 实例的 UserData
属性中定义命令行。
JSON
"UserData": { "Fn::Base64": { "Fn::Join": [ "", [ "#!/bin/bash -xe\n", "/opt/aws/bin/cfn-signal --exit-code 0 '", { "Ref": "myWaitHandle" }, "'\n" ] ] } }
YAML
UserData: 'Fn::Base64': 'Fn::Join': - '' - - | #!/bin/bash -xe - /opt/aws/bin/cfn-signal --exit-code 0 ' - Ref: myWaitHandle - | '
使用 Curl 向等待条件发出信号
此示例显示将成功信号发送到等待条件的 Curl 命令行。
curl -T /tmp/a "https://cloudformation-waitcondition-test.s3.amazonaws.com/arn%3Aaws%3Acloudformation%3Aus-east-1%3A034017226601%3Astack%2Fstack-gosar-20110427004224-test-stack-with-WaitCondition--VEYW%2Fe498ce60-70a1-11e0-81a7-5081d0136786%2FmyWaitConditionHandle?Expires=1303976584&AWSAccessKeyId=AKIAIOSFODNN7EXAMPLE&Signature=ik1twT6hpS4cgNAw7wyOoRejVoo%3D"
其中文件 /tmp/a 包含以下 JSON 结构:
{ "Status" : "SUCCESS", "Reason" : "Configuration Complete", "UniqueId" : "ID1234", "Data" : "Application has completed configuration." }
此示例显示发送同一成功信号的 Curl 命令行,将 JSON 发送为命令行参数的情况除外。
curl -X PUT -H 'Content-Type:' --data-binary '{"Status" : "SUCCESS","Reason" : "Configuration Complete","UniqueId" : "ID1234","Data" : "Application has completed configuration."}' "https://cloudformation-waitcondition-test.s3.amazonaws.com/arn%3Aaws%3Acloudformation%3Aus-east-1%3A034017226601%3Astack%2Fstack-gosar-20110427004224-test-stack-with-WaitCondition--VEYW%2Fe498ce60-70a1-11e0-81a7-5081d0136786%2FmyWaitConditionHandle?Expires=1303976584&AWSAccessKeyId=AKIAIOSFODNN7EXAMPLE&Signature=ik1twT6hpS4cgNAw7wyOoRejVoo%3D"