AWS CloudFormation Parameters
Jakob Jenkov |
AWS CloudFormation templates can contain parameters. Parameters can be used inside the CloudFormation template to refer to values that are provided at the time the CloudFormation template is used to create a new stack. This means that the same CloudFormation template can be used to create multiple stacks that have variations in the places where parameters are used.
Parameters Section
CloudFormation parameters are defined in their own section inside the CloudFormation template. Here is an example of a CloudFormation template outline that contains the Parameters and Resources sections:
Parameters: Resources:
The example does not contain any actual parameter definitions. I will show such examples later in this tutorial.
Parameters Example
Here is an AWS CloudFormation parameter example:
Parameters: BucketNameParam: Description: Name of the bucket Type: String Resources: MyS3Bucket: Type: "AWS::S3::Bucket" Properties: AccessControl: PublicRead BucketName: !Ref BucketNameParam
This CloudFormation template contains the CloudFormation parameter named BucketNameParam
. The
parameter is referenced down in the BucketName
property of the S3 bucket resource definition
in the Resources
section.
Referencing a Parameter
To reference a parameter defined in the Parameters
section of a CloudFormation template,
you use the CloudFormation built-in keyword (function) named !Ref
. So, to reference the
value of a parameter named MyParameter
you would write:
!Ref MyParameter
Default Parameter Values
A CloudFormation parameter can have a default value. This default value is used as parameter value in case no value is provided by the user when creating a stack using the CloudFormation template in which the parameter is defined. Or, the AWS web console will suggest the default value to the user, so it becomes easier to fill in the parameters when creating the CloudFormation stack.
Here is an example of a CloudFormation template that uses a parameter with a default value specified:
Parameters: BucketNameParam: Description: Name of the bucket Type: String Default: PrimaryUploadBucket Resources: MyS3Bucket: Type: "AWS::S3::Bucket" Properties: AccessControl: PublicRead BucketName: !Ref BucketNameParam
More Parameter Features
AWS CloudFormation parameters have several more features such as MaxValue
, MinValue
etc. which you can read more about, here:
https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/parameters-section-structure.html
Tweet | |
Jakob Jenkov |