💾 Archived View for cfdocs.wetterberg.nu › walkthrough-crossstackref.gemini captured on 2023-11-04 at 12:00:09. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2021-12-03)

-=-=-=-=-=-=-

Walkthrough: Refer to resource outputs in another AWS CloudFormation stack

Search

To export resources from one AWS CloudFormation stack to another, create a cross-stack reference. Cross-stack references let you use a layered or service-oriented architecture. Instead of including all resources in a single stack, you create related AWS resources in separate stacks; then you can refer to required resource outputs from other stacks. By restricting cross-stack references to outputs, you control the parts of a stack that are referenced by other stacks.

For example, you might have a network stack with a VPC, a security group, and a subnet for public web applications, and a separate public web application stack. To ensure that the web applications use the security group and subnet from the network stack, you create a cross-stack reference that allows the web application stack to reference resource outputs from the network stack. With a cross-stack reference, owners of the web application stacks don't need to create or maintain networking rules or assets.

To create a cross-stack reference, use the `Export` output field to flag the value of a resource output for export. Then, use the `Fn::ImportValue` intrinsic function to import the value. For more information, see Outputs and Fn::ImportValue.

Outputs

Fn::ImportValue

Prerequisites

Before you begin this walkthrough, check that you have AWS Identity and Access Management \(IAM\) permissions to use all of the following services: Amazon VPC, Amazon EC2, and AWS CloudFormation.

AWS Identity and Access Management (IAM) permissions

AWS CloudFormation is a free service. However, you are charged for the AWS resources that you include in your stacks at the current rate for each one. For more information about AWS pricing, see the detail page for each product.

The following restrictions apply to cross-stack references:

For each AWS account, `Export` names must be unique within a region.

You can't create cross-stack references across regions. You can use the intrinsic function `Fn::ImportValue` to import only values that have been exported within the same region.

For outputs, the value of the `Name` property of an `Export` can't use `Ref` or `GetAtt` functions that depend on a resource.

Similarly, the `ImportValue` function can't include `Ref` or `GetAtt` functions that depend on a resource.

You can't delete a stack if another stack references one of its outputs.

You can't modify or remove an output value that is referenced by another stack.

the detail page for each product

Step 1: Use a sample template to create a network stack

The network stack contains the VPC, security group, and subnet that you will use in the web application stack. In addition to these resources, the network stack creates an Internet gateway and routing tables to enable public access.

You must create this stack before you create the web application stack. If you create the web application stack first, it won't have a security group or subnet.

AWS CloudFormation console

Viewing AWS CloudFormation stack data and resources on the AWS Management Console

Step 2: Use a sample template to create a web application stack

The web application stack creates an EC2 instance that uses the security group and subnet from the network stack.

You must create this stack in the same region as the network stack.

AWS CloudFormation console

https://s3.amazonaws.com/cloudformation-examples/user-guide/cross-stack/SampleWebAppCrossStack.template

Viewing AWS CloudFormation stack data and resources on the AWS Management Console

Amazon EC2 console

Viewing AWS CloudFormation stack data and resources on the AWS Management Console

Step 3: Clean up your resources

To ensure that you are not charged for unwanted services, delete the stacks.

Use the sample templates from this walkthrough to build your own cross-referenced stacks.