AWS CloudFormation
User Guide (API Version 2010-05-15)
AWS 服务或AWS文档中描述的功能,可能因地区/位置而异。请点击 Amazon AWS 入门,可查看中国地区的具体差异

AWS CloudFormation 模板代码段

嵌套堆栈

在模板中嵌套堆栈

此示例模板包含一个称为 myStack 的嵌套堆栈资源。当 AWS CloudFormation 从模板创建堆栈时,它会创建 myStack,其模板在 TemplateURL 属性中指定。 输出值 StackRef 返回 myStack 的堆栈 ID,值 OutputFromNestedStackmyStack 资源内返回输出值 BucketName。输出。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

使用输入参数在模板中嵌套堆栈

此示例模板包含指定输入参数的堆栈资源。当 AWS CloudFormation 从此模板创建堆栈时,它会将 Parameters 属性中声明的值对用作创建 myStackWithParams 堆栈所用模板的输入参数。在此示例中,已指定 InstanceTypeKeyName 参数。

有关更多信息,请参阅 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" : "t1.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: t1.micro KeyName: mykey

等待条件

将等候条件用于 Amazon EC2 实例

重要

对于 Amazon EC2 和 Auto Scaling 资源,我们建议您使用 CreationPolicy 属性而非等待条件。当实例创建过程成功完成后,将“CreationPolicy”属性添加到这些资源,并使用 cfn-signal 帮助程序脚本发送信号。

如果您无法使用创建策略,请查看下面的示例模板,该模板声明了带等待条件的 Amazon EC2 实例。等待条件 myWaitCondition 使用 myWaitConditionHandle 发出信号,使用 DependsOn 属性指定等待条件将在创建 Amazon EC2 实例资源后触发,并使用 Timeout 属性将等待条件的持续时间指定为 4500 秒。此外,发出等候条件信号的预签名 URL 将通过 Ec2Instance 资源的 UserData 属性传输至 Amazon EC2 实例,以使应用程序或脚本可在该 Amazon EC2 实例上运行,从而检索预签名 URL 并用其发出成功或失败信号至等候条件。请注意,您需要创建发出等候条件信号的应用程序或脚本。输出值 ApplicationData 中包含从等候条件信号中传回的数据。

有关更多信息,请参阅在模板中创建等待条件AWS::CloudFormation::WaitConditionAWS::CloudFormation::WaitConditionHandle

JSON

{ "AWSTemplateFormatVersion" : "2010-09-09", "Mappings" : { "RegionMap" : { "us-east-1" : { "AMI" : "ami-76f0061f" }, "us-west-1" : { "AMI" : "ami-655a0a20" }, "eu-west-1" : { "AMI" : "ami-7fd4e10b" }, "ap-northeast-1" : { "AMI" : "ami-8e08a38f" }, "ap-southeast-1" : { "AMI" : "ami-72621c20" } } }, "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-76f0061f us-west-1: AMI: ami-655a0a20 eu-west-1: AMI: ami-7fd4e10b ap-northeast-1: AMI: ami-8e08a38f ap-southeast-1: AMI: ami-72621c20 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.

使用 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"

本页内容: