AWS CloudFormation의 동일한 상위 스택 내에서 중첩된 스택 간에 값을 전달하려면 어떻게 해야 합니까?

3분 분량
0

AWS CloudFormation의 동일한 상위 스택 내에 있는 두 중첩된 스택 간에 값을 전달 또는 공유하려고 합니다.

해결 방법

다음 해결 방법은 RootStack이라는 동일한 상위 스택에 속한 AWS::CloudFormation::Stack 리소스인 NestedStackANestedStackB를 사용합니다. NestedStackA에서 NestedStackB로 값을 전달합니다. NestedStackA는 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 간에 값을 전달하려면 NestedStackBParameter 섹션에서 Fn::GetAtt 함수를 사용하도록 RootStack을 구성합니다. Outputs.NestedStackOutputName 형식의 NestedStackA의 논리적 ID와 버킷 이름 출력 값을 사용합니다.
    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 공식
AWS 공식업데이트됨 한 달 전
댓글 없음

관련 콘텐츠