aws cloudformation - sharing stack outputs with other stacks
We can share stacks outputs from one deployment with another separate deployment. Let's say we create a s3 store stack with outputs. Then we can use this output as input for another stack app stack later.
To see this in action, let create a "storage.yaml" s3 stack as shown here
AWSTemplateFormatVersion: '2010-09-09'
Description: Storage stack exporting an S3 bucket name and ARN
Resources:
MyDataBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: !Sub "my-data-bucket-${AWS::AccountId}"
Outputs:
BucketName:
Value: !Ref MyDataBucket
Export:
Name: !Sub "${AWS::StackName}-BucketName"
BucketArn:
Value: !GetAtt MyDataBucket.Arn
Export:
Name: !Sub "${AWS::StackName}-BucketArn"
Then deploy it:
aws cloudformation create-stack \
--stack-name storageStack \
--region ap-southeast-2 \
--template-body file://storage.yaml
Next we will create the app stack, called app.yaml
AWSTemplateFormatVersion: '2010-09-09'
Description: App stack importing an S3 bucket export from storageStack
Parameters:
StorageStackName:
Type: String
Default: storageStack
Description: Name of the stack that created and exported the S3 bucket
Resources:
AppRole:
Type: AWS::IAM::Role
Properties:
RoleName: my-app-role
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action: sts:AssumeRole
Policies:
- PolicyName: AccessImportedBucket
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- s3:GetObject
- s3:PutObject
Resource:
# Allow access to bucket and all objects
- !ImportValue
'Fn::Sub': "${StorageStackName}-BucketArn"
- !Sub
- "${BucketArn}/*"
-
BucketArn:
!ImportValue
'Fn::Sub': "${StorageStackName}-BucketArn"
Outputs:
ImportedBucketArn:
Description: The ARN of the imported S3 bucket
Value: !ImportValue
'Fn::Sub': "${StorageStackName}-BucketArn"
Next we will create the app stack by running this command:-
aws cloudformation create-stack \
--stack-name appStack \
--region ap-southeast-2 \
--capabilities CAPABILITY_NAMED_IAM \
--template-body file://app.yaml
And you can see the final output from here
Comments