When I create a template in AWS CloudFormation, I want to reference a resource in another CloudFormation stack.
Resolution
To reference a resource in another CloudFormation stack, you must first create cross-stack references. The following resolution provides an example of one method to create a cross-stack reference. The example NetworkStack stack creates the ${AWS::StackName}-SecurityGroupID and ${AWS::StackName}-SubnetID network-related resources and exports. After stack creation, CloudFormation replaces ${AWS::StackName} with NetworkStack. The final export names are NetworkStack-SecurityGroupID and NetworkStack-SubnetID.
Create a stack to export output values
Complete the following steps:
- Create a CloudFormation stack, and then use the SampleNetworkCrossStack template.
Note: You must declare the Export field in the Outputs section of the template.
- Name the stack NetworkStack.
Note: NetworkStack exports the subnet and security group values.
Use an imported subnet and security group to create an Amazon EC2 instance
Amazon Elastic Compute Cloud (Amazon EC2) instance properties, such as SubnetId and SecurityGroupId, use the values from the exporting stack. To import the values, use the Fn::ImportValue intrinsic function.
Note: The importing and exporting stack must be in the same AWS Region and AWS account. Also, the exported value names must be unique to your Region and account.
To create the EC2 instance, complete the following steps:
- Open the CloudFormation console.
- Choose Create Stack, and then choose Design template.
- Choose the Parameters tab of the code editor, and then choose Template.
- To use the values from the exporting stack to create the instance, enter the following template into the code editor:
Important: Set the NetworkStack resource stack as the value for NetworkStackParameter. The NetworkStack value replaces the correct stack name in the corresponding Fn::ImportValue functions.
{ "Parameters": {
"NetworkStackParameter": {
"Type": "String"
}
},
"Resources": {
"WebServerInstance": {
"Type": "AWS::EC2::Instance",
"Properties": {
"InstanceType": "t2.micro",
"ImageId": "ami-a1b23456",
"NetworkInterfaces": [
{
"GroupSet": [
{
"Fn::ImportValue": {
"Fn::Sub": "${NetworkStackParameter}-SecurityGroupID"
}
}
],
"AssociatePublicIpAddress": "true",
"DeviceIndex": "0",
"DeleteOnTermination": "true",
"SubnetId": {
"Fn::ImportValue": {
"Fn::Sub": "${NetworkStackParameter}-SubnetID"
}
}
}
]
}
}
}
}
Note: Replace t2.micro with your instance type and ami-a1b23456 with your Amazon Machine Image (AMI) ID.
- Choose Create Stack (cloud icon with up arrow), and then choose Next.
- For Stack name, enter a name for your stack.
- For Parameters, enter the network stack name that you want to cross reference, for example NetworkStack.
- Choose Next, and then choose Next again.
- Choose Create.
- After the stack status changes to CREATE_COMPLETE, open the Amazon EC2 console.
- In the navigation pane, choose Instances, and then select the instance that you created.
- Choose the Description tab, and then verify that the security group and subnet are configured.
Important: When another stack is importing the source stack or the source stack's export values, you can't delete these values. To update the source stack's export values, first manually replace the values in the stacks that are importing the source stack's export values. Then, you can update the export values of the source stack.
To list all stacks that are importing an exported output value, run the list-imports command. To list all exports in a Region, use the CloudFormation console or run the list-exports command. The export name must be unique for the account in the Region.
Note: If you receive errors when you run AWS Command Line Interface (AWS CLI) commands, then see Troubleshoot AWS CLI errors. Also, make sure that you're using the most recent AWS CLI version.
Related information
How do I use parameters in AWS Systems Manager Parameter Store to share values between CloudFormation stacks?
AWS CloudFormation templates
AWS::EC2::Instance
Refer to resource outputs in another CloudFormation stack