跳至內容

如何在 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 官方已更新 1 年前