Sub topic 9
Exposing values with Outputs
This chapter assumes you can read a template with Parameters, Resources and Conditions, and that you have created a stack from the AWS CLI. It covers the Outputs section: what it produces, how to read it, and how exports turn it into a link between stacks.
What Outputs do
Most resource properties are generated at creation time. Examples are a bucket ARN, a load balancer DNS name and a security group ID. The Outputs section lets a template publish selected values from those resources once the stack is created or updated. Outputs are optional.
An output has a logical name and these fields:
| Field | Required | Purpose |
|---|---|---|
| `Value` | Yes | The value to publish. Usually `!Ref`, `!GetAtt` or `!Sub`. |
| `Description` | No | A text note shown in the console and CLI. |
| `Export.Name` | No | Makes the value available to other stacks in the same account and Region. |
| `Condition` | No | Publishes the output only if the named condition is true. |
Output logical names must be alphanumeric and unique within the template. A template can declare at most 200 outputs.
A basic output
The bucket template from earlier in this tutorial ends with this section:
Outputs:
BucketArn:
Description: ARN of the application bucket
Value: !GetAtt AppBucket.Arn
Export:
Name: !Sub "${AWS::StackName}-BucketArn"Value is evaluated after the resources it refers to exist. That is why !GetAtt AppBucket.Arn works: CloudFormation resolves the output last. If a resource fails to create, the stack rolls back and no outputs are published.
Reading outputs from the CLI
Outputs are stored with the stack. Read them with describe-stacks and a JMESPath query.
# All outputs of one stack
aws cloudformation describe-stacks \
--stack-name demo \
--query "Stacks[0].Outputs"
# A single value, as plain text, for use in a script
aws cloudformation describe-stacks \
--stack-name demo \
--query "Stacks[0].Outputs[?OutputKey=='BucketArn'].OutputValue" \
--output text
# Every export in this account and Region
aws cloudformation list-exportsScripts and CI pipelines commonly use the second form to pass a stack's results to a later deployment step.
Conditional outputs
An output can carry a Condition. This matters when the value comes from a resource that exists only in some environments. Referring to a resource that was never created is an error, so the output must share the condition of that resource.
Conditions:
IsProd: !Equals [!Ref EnvName, prod]
Resources:
AuditBucket:
Type: AWS::S3::Bucket
Condition: IsProd
Outputs:
AuditBucketName:
Condition: IsProd
Description: Audit bucket, created only in prod
Value: !Ref AuditBucketExports and cross-stack references
Adding Export registers the output under a name that other stacks can read with Fn::ImportValue. The consumer stack from the earlier example does this:
Resource: !Join
- ""
- - !ImportValue demo-BucketArn
- "/*"Several rules govern exports:
- Export names must be unique within an account and Region.
- Only stacks in the same account and Region can import a value. Cross-Region sharing is not supported.
Export.Namecan useFn::Sub,Fn::Joinand references to parameters and pseudo parameters. It cannot use a value that is only known after resources are created. This is why${AWS::StackName}is a popular prefix.- The imported value is a string. Use
Fn::Splitto rebuild lists.
The dependency lock
An export in use creates a hard dependency. CloudFormation will refuse to delete the exporting stack while any stack imports one of its values. You also cannot change the value or the name of an export that is being imported. Both operations fail with an error that names the importing stack.
To find who is importing a value, run:
aws cloudformation list-imports --export-name demo-BucketArnTo remove the dependency, update each consumer so it no longer uses the import, then change or delete the producer.
Outputs of nested stacks
An output does not need Export to be useful in a nested stack design. A parent template reads a child's output directly with Fn::GetAtt, using the child's logical name followed by Outputs. and the output name:
Value: !GetAtt NetworkStack.Outputs.VpcIdThis avoids registering an account-wide export name, and the dependency stays inside one parent stack.
Choosing between plain outputs and exports
| Need | Use |
|---|---|
| Show a value to a person or script | Output without `Export` |
| Share a value between independent stacks with different lifecycles | Output with `Export` and `Fn::ImportValue` |
| Pass a value from a child to its parent | Output read with `Fn::GetAtt` |
Treat each export as a public interface. The more stacks that import it, the harder it is to change. Export only values that other teams or stacks genuinely need.
Common errors
No export named ... found: the producer stack does not exist yet, the name is misspelled, or it is in another Region.Export ... cannot be deleted as it is in use: an importer still exists. Uselist-imports.Template format error: Unresolved resource dependencies: an output refers to a resource that is missing or conditional, and the output has no matchingCondition.Export with name ... is already exported: another stack in the account and Region uses that name. Include${AWS::StackName}in the name.
Exercise
Stack net exports net-VpcId, and stack app imports it. You try to delete net and the operation fails. What command shows which stack is blocking the deletion, and what must you do before the deletion can succeed?
Show answer
Run aws cloudformation list-imports --export-name net-VpcId. It lists app as an importer. Update app so it no longer uses Fn::ImportValue for net-VpcId, or delete app. After that, net can be deleted.
Exercise
An output refers to AuditBucket, which has Condition: IsProd. The stack fails validation in the dev environment. What is the fix?
Show answer
Add Condition: IsProd to the output. The output is then skipped when the condition is false, so it never refers to a resource that was not created.