Help us improve the AWS re:Post Knowledge Center by sharing your feedback in a brief survey. Your input can influence how we create and update our content to better support your AWS journey.
AWS CloudFormationの同じ親スタック内にあるネストされたスタック間で値を渡すにはどうすればよいですか?
AWS CloudFormationの同じ親スタックにある2つのネストされたスタック間で値を渡したり共有したりしたいと考えています。
解決策
次の解決策では、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
関連情報
- 言語
- 日本語

関連するコンテンツ
- 質問済み 5年前