When I use Fn::ImportValue in my AWS CloudFormation stack, I receive the "No Export named XYZ found" error.
Resolution
Prerequisites:
- Use the Export: flag in the Outputs section of your CloudFormation template for the stack where you're exporting a value.
- Use Fn::ImportValue in the stack where you're referencing the exported value.
To resolve issues with stack references when you use import and export values, take the following actions.
If you use nested stacks, then use DependsOn
By default, CloudFormation creates all nested stacks in parallel. When CloudFormation creates child stacks in parallel, and a child stack imports an output from another, stack creation might fail. The export value might not be available in time for the child stack to use Fn::ImportValue to import it.
To resolve this issue, use the DependsOn attribute. This attribute creates an explicit dependency between the stack that uses Fn::ImportValue and the stack that uses the Export: attribute to export a value. This way, CloudFormation creates the stack that uses Fn::ImportValue only after it creates the stack that exports the value.
Example: In a nested stack, ChildStack01 exports a value in the Outputs section, and ChildStack02 uses Fn::ImportValue to import the value from ChildStack01. In the following YAML template, ChildStack02 uses the DependsOn attribute:
AWSTemplateFormatVersion: 2010-09-09Resources:
ChildStack01:
Type: 'AWS::CloudFormation::Stack'
Properties:
TemplateURL: 'https://s3.amazonaws.com/cloudformation-templates-us-east-1/VPC.template'
TimeoutInMinutes: '60'
ChildStack02:
Type: 'AWS::CloudFormation::Stack'
DependsOn: ChildStack01
Properties:
TemplateURL: 'https://s3.amazonaws.com/cloudformation-templates-us-east-1/Subnet.template'
TimeoutInMinutes: '60'
Note: For cross-stack references, use Fn::ImportValue to import a value from another template. For references within the same stack, use Fn::Ref and Fn::GetAtt to return the value of the specified parameter or resource.
Verify the export name in the importing stack
To verify your export value is accurate, take the following actions:
- Confirm that the export name is listed in your AWS account.
- When you import an export name from one stack to another stack, confirm that you're using the same export name in both stacks.
- Before you create the stack with Fn::ImportValue, see the Verify the stack configuration of the export and import stacks section.
Make sure that the exported value is in the same AWS Region or same account as where you're importing the value
You can use cross-stack references only within a single account and Region. However, other stacks that are in the same account and Region can only import the exported values.
To resolve this issue, before you create the stack with Fn::ImportValue, see the Verify the stack configuration of the export and import stacks section.
Make sure that the exported value is created or published before the stack imports it
For non-nested stacks, you must first deploy the stack that's exporting a value. The stack must be in the Create_Complete or Update_Complete state.
Before you create the stack with Fn::ImportValue, see the Verify the stack configuration of the export and import stacks section.
Verify the stack configuration of the export and import stacks
To verify that the export value is ready to import, use the CloudFormation console or the AWS Command Line Interface (AWS CLI).
Note: The import and export stacks must be in the same Region and account.
To use the CloudFormation console, complete the following steps:
- Open the CloudFormation console.
- From the navigation pane, choose Exports.
- Confirm that the export value for the stack is listed.
To use the AWS CLI, complete the following steps:
-
To list the available exports, run the following list-exports command:
aws cloudformation list-exports --region us-east-1
Note: Replace us-east-1 with your Region.
-
In the output, verify that the value for Name in the export is the same as the Fn::ImportValue value.
The output looks similar to this example:
aws cloudformation list-exports --region us-east-1 --output yamlExports:
- ExportingStackId: arn:aws:cloudformation:us-east-1:123456789012:stack/private-vpc/99764070-b56c-xmpl-bee8-062a88d1d800
Name: private-vpc-subnet-a
Value: subnet-01a234bcdefghij56
Note: If you receive errors when you run AWS CLI commands, then see Troubleshoot AWS CLI errors. Also, make sure that you're using the most recent AWS CLI version.
After a stack imports an output value, you can't delete the stack that's exporting the output value or modify the exported output value. You must remove all the imports before you can delete the exporting stack or modify the output value. For information about how to use a workaround, see How do I use parameters in AWS Systems Manager Parameter Store to share values between CloudFormation stacks? Note that the export name must be unique within the Region.
The import statement in the stack that's importing the information looks similar to this example:
Resources:
WebServerInstance:
Type: 'AWS::EC2::Instance'
Properties:
InstanceType: t2.micro
ImageId: ami-a1b23456
NetworkInterfaces:
AssociatePublicIpAddress: 'true'
DeviceIndex: '0'
DeleteOnTermination: 'true'
SubnetId: Fn::ImportValue: 'private-vpc-subnet-a'