AWS Cloud Formation Cross Stack

As mentioned in AWS blog https://aws.amazon.com/blogs/aws/aws-cloudformation-update-yaml-cross-stack-references-simplified-substitution/

Many AWS customers use one “system” CloudFormation stack to set up their environment (VPCs, VPC subnets, security groups, IP addresses, and so forth) and several other “application” stacks to populate it (EC2 & RDS instances, message queues, and the like). 
Until now there was no easy way for the application stacks to reference resources created by the system stack.

You can now create and export values from one stack and make use of them in other stacks without going to the trouble of creating custom CloudFormation resources.

Lets try this… We write couple of templates : first one is to create vpc with subnets and the second one to create EC2 instance in the public subnet of vpc created.

First CF template to form the VPC with three public subnets and three private subnets.  In the Outputs sections we need to export the values that we would like to access from other templates.

In the example chosen we exported VPC and Public Subnet One as we are going to use these values in the second template.

AWS_CF_CrossStack1.JPG

Using the CF Template created stack..

AWS_CF_CrossStack2.JPG

Now the second template to create EC2 Instance in the Public Subnet One of VPC created above.. We need to import the values using “Fn::ImportValue”

AWS_CF_CrossStack3.JPG

AWS_CF_CrossStack4

Using EC2 Instance CF Template provisioned one..

AWS_CF_CrossStack5.JPG

Now lets check the Security Group..

AWS_CF_CrossStack7.JPG

VPC and Subnet of EC2 Instance..

AWS_CF_CrossStack6.JPG

Note that the exported names must be unique with the AWS account and the region. A stack that is referenced by another stack cannot be deleted and it cannot modify or remove the exported value.

Leave a comment