Sub topic 3
Parameterizing a template
This chapter assumes you can read YAML, know the template sections (Parameters, Resources, Outputs), and have the AWS CLI configured. A parameter is an input value you supply when you create or update a stack. Parameters let one template build different stacks, such as a dev stack and a prod stack, without editing the file.
Declaring a parameter
Parameters live in the top-level Parameters section. Each one needs a Type. Other properties are optional but make the template safer.
Parameters:
EnvName:
Type: String
Default: dev
AllowedValues:
- dev
- prod
Description: Deployment environmentYou read a parameter's value with !Ref EnvName. Use !Sub "${EnvName}" when the value sits inside a longer string.
Common parameter properties
| Property | Purpose |
|---|---|
| `Default` | Value used when none is supplied. Without it, the caller must provide one. |
| `AllowedValues` | A fixed list of accepted values. |
| `AllowedPattern` | A regular expression the value must match. |
| `MinLength`, `MaxLength` | Length limits for `String` parameters. |
| `MinValue`, `MaxValue` | Range limits for `Number` parameters. |
| `ConstraintDescription` | The message shown when validation fails. |
| `NoEcho` | Masks the value in console and API output. |
Parameter types
String,Number, andCommaDelimitedListare the basic types.List<Number>is a list of numbers.- AWS-specific types such as
AWS::EC2::KeyPair::KeyNameorAWS::EC2::VPC::Idmake CloudFormation check that the value exists in your account and Region. - SSM parameter types such as
AWS::SSM::Parameter::Value<String>take the name of a Systems Manager parameter and resolve its current value.
Parameters:
InstanceType:
Type: String
Default: t3.micro
AllowedPattern: "^t3\\..*"
ConstraintDescription: Must be a t3 instance type.
LatestAmiId:
Type: AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>
Default: /aws/service/ami-amazon-linux-latest/al2023-ami-kernel-default-x86_64Supplying values from the CLI
With deploy, use --parameter-overrides. With create-change-set or create-stack, use --parameters.
aws cloudformation deploy \
--template-file template.yaml \
--stack-name demo \
--parameter-overrides EnvName=prodOn an update through the low-level commands, a parameter you leave out falls back to its Default. To keep the value already in the stack, pass ParameterKey=EnvName,UsePreviousValue=true.
Common errors
- Validation failure on create. A value outside
AllowedValues, or one that failsAllowedPattern, stops the operation before any resource is created. Read the error message and fix the input. - Missing required parameter. A parameter with no
Defaultmust be supplied every time. - Secrets in plain parameters.
NoEchoonly hides the value in output. It is still available to the template's resources. For secrets, prefer a Secrets Manager reference or an SSM Parameter Store value. - Too many parameters. A template with dozens of inputs is hard to use. Keep parameters for values that really differ between stacks.
Exercise
You update a stack with aws cloudformation create-change-set and omit the EnvName parameter, which has Default: dev. The stack currently uses prod. What value does the change set use, and how do you keep prod?
Show answer
The change set uses the default, dev, because an omitted parameter falls back to its Default. To keep the current value, pass ParameterKey=EnvName,UsePreviousValue=true.