Parameters Section Deep Dive
What you'll learn: How to define input parameters with types, constraints, and default values to make your CloudFormation templates flexible and reusable across different environments.
Why Parameters Matter
Think of parameters like function arguments in programming—they let you pass different values into the same template without rewriting it. Instead of hardcoding "t2.micro" as an instance type, you can ask the user what size they need when creating the stack.
Core Parameter Anatomy
Each parameter in the Parameters section needs a Type (what kind of data) and can include optional constraints:
Parameters:
InstanceType:
Type: String
Default: t2.micro
AllowedValues:
- t2.micro
- t2.small
- t2.medium
Description: EC2 instance type for the web server
Essential Parameter Properties
Type defines what data you accept: String, Number, CommaDelimitedList, or AWS-specific types like AWS::EC2::KeyPair::KeyName.
Default provides a fallback value if the user doesn't specify one—great for sensible defaults that work in most cases.
AllowedValues restricts choices to a predefined list, preventing invalid inputs (like someone requesting a "t2.gigantic" instance that doesn't exist).
Description explains the parameter's purpose to anyone using your template.
Additional useful constraints include MinLength/MaxLength for strings, MinValue/MaxValue for numbers, and AllowedPattern for regex validation.
Using Parameters in Resources
Reference parameters with the !Ref intrinsic function:
Resources:
MyInstance:
Type: AWS::EC2::Instance
Properties:
InstanceType: !Ref InstanceType
Key Takeaway: Parameters transform static templates into reusable blueprints by accepting user input with built-in validation, making your infrastructure code work across dev, staging, and production with the same template file.