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.
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/*'
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