Como faço para passar valores entre pilhas aninhadas dentro da mesma pilha pai no AWS CloudFormation?

3 minuto de leitura
0

Quero passar ou compartilhar um valor entre duas pilhas aninhadas dentro da mesma pilha pai no AWS CloudFormation.

Resolução

A resolução a seguir usa os recursos AWS::CloudFormation::Stack NestedStackA e NestedStackB que fazem parte da mesma pilha pai chamada RootStack. Você está passando um valor de NestedStackA para NestedStackB. O NestedStackA criou o recurso de bucket do S3 e o NestedStackB criou a política de bucket do S3 anexada ao bucket do S3.

Conclua as etapas a seguir:

  1. Na seção Saídas do seu modelo do CloudFormation para NestedStackA, inclua o valor que você deseja compartilhar.
    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.

    Observação: Nos modelos anteriores, substitua DOC-EXAMPLE-BUCKET pelo nome do seu bucket.A seção Saídas dos modelos anteriores retorna o nome do bucket de !Ref.

  2. Na seção Parâmetros do modelo do CloudFormation para NestedStackB, inclua um parâmetro para usar o nome do bucket do S3 da saída do NestedStackA.
    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. Para passar um valor entre NestedStackA e NestedStackB, configure o RootStack para usar a função Fn::GetAtt na seção Parâmetro para NestedStackB. Use o ID lógico do NestedStackA e o nome do bucket no formato 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

Informações relacionadas

Incorporar pilhas em outras usando pilhas aninhadas

Trechos de modelo do AWS CloudFormation

AWS OFICIAL
AWS OFICIALAtualizada há um mês