CloudFormation Parameters: Make your life simple

picture of clouds

Before we dive into the details of CloudFormation Parameters, lets build some context around what CloudFormation is. It is a service by Amazon, used to  model, provision and configure AWS  resources in the Cloud. You do this by creating a template, which is a a simple text file in JSON or YAML format. 

In case you are not familiar with JSON and YAML, JSON stands for JavaScript Object Notation and is lightweight data-interchange format easy for humans and machines to comprehend. Similarly, YAML ( “YAML Aint Markup Language“) is a data-serialization language  and uses indentation for formatting. This minimal syntax makes it easy to understand and write.

Lets walk thru an example to make better sense. Assume you want to create a DynamoDB table with indexes. You would have to manually provision and configure the table name,  keys, capacity units, field attributes, primary and secondary indexes and more. With CloudFormation, you specify your required AWS resources and its properties in your template and create a stack from either the CLI, API or Console.

In this post, lets look at the below Cloudformation Parameters and their attributes

CloudFormation Basics

cloudformation parameters

A CloudFormation template is composed of multiple sections – Format Version, Description, Metadata, Parameters, Mappings, Conditions, Transform, Resources and Output. Parameters, though an optional section in the template can be used to turbo charge your resource deployment game.

CloudFormation Parameters are used to pass input values to the template during run-time when creating or updating a stack. These parameters are then referenced from the Resources or Output sections in the template. Keep the following points in mind when you use parameters in your CloudFormation template.

  • A parameter can only be referenced in the context of the template it is declared in. In other words, they are not global.
  • You have to provide a value during runtime. If you decide to declare a parameter and not use it, then it has to have a default value assigned.
  • You can have a maximum of 60 parameters in a template and each one must have a unique logical name.
  • And finally, every parameter should have a type assigned to it.

CloudFormation Parameter Types

AWS CloudFormation Parameter types require a bit more attention. So, what are Cloudformation Parameter Types ?

A Parameter Type in a CloudFormation template is the data type of the parameter. This a required property of a parameter. Currently, there are 6 CloudFormation Parameter Types

Cloudformation Parameter Type
Example
Description
1. String
“InstanceType”
Defines the parameter as a literal string
2. Number
“11011”
Defined as a Number type (integer/float)
3. List
[“20”, “40”, “60”]
Defined as an Array of integers/floats separated by commas
4. CommaDelimitedList
[“apple”, “mango”, “orange”]
Defined as an Array of strings separated by commas
5. AWS-Specific Parameter Types
AWS::EC2::KeyPair::KeyName
Defined as an AWS value and VPC ID key pair
6. SSM Parameter Types
AWS::SSM::Parameter::Value
Defines a type similar to a System Manager Parameter

Now lets look at a simple exalmple of an AWS CloudFormation Parameter in JSON and YAML format.

Parameter Format for JSON

“Parameters” : {
          “ParameterLogicalID” : {
                  “Type” : “DataType”,
                  “ParameterProperty” : “value”
          }
}

Parameter Format for YAML

Parameters :
          ParameterLogicalID:
                  Type : DataType
                  ParameterProperty : value

CloudFormation Parameter Properties

When using CloudFormation Parameters, there are 11 properties you can specify for control and structure. Almost all of them are optional, except for the Type parameter. The table below lists the parameter properties and description.

Parameter Property
Required
Description
AllowedPattern
No
Regular expression denoting the permitted String type patterns
AllowedValues
No
List of Values in Array format
ConstraintDescription
No
Text to be displayed when a constraint is violated
Default
No
Value to use if no input is provided
Description
No
Textual description of the parameter not exceeding 4000 characters
MinLength
No
Smallest number of characters allowed for String types in integer format
MaxLength
No
Largest number of characters allowed for String types in integer format
MinValue
No
Smallest numeric value allowed for Number types
MaxValue
No
Largest numeric value allowed for Number types
NoEcho
No
Used to display or mask the parameter value for display
Type
Yes
Used to specify the data type for the parameter

CloudFormation Parameter Examples

If all this technical jargon is making your head spin, don’t fret! We will walk through the top 5 frequently used CloudFormation Parameter patterns. Once you get familiar with them, try tweaking them for your specific use cases.

Example 1: Create an AWS-Specific Cloudformation Parameter for an EC2 Key Pair

JSON

“Parameters” : {
          “KeyName”: {
                    “Description” : “EC2 Key Pair Name”,
                    “Type”: “AWS::EC2::KeyPair::KeyName”,
                    “ConstraintDescription” : “EC2 Key Pair must exist”
     }
}

YAML

Parameters :
          KeyName:
                    Description : EC2 Key Pair Name
                    Type: AWS::EC2::KeyPair::KeyName
                    ConstraintDescription : EC2 Key Pair must exist

Example 2: Create a CloudFormation Parameter for SSH access to an EC2 Instance

JSON

“Parameters” : {
          “SSHLocation” : {
                  “Description” : ” IP address for SSH to
                                                 the EC2 instance”,
                  “Type”: “String”,
                  “MinLength”: “9”,
                  “MaxLength”: “18”,
                  “Default”: “0.0.0.0/0”,
                  “AllowedPattern”: “(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})
                                                      \\.(\\d{1,3})/(\\d{1,2})”,
                  “ConstraintDescription”: “IP in x.x.x.x/x format.”
          }
}

YAML

Parameters :
          SSHLocation :
                  Description : IP address for SSH to
                                          the EC2 instance
                  Type: String
                  MinLength: 9
                  MaxLength: 18
                  Default: 0000/0
                  AllowedPattern: (\\d13)\\(\\d13)\\(\\d13)
                                                  \\(\\d13)/(\\d12)
                  ConstraintDescription: IP CIDR range in xxxx/x format

Example 3: Create CloudFormation Parameters for a Database

In this example, we will create 3 parameters for a DynamoDB database. One for the Database Name (DBName),One for the Database User (DBUser) and one for the Database Password (DB Password). 

This example can be used for other databases and any scenario requiring Username and Password parameters.

JSON

“Parameters” : {

“DBName”: {

“Default”: “testDynamoDB”,
“Description” : “DynamoDB database name”,
“Type”: “String”,
“MinLength”: “1”,
“MaxLength”: “64”,
“AllowedPattern” : “[a-zA-Z][a-zA-Z0-9]*”,
“ConstraintDescription” : “Must start with a letter and contain only alphanumeric characters”

};
“DBUser”: {

“NoEcho”: “true”,
“Description” : “Username for DynamoDB database”,
“Type”: “String”,
“MinLength”: “1”,
“MaxLength”: “16”,
“AllowedPattern” : “[a-zA-Z][a-zA-Z0-9]*”,
“ConstraintDescription” : “Must start with a letter and contain only alphanumeric characters”

},
“DBPassword”: {

“NoEcho”: “true”,
“Description” : “Password for DynamoDB database”,
“Type”: “String”,
“MinLength”: “8”,
“MaxLength”: “41”,
“AllowedPattern” : “[a-zA-Z0-9]*”,
“ConstraintDescription” : “Alphanumeric characters only”

}

}

YAML

Parameters :

DBName:

Default: testDynamoDB
Description : DynamoDB database name
Type: String
MinLength: 1
MaxLength: 64
AllowedPattern : [a-zA-Z][a-zA-Z0-9]*
ConstraintDescription : Must start with a letter and contain only alphanumeric characters

DBUser:

NoEcho: true
Description : Username for DynamoDB database
Type: String
MinLength: 1
MaxLength: 16
AllowedPattern : [a-zA-Z][a-zA-Z0-9]*
ConstraintDescription : Must start with a letter and contain only alphanumeric characters

DBPassword:

NoEcho: true
Description : Password for DynamoDB database
Type: String
MinLength: 8
MaxLength: 41
AllowedPattern : [a-zA-Z0-9]*
ConstraintDescription : Alphanumeric characters only

Example 4: Create a CloudFormation Parameter for an Email Address

JSON

“Parameters”: {
     “EMailAddress”: {
         “Description”: “Email address for issue notification”,
         “Type”: “String”,
         “AllowedPattern”: “([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}
               \\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))
               ([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)”,
         “ConstraintDescription”: “Enter a valid email address.”
     }
}

YAML

Parameters:
     EMailAddress:
         Description: Email address for issue notification
         Type: String
         AllowedPattern: [a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}
          \\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))
          ([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?
         ConstraintDescription: Enter a valid email address

 

Example 5: Create a CloudFormation Parameter for a SageMaker EC2 Instance Type

JSON

“Parameters” : {
     “InstanceType” : {
         “Description” : “SageMaker EC2 instance type”,
         “Type” : “String”,
         “Default” : “ml.m4.xlarge“,

         “AllowedValues” : [ “ml.m4.xlarge”, “ml.m4.4xlarge”,
                                             “ml.m4.10xlarge”,”ml.c4.xlarge”,
                                             “ml.c4.2xlarge”, “ml.c4.8xlarge”,
                                             “ml.p2.xlarge”, “ml.p2.8xlarge”,
                                             “ml.p2.16xlarge”],
         “ConstraintDescription” : “Must be a valid EC2 instance
                                                         type.”
     }
}

YAML

Parameters :
     InstanceType :
         Description : SageMaker EC2 instance type
         Type : String
         Default : ml.m4.xlarge
         AllowedValues :
                 – ml.m4.xlarge
                 – ml.m4.4xlarge
                 – ml.m4.10xlarge
                 – ml.c4.xlarge
                 – ml.c4.2xlarge
                 – ml.c4.8xlarge
                 – ml.p2.xlarge
                 – ml.p2.8xlarge
                 – ml.p2.16xlarge
         ConstraintDescription : Must be a valid EC2 instance
                                                     type

In Conclusion

Keep the following key points in mind: 

  • CloudFormation Parameters are an optional section in the template. However, using them will make your template flexible and dynamic.
  • At a minimum, you need to specify a logical id (name) and type for your parameter.
  • For AWS specific values, always use the AWS-Specific Parameter Types.
  • Though not required, using Parameter Constraints and Defaults is considered best practice.

 

Now that we covered some of the basics. See if you can provision an Amazon Redshift Cluster using AWS CloudFormation. Here are a few articles to get you started

 

Spark SQL Related Resources

AWS CloudFormation User Guide

AWS Official User guide on CloudFormation

Service Offerings by Obstkel

Get to know the AWS Cloud Services offered by Obstkel

Email us at : info@obstkel.com

Copyright 2020 © OBSTKEL LLC. All rights Reserved.
Scroll to Top