How to Use Outputs in CloudFormation Templates
In AWS CloudFormation, use the
Outputs section to declare values you want to display or share after stack creation. Outputs can show resource IDs, URLs, or export values for use in other stacks with Export names.Syntax
The Outputs section in a CloudFormation template defines key-value pairs that provide information about your stack resources. Each output has a logical name, a Description, a Value that can reference resources or parameters, and optionally an Export to share the value with other stacks.
yaml
Outputs:
OutputLogicalName:
Description: "Description of the output"
Value: !Ref SomeResource
Export:
Name: "ExportName"Example
This example shows a CloudFormation template outputting the ID of an S3 bucket it creates. The output is also exported so other stacks can import it.
yaml
Resources:
MyBucket:
Type: AWS::S3::Bucket
Outputs:
BucketNameOutput:
Description: "The name of the S3 bucket"
Value: !Ref MyBucket
Export:
Name: "MyBucketName"Output
After stack creation, the output "BucketNameOutput" shows the bucket name and exports it as "MyBucketName".
Common Pitfalls
- Missing Export Name: Forgetting to add an
Exportname means other stacks cannot import the output. - Duplicate Export Names: Export names must be unique within an AWS region; duplicates cause deployment errors.
- Incorrect References: Outputs must reference valid resources or parameters; invalid references cause stack failures.
yaml
Outputs:
WrongExport:
Description: "Missing export name"
Value: !Ref MyBucket
Outputs:
CorrectExport:
Description: "Proper export"
Value: !Ref MyBucket
Export:
Name: "UniqueExportName"Quick Reference
| Field | Description |
|---|---|
| Outputs | Section to declare output values |
| OutputLogicalName | Unique name for each output |
| Description | Explains what the output shows |
| Value | The actual value to output, often a resource reference |
| Export | Optional; shares output with other stacks using a unique name |
Key Takeaways
Use the Outputs section to expose useful information from your CloudFormation stack.
Export output values with unique names to share them across stacks.
Always provide a clear Description for each output to explain its purpose.
Avoid duplicate export names to prevent deployment errors.
Validate that output values reference existing resources or parameters.