Knowledge Center Monthly Newsletter - June 2025
Stay up to date with the latest from the Knowledge Center. See all new Knowledge Center articles published in the last month, and re:Post's top contributors.
如何在 AWS CloudFormation 中同一父堆栈内的嵌套堆栈之间传递值?
我想在 AWS CloudFormation 中同一父堆栈中的两个嵌套堆栈之间传递或共享值。
解决方法
以下解析使用了 AWS::CloudFormation::Stack 资源 NestedStackA 和 NestedStackB,它们是名为 RootStack 的同一个父堆栈的一部分。您要将一个值从 NestedStackA 传递给 NestedStackB。NestedStackA 创建了 S3 存储桶资源,而 NestedStackB 创建了附加到 S3 存储桶的 S3 存储桶策略。
完成以下步骤:
-
在 NestedStackA 的 CloudFormation 模板的 Outputs(输出)部分中,添加您要分享的值。
JSON:{ "Resources": { "S3Bucket": { "Type": "AWS::S3::Bucket", "DeletionPolicy": "Retain", "Properties": { "BucketName": "DOC-EXAMPLE-BUCKET" } } }, "Outputs": { "BucketNameOutput": { "Value": { "Ref" : "S3Bucket" }, "Description": "You can refer to any resource from the template." } } }
YAML:
Resources: S3Bucket: Type: 'AWS::S3::Bucket' DeletionPolicy: Retain Properties: BucketName: DOC-EXAMPLE-BUCKET Outputs: BucketNameOutput: Value: !Ref S3Bucket Description: You can refer to any resource from the template.
**注意:**在上述模板中,将 DOC-EXAMPLE-BUCKET 替换为您的存储桶名称。上述模板的 Outputs(输出)部分返回来自 !Ref 的存储桶名称。
-
在 NestedStackB 的 CloudFormation 模板的 Parameters(参数)部分中,添加一个参数,使用 NestedStackA 输出中的 S3 存储桶名称。
JSON:{ "Parameters": { "BucketNameValueParameter": { "Type": "String", "Description": "The shared bucket name value from nestedStackA that will be passed to this parameter from the parent stack." } }, "Resources": { "SampleBucketPolicy": { "Type": "AWS::S3::BucketPolicy", "Properties": { "Bucket": { "Ref": "BucketNameValueParameter" }, "PolicyDocument": { "Version": "2012-10-17", "Statement": [ { "Action": [ "s3:GetObject" ], "Effect": "Allow", "Resource": { "Fn::Join": [ "", [ "arn:aws:s3:::", { "Ref": "DOC-EXAMPLE-BUCKET" }, "/*" ] ] }, "Principal": "*", "Condition": { "StringLike": { "aws:Referer": [ "http://www.example.com/*", "http://example.net/*" ] } } } ] } } } } }
YAML:
Parameters: BucketNameValueParameter: Type: String Description: >- The shared bucket name value from nestedStackA that will be passed to this parameter from the parent stack. Resources: SampleBucketPolicy: Type: 'AWS::S3::BucketPolicy' Properties: Bucket: !Ref BucketNameValueParameter PolicyDocument: Version: 2012-10-17 Statement: - Action: - 's3:GetObject' Effect: Allow Resource: !Join - '' - - 'arn:aws:s3:::' - !Ref DOC-EXAMPLE-BUCKET - /* Principal: '*' Condition: StringLike: 'aws:Referer': - 'http://www.example.com/*' - 'http://example.net/*'
-
要在 NestedStackA 和 NestedStackB 之间传递值,请将 RootStack 配置为使用 NestedStackB 的 Parameter(参数)部分中的 Fn::GetAtt 函数。使用 NestedStackA 的逻辑 ID 以及采用 Outputs.NestedStackOutputName 格式的存储桶名称输出值。
JSON:{ "AWSTemplateFormatVersion" : "2010-09-09", "Resources" : { "NestedStackA" : { "Type" : "AWS::CloudFormation::Stack", "Properties" : { "TemplateURL" : "https://s3.amazonaws.com/<pathway to .template for NestedStack A>" } }, "NestedStackB" : { "Type" : "AWS::CloudFormation::Stack", "Properties" : { "TemplateURL" : "https://s3.amazonaws.com/<pathway to .template for NestedStack B>", "Parameters" : { "BucketNameValueParameter" : { "Fn::GetAtt": [ "NestedStackA", "Outputs.BucketNameOutput" ] } } } } } }
YAML:
AWSTemplateFormatVersion: 2010-09-09 Resources: NestedStackA: Type: 'AWS::CloudFormation::Stack' Properties: TemplateURL: 'https://s3.amazonaws.com/<pathway to .template for NestedStack A>' NestedStackB: Type: 'AWS::CloudFormation::Stack' Properties: TemplateURL: 'https://s3.amazonaws.com/<pathway to .template for NestedStack B>' Parameters: BucketNameValueParameter: !GetAtt - NestedStackA - Outputs.BucketNameOutput
相关信息

相关内容
- AWS 官方已更新 1 年前