Comment transmettre des valeurs entre des piles imbriquées au sein de la même pile parent dans AWS CloudFormation ?

Lecture de 3 minute(s)
0

Je souhaite transmettre ou partager une valeur entre deux piles imbriquées au sein de la même pile parent dans AWS CloudFormation.

Résolution

La résolution suivante utilise les ressources AWS::CloudFormation::Stack NestedStackA et NestedStackB qui font partie intégrante de la même pile parente nommée RootStack. Vous transmettez une valeur de NestedStackA vers NestedStackB. NestedStackA a créé la ressource de compartiment S3 et NestedStackB a créé la politique de compartiment S3 qui est associée au compartiment S3.

Procédez comme suit :

  1. Dans la section Sorties de votre modèle CloudFormation pour NestedStackA, incluez la valeur que vous souhaitez partager.
    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.

    Remarque : Dans les modèles précédents, remplacez DOC-EXAMPLE-BUCKET par le nom de votre compartiment. La section Sorties des modèles précédents renvoie le nom du compartiment provenant de !Ref.

  2. Dans la section Paramètres du modèle CloudFormation pour NestedStackB, incluez un paramètre permettant d'utiliser le nom du compartiment S3 à partir de la sortie de 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. Pour transmettre une valeur entre NestedStackA et NestedStackB, configurez RootStack pour utiliser la fonction Fn::GetAtt dans la section Paramètre pour NestedStackB. Utilisez l'ID logique de NestedStackA et la valeur de sortie du nom du compartiment au format 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

Informations connexes

Intégrer des piles dans d'autres piles à l'aide de piles imbriquées

Extraits de modèles AWS CloudFormation

AWS OFFICIEL
AWS OFFICIELA mis à jour il y a 22 jours