Deseo transferir o compartir un valor entre dos pilas anidadas en la misma pila principal en AWS CloudFormation.
Resolución
La siguiente resolución utiliza los recursos de AWS::CloudFormation::Stack NestedStackA y NestedStackB, que forman parte de la misma pila principal, denominada RootStack. Estás pasando un valor de NestedStackA a NestedStackB. NestedStackA creó el recurso de bucket de S3 y NestedStackB creó la política de bucket de S3 que se adjunta al bucket de S3.
Sigue estos pasos:
-
En la sección Salidas de la plantilla de CloudFormation para NestedStackA, incluya el valor que deseas compartir.
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.
Nota: En las plantillas anteriores, sustituye DOC-EXAMPLE-BUCKET por el nombre de tu bucket. La sección Salidas de las plantillas anteriores devuelve el nombre del bucket de**!Ref**.
-
En la sección Parámetros de la plantilla de CloudFormation para NestedStackB, incluye un parámetro para usar el nombre del bucket de S3 de la salida 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/*'
-
Para pasar un valor entre NestedStackA y NestedStackB, configura RootStack para que use la función Fn::GetAtt en la sección Parámetros de NestedStackB. Utiliza el ID lógico de NestedStackA y el nombre del valor de salida del bucket en el formato Outputs.NombreSalidaNestedStack.
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
Información relacionada
Integre pilas dentro de otras pilas mediante pilas anidadas
Fragmentos de plantillas de AWS CloudFormation