Ongoing service disruptions
For the most recent update on ongoing service disruptions affecting the AWS Middle East (UAE) Region (ME-CENTRAL-1), refer to the AWS Health Dashboard. For information on AWS Service migration, see How do I migrate my services to another region?
如何在 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
相關資訊
- 語言
- 中文 (繁體)

相關內容
- 已提問 8 個月前
- 已提問 1 年前
- 已提問 3 年前