Sub topic 4
Parameterizing a template
This chapter assumes you can read a CloudFormation YAML template and know the Resources section and the Ref function. It covers the Parameters section: declaring inputs, constraining them, and supplying values from the CLI.
Why parameters exist
A template with hard-coded names and sizes can only build one thing. Parameters let you reuse one template for several environments or teams. Each parameter is a named input. You give its value when you create or update a stack, and CloudFormation substitutes it wherever the template refers to it.
Parameters are checked before any resource is touched. If a value breaks a rule, the operation fails immediately, and nothing is created and nothing needs rollback.
Declaring a parameter
Each entry under Parameters needs a Type. The common attributes are:
| Attribute | Purpose |
|---|---|
| `Default` | Value used when the caller supplies none. Without it, the parameter is required. |
| `Description` | Text shown in the console and `describe-stacks`. |
| `AllowedValues` | Exact list of accepted values. |
| `AllowedPattern` | Regular expression a String value must match. |
| `MinLength`, `MaxLength` | Length limits for strings. |
| `MinValue`, `MaxValue` | Range limits for numbers. |
| `ConstraintDescription` | Message shown when a constraint fails. |
| `NoEcho` | Masks the value in console and CLI output. |
A worked example
AWSTemplateFormatVersion: "2010-09-09"
Description: Bucket configured entirely through parameters
Parameters:
EnvName:
Type: String
Default: dev
AllowedValues:
- dev
- prod
Description: Deployment environment
BucketPrefix:
Type: String
AllowedPattern: "^[a-z0-9-]{3,20}$"
ConstraintDescription: 3 to 20 lowercase letters, digits or hyphens
RetentionDays:
Type: Number
Default: 30
MinValue: 1
MaxValue: 365
Teams:
Type: CommaDelimitedList
Default: "alpha,beta"
Description: Team names; the first one is tagged as owner
Resources:
AppBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: !Sub "${BucketPrefix}-${EnvName}-${AWS::AccountId}"
LifecycleConfiguration:
Rules:
- Id: ExpireOldObjects
Status: Enabled
ExpirationInDays: !Ref RetentionDays
Tags:
- Key: Owner
Value: !Select [0, !Ref Teams]
Outputs:
BucketName:
Value: !Ref AppBucketBucketPrefix has no default, so every caller must supply it. Fn::Sub reads the parameters by name, and the pseudo parameter AWS::AccountId makes the bucket name globally unique. Fn::Select picks the first item of a list parameter.
Parameter types
StringandNumberare the basic scalars.List<Number>andCommaDelimitedListgive you lists. ACommaDelimitedListsplits on commas and trims no quoting, so values cannot contain commas.- AWS-specific types such as
AWS::EC2::KeyPair::KeyNameorAWS::EC2::VPC::Idmake CloudFormation check that the value exists in your account and Region. The console also shows a dropdown. - SSM parameter types, such as
AWS::SSM::Parameter::Value<String>, take the name of a Systems Manager parameter and resolve its current value when the stack operation runs.
Parameters:
LatestAmiId:
Type: AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>
Default: /aws/service/ami-amazon-linux-latest/al2023-ami-kernel-default-x86_64The parameter holds the SSM path, not the AMI ID. Because the lookup is repeated on each update, a new AMI release can show up as a change to an instance property.
Supplying values from the CLI
With deploy, pass Key=Value pairs. Quote a list value as a whole:
aws cloudformation deploy \
--template-file template.yaml \
--stack-name params-demo \
--parameter-overrides BucketPrefix=acme EnvName=prod RetentionDays=90 "Teams=red,blue"With the lower-level commands, each parameter is a structure:
aws cloudformation update-stack \
--stack-name params-demo \
--use-previous-template \
--parameters \
ParameterKey=BucketPrefix,UsePreviousValue=true \
ParameterKey=EnvName,UsePreviousValue=true \
ParameterKey=RetentionDays,ParameterValue=120 \
ParameterKey=Teams,UsePreviousValue=trueUsePreviousValue=true keeps the value from the last operation. With update-stack, a parameter you omit that has no default makes the update fail, so list every required one.
Common errors
- "Parameters: [X] must have values." A parameter with no
Defaultwas not supplied. - Constraint failures. A value outside
AllowedValuesorAllowedPatternis rejected up front. Always setConstraintDescriptionso the message is readable. - Wrong list syntax. A
CommaDelimitedListgiven asa, bis accepted, but the shell splits unquoted spaces. Quote the wholeKey=Valuepair. - Parameters referencing parameters. A
Defaultmust be a literal. You cannot writeDefault: !Ref Other. - Unexpected replacement. Changing a parameter that feeds an immutable property, such as
BucketName, replaces the resource. Preview it with a change set first.
Keeping the interface manageable
A template can declare at most 200 parameters. Long lists are hard to fill in, so group them for the console with Metadata:
Metadata:
AWS::CloudFormation::Interface:
ParameterGroups:
- Label:
default: Naming
Parameters:
- BucketPrefix
- EnvName
- Label:
default: Lifecycle
Parameters:
- RetentionDaysGrouping only changes how the console displays the form. It does not affect the CLI or validation. Parameters that are not listed in any group still appear under a default heading.
Prefer few parameters with sensible defaults. If a value is derived from another, such as retention by environment, use a mapping and a condition rather than asking the caller for both.
Exercise
A template declares Port as Type: Number with MinValue: 1024 and MaxValue: 65535, and no Default. You run update-stack with --use-previous-template and --parameters ParameterKey=Port,ParameterValue=80. What happens, and what does the stack's existing resources go through?
Show answer
The update fails during parameter validation because 80 is below MinValue 1024. Validation runs before any resource is changed, so no resources are modified and no rollback is needed. The stack stays in its previous state (for example UPDATECOMPLETE or CREATECOMPLETE).