0
0
AWScloud~5 mins

Outputs for cross-stack references in AWS - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you build cloud resources in separate groups called stacks, you often need to share information between them. Outputs let one stack share values like resource IDs or URLs so another stack can use them easily.
When you create a database in one stack and want to connect your app in another stack to that database.
When you deploy a network setup in one stack and need to use its subnet IDs in another stack.
When you want to share a storage bucket name created in one stack with another stack that uploads files.
When you separate your infrastructure into parts for easier management but still need them to work together.
When you want to avoid hardcoding resource details and instead get them dynamically from another stack.
Config File - main.yaml
main.yaml
AWSTemplateFormatVersion: '2010-09-09'
Resources:
  MyBucket:
    Type: AWS::S3::Bucket
Outputs:
  BucketNameOutput:
    Description: "Name of the S3 bucket"
    Value: !Ref MyBucket
    Export:
      Name: MyBucketName

This CloudFormation template creates an S3 bucket resource named MyBucket. The Outputs section defines BucketNameOutput which shares the bucket's name. The Export key with Name: MyBucketName makes this output available for other stacks to import by this name.

Commands
This command creates a stack named 'bucket-stack' using the template file 'main.yaml'. It provisions the S3 bucket and sets up the output for sharing.
Terminal
aws cloudformation deploy --template-file main.yaml --stack-name bucket-stack
Expected OutputExpected
Waiting for stack create/update to complete... Successfully created/updated stack - bucket-stack
--template-file - Specifies the CloudFormation template file to use
--stack-name - Names the stack to create or update
This command lists all exported output values from all stacks, showing the shared names and their values.
Terminal
aws cloudformation list-exports
Expected OutputExpected
Exports: - ExportingStackId: arn:aws:cloudformation:us-east-1:123456789012:stack/bucket-stack/abcd1234 Name: MyBucketName Value: bucket-stack-mybucket-1a2b3c4d5e6f7g
This command deploys another stack 'app-stack' using 'app.yaml' and passes the bucket name from the exported output as a parameter to connect resources.
Terminal
aws cloudformation deploy --template-file app.yaml --stack-name app-stack --parameter-overrides BucketName=bucket-stack-mybucket-1a2b3c4d5e6f7g
Expected OutputExpected
Waiting for stack create/update to complete... Successfully created/updated stack - app-stack
--parameter-overrides - Passes parameters to the stack, here using the exported bucket name
Key Concept

If you remember nothing else from this pattern, remember: outputs let one stack share resource details that other stacks can import to connect resources safely and dynamically.

Common Mistakes
Not adding the Export name in the output section
Without Export, other stacks cannot import the output value, so cross-stack sharing fails.
Always include the Export key with a unique name in the output you want to share.
Trying to import an output that does not exist or has a different export name
The stack import will fail because it cannot find the matching exported output.
Check the export names carefully using 'aws cloudformation list-exports' before importing.
Hardcoding exported values instead of importing them dynamically
Hardcoding causes errors if resource names change or stacks are redeployed with different values.
Use CloudFormation's ImportValue function or pass parameters dynamically from exported outputs.
Summary
Define outputs with Export names in one stack to share resource details.
Use AWS CLI to deploy stacks and list exported outputs.
Import exported values in other stacks to connect resources dynamically.