Stacks, Parameters & Outputs
Reuse one template across environments with parameters, and share values between stacks via outputs.
A single template should power every environment. Parameters make that possible: the same file becomes dev, stage, or prod depending on the values you pass in.
Composing stacks
- Parameters feed environment-specific values in at deploy time.
- Outputs expose values (a VPC id, a database endpoint) other stacks can import.
- Nested / layered stacks split a big system into a network layer, a data layer, and an app layer that reference each other.
This layering keeps templates small, lets teams own different layers, and limits the blast radius of any one change.
Example
# Deploy the same template to two environments with different params
aws cloudformation deploy \
--template-file infra/app.yaml \
--stack-name myapp-staging \
--parameter-overrides EnvName=staging InstanceCount=1
aws cloudformation deploy \
--template-file infra/app.yaml \
--stack-name myapp-prod \
--parameter-overrides EnvName=prod InstanceCount=4When to use it
- A team uses CloudFormation parameters to deploy the same VPC template to dev, stage, and prod with different CIDR blocks and instance sizes.
- An ops engineer exports the VPC ID from a networking stack as an output and imports it into an application stack using cross-stack references.
- A company passes environment-specific values as parameters through CodePipeline so the same template deploys correctly to each stage.
More examples
Template with Parameters and Outputs
Defines parameters for environment and instance type, then exports the created instance ID for use in other stacks.
Parameters:
Environment:
Type: String
AllowedValues: [dev, staging, prod]
InstanceType:
Type: String
Default: t3.micro
Resources:
AppServer:
Type: AWS::EC2::Instance
Properties:
InstanceType: !Ref InstanceType
Tags:
- Key: Env
Value: !Ref Environment
Outputs:
InstanceId:
Value: !Ref AppServer
Export:
Name: !Sub '${AWS::StackName}-InstanceId'Import output from another stack
Uses Fn::ImportValue to pull a VPC ID exported by a separate networking stack into the current template.
Resources:
MyBucket:
Type: AWS::S3::Bucket
Properties:
# Reference output exported by the networking stack
BucketName: !Sub
- 'logs-${VpcId}'
- VpcId: !ImportValue networking-stack-VpcIdDeploy with parameter overrides
Deploys the stack to prod by passing parameter overrides so the same template is reused across all environments.
aws cloudformation deploy \
--template-file app.yaml \
--stack-name app-prod \
--parameter-overrides \
Environment=prod \
InstanceType=t3.small
Discussion