如何在 AWS CloudFormation 中同一父堆栈内的嵌套堆栈之间传递值?

3 分钟阅读
0

我想在 AWS CloudFormation 中同一父堆栈中的两个嵌套堆栈之间传递或共享值。

解决方法

以下解析使用了 AWS::CloudFormation::Stack 资源 NestedStackANestedStackB,它们是名为 RootStack 的同一个父堆栈的一部分。您要将一个值从 NestedStackA 传递给 NestedStackBNestedStackA 创建了 S3 存储桶资源,而 NestedStackB 创建了附加到 S3 存储桶的 S3 存储桶策略。

完成以下步骤:

  1. 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 的存储桶名称。

  2. 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/*'
  3. 要在 NestedStackANestedStackB 之间传递值,请将 RootStack 配置为使用 NestedStackBParameter(参数)部分中的 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 CloudFormation 模板代码段

AWS 官方已更新 2 个月前