CloudFormation Stack Export

CloudFormation Stack Export

Hello Everyone

Welcome to CloudAffaire and this is Debjeet.

In the last blog post, we have discussed CloudFormation StackSets.

In this blog post, we are going to discuss how to share information between stacks using Stack Export.

CloudFormation Stack Export:

To share information between stacks, we can export a stack’s output values. Other stacks that are in the same AWS account and region can then import the exported values. To export a stack’s output value, we can use the Export field in the Output section of the stack’s template. To import those values, we can use the Fn::ImportValue function in the template for the other stacks.

In this demo, we are going to create two stacks, one for VPC creation and one for Subnet creation. The VPC creation stack will output the vpc-id data and using Fn:ImportValue function in the subnet creation stack, we will import the vpi-id required to create the Subnet.

Step 1: Login to AWS console and navigate to ‘CloudFormation’.

CloudFormation Stack Export

Step 2: Create a stack named ‘myVpcSTACK’ with below YAML code.

    Type: 'AWS::EC2::VPC'
      EnableDnsSupport: 'true'
      EnableDnsHostnames: 'true'
        - Key: "Name"
          Value: "myVPC"
    Description: VPC id
    Value: !Ref myVPC
          Fn::Sub: "${AWS::StackName}-VpcID"

Or you can use Stack_export_demo_create_vpc.yaml from below GitHub repo.

CloudFormation Stack Export

Observe: We have exported the VPC-ID value from myVpcSTACK stack in the form of output.

You can also view all the exports under ‘Export’ tab.

CloudFormation Stack Export

Next, we are going to create another stack for subnet creation using exported vpi-id of the 1st stack.

Step 3: Create a stack named ‘mySubnetSTACK’ with below YAML code.

    Description: "Source stack name"
    Type: String
    AllowedPattern: '^[a-zA-Z][-a-zA-Z0-9]*$'
    Default: "myVpcSTACK"
    Type: 'AWS::EC2::Subnet'
          Fn::Sub: "${SourceStackName}-VpcID" 
      MapPublicIpOnLaunch: 'true'
        - Key: "Name"
          Value: "mySUBNET"

Or you can use Stack_export_demo_create_subnet.yaml from below GitHub repo.

CloudFormation Stack Export

Note: You have to pass the 1st stack name (myVpcSTACK) as a parameter in the 2nd stack.

You can view the stack import details under ‘Export details’ of the export.

CloudFormation Stack Export

Hope you have enjoyed this article. In the next blog post, we will CloudFormation stack change sets.

To get more details on CloudFormation, please refer below AWS documentation


Leave a Reply

Close Menu