How to Use Parameters in CloudFormation Templates
In AWS CloudFormation, you use
Parameters to pass values into your template at stack creation time. Define parameters in the Parameters section, then reference them in resources using Ref or Fn::GetAtt. This lets you customize your infrastructure without changing the template code.Syntax
The Parameters section defines inputs your template accepts. Each parameter has a name and properties like Type, Description, and optional Default value. You reference parameters inside resources using Ref to get their value.
- Parameters: The section where you declare inputs.
- Type: Defines the kind of input (e.g., String, Number, AWS-specific types).
- Default: Optional default value if none is provided.
- Ref: Used to access the parameter value in the template.
yaml
Parameters:
InstanceTypeParameter:
Type: String
Description: EC2 instance type
Default: t2.micro
Resources:
MyEC2Instance:
Type: AWS::EC2::Instance
Properties:
InstanceType: !Ref InstanceTypeParameter
ImageId: ami-0abcdef1234567890Example
This example shows a CloudFormation template that uses a parameter to set the EC2 instance type. When you create the stack, you can choose the instance type or use the default t2.micro. The template references the parameter with !Ref to assign the instance type.
yaml
AWSTemplateFormatVersion: '2010-09-09' Description: Simple EC2 instance with parameterized instance type Parameters: InstanceTypeParameter: Type: String Description: EC2 instance type Default: t2.micro Resources: MyEC2Instance: Type: AWS::EC2::Instance Properties: InstanceType: !Ref InstanceTypeParameter ImageId: ami-0abcdef1234567890
Output
When deployed, the EC2 instance will launch with the instance type specified by the parameter or the default t2.micro if none is provided.
Common Pitfalls
Common mistakes when using parameters include:
- Not specifying a
Typefor the parameter, causing deployment errors. - Using
Refincorrectly on resources instead of parameters. - Forgetting to provide a value when no default is set, which causes stack creation to fail.
- Using parameters for values that should be hardcoded or managed by other means.
Always validate your template and test parameter inputs before deployment.
yaml
Parameters:
InstanceTypeParameter:
Description: EC2 instance type
Resources:
MyEC2Instance:
Type: AWS::EC2::Instance
Properties:
InstanceType: !Ref MyEC2Instance # Incorrect: Ref should point to parameter, not resource
ImageId: ami-0abcdef1234567890
# Correct usage:
# InstanceType: !Ref InstanceTypeParameterQuick Reference
Here is a quick summary of key points when using parameters in CloudFormation:
| Concept | Description |
|---|---|
| Parameters Section | Defines inputs your template accepts. |
| Type | Specifies the kind of input (String, Number, AWS-specific). |
| Default | Optional default value if no input is given. |
| Ref | Used to get the parameter value inside the template. |
| No Default | Requires user to provide a value at stack creation. |
| Validation | Use AllowedValues or AllowedPattern to restrict inputs. |
Key Takeaways
Define parameters in the Parameters section with a clear Type and optional Default.
Use !Ref to access parameter values inside your resources.
Always provide a value for parameters without defaults when creating a stack.
Validate parameter inputs to avoid deployment errors.
Parameters let you customize templates without changing the code.