CloudFormation Stack Export
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed CloudFormation StackSets.
https://cloudaffaire.com/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’.
Step 2: Create a stack named ‘myVpcSTACK’ with below YAML code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
Resources: myVPC: Type: 'AWS::EC2::VPC' Properties: CidrBlock: 10.0.0.0/16 EnableDnsSupport: 'true' EnableDnsHostnames: 'true' Tags: - Key: "Name" Value: "myVPC" Outputs: vpcID: Description: VPC id Value: !Ref myVPC Export: Name: Fn::Sub: "${AWS::StackName}-VpcID" |
Or you can use Stack_export_demo_create_vpc.yaml from below GitHub repo.
https://github.com/CloudAffaire/CloudFormation
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.
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
Parameters: SourceStackName: Description: "Source stack name" Type: String AllowedPattern: '^[a-zA-Z][-a-zA-Z0-9]*$' Default: "myVpcSTACK" Resources: mySUBNET: Type: 'AWS::EC2::Subnet' Properties: VpcId: Fn::ImportValue: Fn::Sub: "${SourceStackName}-VpcID" CidrBlock: 10.0.0.0/24 MapPublicIpOnLaunch: 'true' Tags: - Key: "Name" Value: "mySUBNET" |
Or you can use Stack_export_demo_create_subnet.yaml from below GitHub repo.
https://github.com/CloudAffaire/CloudFormation
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.
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
https://docs.aws.amazon.com/cloudformation/index.html