Read more

Best Practices for Creating Amazon CloudFormation Templates




Amazon CloudFormation templates are widely used in the AWS cloud for environment creation by the IT and application teams.  We have been helping enterprises with Amazon CFT based automation for years and following are some of the best practices to follow while creating Amazon CFT Templates. This article compiles the CFT practices /pointers used by us for provisioning complex large scale environments on AWS, not all of them will be applicable for all use cases.
Practice #1: Version your CloudFormation templates
CloudFormation template should be commenced with a template format version and description such that it could be artifact-able with version control tools like Git, SVN, CVS, etc.,
Example:
“AWSTemplateFormatVersion”: “2010-09-09”,
    “Description”: “AWS CloudFormation Template for CustomerXYZ Version 5.0”,
Practice #2: Use input parameters
Input parameters section should be developed in scope with getting values from the end users of the CFT for environments, SSH Key Pairs, EC2 instance types, RDS instance types, ElastiCache node types, etc., and this makes the entire CloudFormation template configurable and maintainable.
Example:
“WebTierKeyName”: {
            “Description”: “Name of an existing EC2 KeyPair to enable SSH access to the WebTier”,
            “Type”: “String”,
            “MinLength”: “1”,
            “MaxLength”: “64”,
            “Default”: “testkey”,
            “AllowedPattern”: “[-_ a-zA-Z0-9]*”,
            “ConstraintDescription”: “can contain only alphanumeric characters, spaces, dashes and underscores.”
Practice #3: AMI ID should be a parameter in inputs section
AMI ID should be asked as an input parameter from the end users for launching EC2 instances. It is highly recommended not to hard code it inside your template and make it configurable dynamically.
Example:
“NatAMIID”: {
            “Type”: “String”,
            “Default”: “ami-XXXXXXXX”,
            “Description”: “The AMIID of NAT Instance”
        },
Practice #4: Mention the AMI mappings
AMI Mappings should be included in the CloudFormation template to validate the respective AMI(s) in the region(s) specified else it will take default AMI for the region (which is not advisable).
Example:
“Mappings”: {
        “RegionMap”: {
            “us-east-1”: {
                “AMI”: “ami-XXXXXXXX”
            },
“us-west-2”: {
                “AMI”: “ami-XXXXXXXX”
            },
Practice #5: Use WaitConditionHandle, WaitCondition, DependsOn wherever applicable
When creating a large multi-tiered infrastructure using CFT on AWS, there are times when the order of the resource launch is important. For Example WaitCondition that waits for the desired number of instances in a web server group.
Using the AWS::CloudFormation::WaitCondition and AWS::CloudFormation::WaitConditionHandle resources, a wait condition can be placed with in a template to make AWS CloudFormation pause the creation of the stack and wait for a signal before it continues to create the stack.
We can specify that the creation of a specific resource to follow another using the “DependsOn” attribute. On adding a DependsOn attribute to a resource, it is created only after the creation of the resource specified in the DependsOn attribute.
Example:
“WebServerGroup” : {
   “Type” : “AWS::AutoScaling::AutoScalingGroup”,
   “Properties” : {
      “AvailabilityZones” : { “Fn::GetAZs” : “” },
      “LaunchConfigurationName” : { “Ref” : “LaunchConfig” },
      “MinSize” : “1”,
      “MaxSize” : “5”,
      “DesiredCapacity” : { “Ref” : “WebServerCapacity” },
      “LoadBalancerNames” : [ { “Ref” : “ElasticLoadBalancer” } ]
   }
},
“WaitHandle” : {
   “Type” : “AWS::CloudFormation::WaitConditionHandle”
},
“WaitCondition” : {
   “Type” : “AWS::CloudFormation::WaitCondition”,
   “DependsOn” : “WebServerGroup”,
   “Properties” : {
      “Handle”  : { “Ref” : “WaitHandle” },
      “Timeout” : “300”,
      “Count”   : { “Ref” : “WebServerCapacity” }