AWS Cloudformation - Getting Started

AWS Cloudformation

 
AWS CloudFormation is a service that helps you model and set up your Amazon Web Services resources so that you can spend less time managing those resources and more time focusing on your applications that run in AWS.
 
AWS Cloudformation
 
In the given screenshot, template is a. JSON or .Yaml file with parameter definitions, resource and configuration actions. CloudFormation works as a framework for creating a new stack, updating a stack, error detection and/or rollback. Stack is basically used to configure AWS services.
 

Why CloudFormation?

 
AWS Cloudformation
 
Getting Started,
  • Log in to here
  • Enter Username and Password
  • Go to services
  • Search CloudFormation in Management & Governance
  • You will see running stacks there and have an option for creating new stack
AWS Cloudformation
 

What is a stack?

 
The CloudFormation Stack provides the ability to deploy, update and delete a template and its associated collection of resources by using the AWS Management Console, AWS Command Line Interface or APIs.
 
AWS Cloudformation
 
Before going further let’s discuss about Templates and create a sample template.
 

What are Templates?

 
A template is a JSON-formatted text file that describes your AWS infrastructure. Templates include several major sections. The Resources section is the only section that is required. You can use AWS CloudFormation's sample templates or create your own templates to describe the AWS resources and any associated dependencies or runtime parameters required to run your application.
 
AWS Cloudformation
 
The above screenshot is just a sample template. Resources is the only mandatory parameter there. Now let’s create a new sample template and create a new stack using that template. In my template I am creating a new EC2 Instance along with Security group.
 
Sample Template
 
Create a new EC2 Instance with Security Group. Here is my JSON template file, you can modify according to the need. 
  1. {  
  2.     "AWSTemplateFormatVersion""2010-09-09",  
  3.     "Description""AWS CloudFormation Sample Template EC2InstanceWithSecurityGroupSample: Create an Amazon EC2 instance running the Amazon Linux AMI. The AMI is chosen based on the region in which the stack is run. This example creates an EC2 security group for the instance to give you SSH access. **WARNING** This template creates an Amazon EC2 instance. You will be billed for the AWS resources used if you create a stack from this template.",  
  4.     "Parameters": {  
  5.         "KeyName": {  
  6.             "Description""Name of an existing EC2 KeyPair to enable SSH access to the instance",  
  7.             "Type""AWS::EC2::KeyPair::KeyName",  
  8.             "ConstraintDescription""must be the name of an existing EC2 KeyPair."  
  9.         },  
  10.         "InstanceType": {  
  11.             "Description""WebServer EC2 instance type",  
  12.             "Type""String",  
  13.             "Default""t2.small",  
  14.             "AllowedValues": ["t1.micro""t2.nano""t2.micro""t2.small""t2.medium""t2.large""m1.small""m1.medium""m1.large""m1.xlarge""m2.xlarge""m2.2xlarge""m2.4xlarge""m3.medium""m3.large""m3.xlarge""m3.2xlarge""m4.large""m4.xlarge""m4.2xlarge""m4.4xlarge""m4.10xlarge""c1.medium""c1.xlarge""c3.large""c3.xlarge""c3.2xlarge""c3.4xlarge""c3.8xlarge""c4.large""c4.xlarge""c4.2xlarge""c4.4xlarge""c4.8xlarge""g2.2xlarge""g2.8xlarge""r3.large""r3.xlarge""r3.2xlarge""r3.4xlarge""r3.8xlarge""i2.xlarge""i2.2xlarge""i2.4xlarge""i2.8xlarge""d2.xlarge""d2.2xlarge""d2.4xlarge""d2.8xlarge""hi1.4xlarge""hs1.8xlarge""cr1.8xlarge""cc2.8xlarge""cg1.4xlarge"],  
  15.             "ConstraintDescription""must be a valid EC2 instance type."  
  16.         },  
  17.         "SSHLocation": {  
  18.             "Description""The IP address range that can be used to SSH to the EC2 instances",  
  19.             "Type""String",  
  20.             "MinLength""9",  
  21.             "MaxLength""18",  
  22.             "Default""0.0.0.0/0",  
  23.             "AllowedPattern""(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})/(\\d{1,2})",  
  24.             "ConstraintDescription""must be a valid IP CIDR range of the form x.x.x.x/x."  
  25.         }  
  26.     },  
  27.     "Mappings": {  
  28.         "AWSInstanceType2Arch": {  
  29.             "t1.micro": {  
  30.                 "Arch""HVM64"  
  31.             },  
  32.             "t2.nano": {  
  33.                 "Arch""HVM64"  
  34.             },  
  35.             "t2.micro": {  
  36.                 "Arch""HVM64"  
  37.             },  
  38.             "t2.small": {  
  39.                 "Arch""HVM64"  
  40.             },  
  41.             "t2.medium": {  
  42.                 "Arch""HVM64"  
  43.             },  
  44.             "t2.large": {  
  45.                 "Arch""HVM64"  
  46.             },  
  47.             "m1.small": {  
  48.                 "Arch""HVM64"  
  49.             },  
  50.             "m1.medium": {  
  51.                 "Arch""HVM64"  
  52.             },  
  53.             "m1.large": {  
  54.                 "Arch""HVM64"  
  55.             },  
  56.             "m1.xlarge": {  
  57.                 "Arch""HVM64"  
  58.             },  
  59.             "m2.xlarge": {  
  60.                 "Arch""HVM64"  
  61.             },  
  62.             "m2.2xlarge": {  
  63.                 "Arch""HVM64"  
  64.             },  
  65.             "m2.4xlarge": {  
  66.                 "Arch""HVM64"  
  67.             },  
  68.             "m3.medium": {  
  69.                 "Arch""HVM64"  
  70.             },  
  71.             "m3.large": {  
  72.                 "Arch""HVM64"  
  73.             },  
  74.             "m3.xlarge": {  
  75.                 "Arch""HVM64"  
  76.             },  
  77.             "m3.2xlarge": {  
  78.                 "Arch""HVM64"  
  79.             },  
  80.             "m4.large": {  
  81.                 "Arch""HVM64"  
  82.             },  
  83.             "m4.xlarge": {  
  84.                 "Arch""HVM64"  
  85.             },  
  86.             "m4.2xlarge": {  
  87.                 "Arch""HVM64"  
  88.             },  
  89.             "m4.4xlarge": {  
  90.                 "Arch""HVM64"  
  91.             },  
  92.             "m4.10xlarge": {  
  93.                 "Arch""HVM64"  
  94.             },  
  95.             "c1.medium": {  
  96.                 "Arch""HVM64"  
  97.             },  
  98.             "c1.xlarge": {  
  99.                 "Arch""HVM64"  
  100.             },  
  101.             "c3.large": {  
  102.                 "Arch""HVM64"  
  103.             },  
  104.             "c3.xlarge": {  
  105.                 "Arch""HVM64"  
  106.             },  
  107.             "c3.2xlarge": {  
  108.                 "Arch""HVM64"  
  109.             },  
  110.             "c3.4xlarge": {  
  111.                 "Arch""HVM64"  
  112.             },  
  113.             "c3.8xlarge": {  
  114.                 "Arch""HVM64"  
  115.             },  
  116.             "c4.large": {  
  117.                 "Arch""HVM64"  
  118.             },  
  119.             "c4.xlarge": {  
  120.                 "Arch""HVM64"  
  121.             },  
  122.             "c4.2xlarge": {  
  123.                 "Arch""HVM64"  
  124.             },  
  125.             "c4.4xlarge": {  
  126.                 "Arch""HVM64"  
  127.             },  
  128.             "c4.8xlarge": {  
  129.                 "Arch""HVM64"  
  130.             },  
  131.             "g2.2xlarge": {  
  132.                 "Arch""HVMG2"  
  133.             },  
  134.             "g2.8xlarge": {  
  135.                 "Arch""HVMG2"  
  136.             },  
  137.             "r3.large": {  
  138.                 "Arch""HVM64"  
  139.             },  
  140.             "r3.xlarge": {  
  141.                 "Arch""HVM64"  
  142.             },  
  143.             "r3.2xlarge": {  
  144.                 "Arch""HVM64"  
  145.             },  
  146.             "r3.4xlarge": {  
  147.                 "Arch""HVM64"  
  148.             },  
  149.             "r3.8xlarge": {  
  150.                 "Arch""HVM64"  
  151.             },  
  152.             "i2.xlarge": {  
  153.                 "Arch""HVM64"  
  154.             },  
  155.             "i2.2xlarge": {  
  156.                 "Arch""HVM64"  
  157.             },  
  158.             "i2.4xlarge": {  
  159.                 "Arch""HVM64"  
  160.             },  
  161.             "i2.8xlarge": {  
  162.                 "Arch""HVM64"  
  163.             },  
  164.             "d2.xlarge": {  
  165.                 "Arch""HVM64"  
  166.             },  
  167.             "d2.2xlarge": {  
  168.                 "Arch""HVM64"  
  169.             },  
  170.             "d2.4xlarge": {  
  171.                 "Arch""HVM64"  
  172.             },  
  173.             "d2.8xlarge": {  
  174.                 "Arch""HVM64"  
  175.             },  
  176.             "hi1.4xlarge": {  
  177.                 "Arch""HVM64"  
  178.             },  
  179.             "hs1.8xlarge": {  
  180.                 "Arch""HVM64"  
  181.             },  
  182.             "cr1.8xlarge": {  
  183.                 "Arch""HVM64"  
  184.             },  
  185.             "cc2.8xlarge": {  
  186.                 "Arch""HVM64"  
  187.             }  
  188.         },  
  189.         "AWSInstanceType2NATArch": {  
  190.             "t1.micro": {  
  191.                 "Arch""NATHVM64"  
  192.             },  
  193.             "t2.nano": {  
  194.                 "Arch""NATHVM64"  
  195.             },  
  196.             "t2.micro": {  
  197.                 "Arch""NATHVM64"  
  198.             },  
  199.             "t2.small": {  
  200.                 "Arch""NATHVM64"  
  201.             },  
  202.             "t2.medium": {  
  203.                 "Arch""NATHVM64"  
  204.             },  
  205.             "t2.large": {  
  206.                 "Arch""NATHVM64"  
  207.             },  
  208.             "m1.small": {  
  209.                 "Arch""NATHVM64"  
  210.             },  
  211.             "m1.medium": {  
  212.                 "Arch""NATHVM64"  
  213.             },  
  214.             "m1.large": {  
  215.                 "Arch""NATHVM64"  
  216.             },  
  217.             "m1.xlarge": {  
  218.                 "Arch""NATHVM64"  
  219.             },  
  220.             "m2.xlarge": {  
  221.                 "Arch""NATHVM64"  
  222.             },  
  223.             "m2.2xlarge": {  
  224.                 "Arch""NATHVM64"  
  225.             },  
  226.             "m2.4xlarge": {  
  227.                 "Arch""NATHVM64"  
  228.             },  
  229.             "m3.medium": {  
  230.                 "Arch""NATHVM64"  
  231.             },  
  232.             "m3.large": {  
  233.                 "Arch""NATHVM64"  
  234.             },  
  235.             "m3.xlarge": {  
  236.                 "Arch""NATHVM64"  
  237.             },  
  238.             "m3.2xlarge": {  
  239.                 "Arch""NATHVM64"  
  240.             },  
  241.             "m4.large": {  
  242.                 "Arch""NATHVM64"  
  243.             },  
  244.             "m4.xlarge": {  
  245.                 "Arch""NATHVM64"  
  246.             },  
  247.             "m4.2xlarge": {  
  248.                 "Arch""NATHVM64"  
  249.             },  
  250.             "m4.4xlarge": {  
  251.                 "Arch""NATHVM64"  
  252.             },  
  253.             "m4.10xlarge": {  
  254.                 "Arch""NATHVM64"  
  255.             },  
  256.             "c1.medium": {  
  257.                 "Arch""NATHVM64"  
  258.             },  
  259.             "c1.xlarge": {  
  260.                 "Arch""NATHVM64"  
  261.             },  
  262.             "c3.large": {  
  263.                 "Arch""NATHVM64"  
  264.             },  
  265.             "c3.xlarge": {  
  266.                 "Arch""NATHVM64"  
  267.             },  
  268.             "c3.2xlarge": {  
  269.                 "Arch""NATHVM64"  
  270.             },  
  271.             "c3.4xlarge": {  
  272.                 "Arch""NATHVM64"  
  273.             },  
  274.             "c3.8xlarge": {  
  275.                 "Arch""NATHVM64"  
  276.             },  
  277.             "c4.large": {  
  278.                 "Arch""NATHVM64"  
  279.             },  
  280.             "c4.xlarge": {  
  281.                 "Arch""NATHVM64"  
  282.             },  
  283.             "c4.2xlarge": {  
  284.                 "Arch""NATHVM64"  
  285.             },  
  286.             "c4.4xlarge": {  
  287.                 "Arch""NATHVM64"  
  288.             },  
  289.             "c4.8xlarge": {  
  290.                 "Arch""NATHVM64"  
  291.             },  
  292.             "g2.2xlarge": {  
  293.                 "Arch""NATHVMG2"  
  294.             },  
  295.             "g2.8xlarge": {  
  296.                 "Arch""NATHVMG2"  
  297.             },  
  298.             "r3.large": {  
  299.                 "Arch""NATHVM64"  
  300.             },  
  301.             "r3.xlarge": {  
  302.                 "Arch""NATHVM64"  
  303.             },  
  304.             "r3.2xlarge": {  
  305.                 "Arch""NATHVM64"  
  306.             },  
  307.             "r3.4xlarge": {  
  308.                 "Arch""NATHVM64"  
  309.             },  
  310.             "r3.8xlarge": {  
  311.                 "Arch""NATHVM64"  
  312.             },  
  313.             "i2.xlarge": {  
  314.                 "Arch""NATHVM64"  
  315.             },  
  316.             "i2.2xlarge": {  
  317.                 "Arch""NATHVM64"  
  318.             },  
  319.             "i2.4xlarge": {  
  320.                 "Arch""NATHVM64"  
  321.             },  
  322.             "i2.8xlarge": {  
  323.                 "Arch""NATHVM64"  
  324.             },  
  325.             "d2.xlarge": {  
  326.                 "Arch""NATHVM64"  
  327.             },  
  328.             "d2.2xlarge": {  
  329.                 "Arch""NATHVM64"  
  330.             },  
  331.             "d2.4xlarge": {  
  332.                 "Arch""NATHVM64"  
  333.             },  
  334.             "d2.8xlarge": {  
  335.                 "Arch""NATHVM64"  
  336.             },  
  337.             "hi1.4xlarge": {  
  338.                 "Arch""NATHVM64"  
  339.             },  
  340.             "hs1.8xlarge": {  
  341.                 "Arch""NATHVM64"  
  342.             },  
  343.             "cr1.8xlarge": {  
  344.                 "Arch""NATHVM64"  
  345.             },  
  346.             "cc2.8xlarge": {  
  347.                 "Arch""NATHVM64"  
  348.             }  
  349.         },  
  350.         "AWSRegionArch2AMI": {  
  351.             "us-east-1": {  
  352.                 "HVM64""ami-0080e4c5bc078760e",  
  353.                 "HVMG2""ami-0aeb704d503081ea6"  
  354.             },  
  355.             "us-west-2": {  
  356.                 "HVM64""ami-01e24be29428c15b2",  
  357.                 "HVMG2""ami-0fe84a5b4563d8f27"  
  358.             },  
  359.             "us-west-1": {  
  360.                 "HVM64""ami-0ec6517f6edbf8044",  
  361.                 "HVMG2""ami-0a7fc72dc0e51aa77"  
  362.             },  
  363.             "eu-west-1": {  
  364.                 "HVM64""ami-08935252a36e25f85",  
  365.                 "HVMG2""ami-0d5299b1c6112c3c7"  
  366.             },  
  367.             "eu-west-2": {  
  368.                 "HVM64""ami-01419b804382064e4",  
  369.                 "HVMG2""NOT_SUPPORTED"  
  370.             },  
  371.             "eu-west-3": {  
  372.                 "HVM64""ami-0dd7e7ed60da8fb83",  
  373.                 "HVMG2""NOT_SUPPORTED"  
  374.             },  
  375.             "eu-central-1": {  
  376.                 "HVM64""ami-0cfbf4f6db41068ac",  
  377.                 "HVMG2""ami-0aa1822e3eb913a11"  
  378.             },  
  379.             "eu-north-1": {  
  380.                 "HVM64""ami-86fe70f8",  
  381.                 "HVMG2""ami-32d55b4c"  
  382.             },  
  383.             "ap-northeast-1": {  
  384.                 "HVM64""ami-00a5245b4816c38e6",  
  385.                 "HVMG2""ami-09d0e0e099ecabba2"  
  386.             },  
  387.             "ap-northeast-2": {  
  388.                 "HVM64""ami-00dc207f8ba6dc919",  
  389.                 "HVMG2""NOT_SUPPORTED"  
  390.             },  
  391.             "ap-northeast-3": {  
  392.                 "HVM64""ami-0b65f69a5c11f3522",  
  393.                 "HVMG2""NOT_SUPPORTED"  
  394.             },  
  395.             "ap-southeast-1": {  
  396.                 "HVM64""ami-05b3bcf7f311194b3",  
  397.                 "HVMG2""ami-0e46ce0d6a87dc979"  
  398.             },  
  399.             "ap-southeast-2": {  
  400.                 "HVM64""ami-02fd0b06f06d93dfc",  
  401.                 "HVMG2""ami-0c0ab057a101d8ff2"  
  402.             },  
  403.             "ap-south-1": {  
  404.                 "HVM64""ami-0ad42f4f66f6c1cc9",  
  405.                 "HVMG2""ami-0244c1d42815af84a"  
  406.             },  
  407.             "us-east-2": {  
  408.                 "HVM64""ami-0cd3dfa4e37921605",  
  409.                 "HVMG2""NOT_SUPPORTED"  
  410.             },  
  411.             "ca-central-1": {  
  412.                 "HVM64""ami-07423fb63ea0a0930",  
  413.                 "HVMG2""NOT_SUPPORTED"  
  414.             },  
  415.             "sa-east-1": {  
  416.                 "HVM64""ami-05145e0b28ad8e0b2",  
  417.                 "HVMG2""NOT_SUPPORTED"  
  418.             },  
  419.             "cn-north-1": {  
  420.                 "HVM64""ami-053617c9d818c1189",  
  421.                 "HVMG2""NOT_SUPPORTED"  
  422.             },  
  423.             "cn-northwest-1": {  
  424.                 "HVM64""ami-0f7937761741dc640",  
  425.                 "HVMG2""NOT_SUPPORTED"  
  426.             }  
  427.         }  
  428.     },  
  429.     "Resources": {  
  430.         "EC2Instance": {  
  431.             "Type""AWS::EC2::Instance",  
  432.             "Properties": {  
  433.                 "InstanceType": {  
  434.                     "Ref""InstanceType"  
  435.                 },  
  436.                 "SecurityGroups": [{  
  437.                     "Ref""InstanceSecurityGroup"  
  438.                 }],  
  439.                 "KeyName": {  
  440.                     "Ref""KeyName"  
  441.                 },  
  442.                 "ImageId": {  
  443.                     "Fn::FindInMap": ["AWSRegionArch2AMI", {  
  444.                         "Ref""AWS::Region"  
  445.                     }, {  
  446.                         "Fn::FindInMap": ["AWSInstanceType2Arch", {  
  447.                             "Ref""InstanceType"  
  448.                         }, "Arch"]  
  449.                     }]  
  450.                 }  
  451.             }  
  452.         },  
  453.         "InstanceSecurityGroup": {  
  454.             "Type""AWS::EC2::SecurityGroup",  
  455.             "Properties": {  
  456.                 "GroupDescription""Enable SSH access via port 22",  
  457.                 "SecurityGroupIngress": [{  
  458.                     "IpProtocol""tcp",  
  459.                     "FromPort""22",  
  460.                     "ToPort""22",  
  461.                     "CidrIp": {  
  462.                         "Ref""SSHLocation"  
  463.                     }  
  464.                 }]  
  465.             }  
  466.         }  
  467.     },  
  468.     "Outputs": {  
  469.         "InstanceId": {  
  470.             "Description""InstanceId of the newly created EC2 instance",  
  471.             "Value": {  
  472.                 "Ref""EC2Instance"  
  473.             }  
  474.         },  
  475.         "AZ": {  
  476.             "Description""Availability Zone of the newly created EC2 instance",  
  477.             "Value": {  
  478.                 "Fn::GetAtt": ["EC2Instance""AvailabilityZone"]  
  479.             }  
  480.         },  
  481.         "PublicDNS": {  
  482.             "Description""Public DNSName of the newly created EC2 instance",  
  483.             "Value": {  
  484.                 "Fn::GetAtt": ["EC2Instance""PublicDnsName"]  
  485.             }  
  486.         },  
  487.         "PublicIP": {  
  488.             "Description""Public IP address of the newly created EC2 instance",  
  489.             "Value": {  
  490.                 "Fn::GetAtt": ["EC2Instance""PublicIp"]  
  491.             }  
  492.         }  
  493.     }  
Let’s create a new stack and ingest the template file. Go to the CloudFormation and click on Create stack.
 
AWS Cloudformation
 
As my template is ready, so I am choosing the Template is ready option and the template I have in my local system.  I am choosing to upload a template file and browse template file and click Next.
 
AWS Cloudformation
 
Provide a Stack name select instance type and key name in parameters and click next. If you don’t know how to create a EC2 Key Pair, I'm going to explain that in the next article. Click Next.
 
AWS Cloudformation
 
I am keeping all default configurations there and click Next.
 
AWS Cloudformation
 
Review all configurations before hitting Create stack. If everything looks good then click Create stack button.
 
AWS Cloudformation
 
As you can see your stack creation is in progress, You have to wait  a little bit until stack completes.
 
AWS Cloudformation
 
Once you see Create-Complete, that means your stack is initiated and EC2 Instance is successfully created and running.
 
Go to services and click on EC2 Instance to check the instance and you can see running instances there.
 
AWS Cloudformation
 
As you can see 1 instance is running. Click on Running instances.
 
AWS Cloudformation
 
Here you can see new created instance is running successfully, if you click on instance then you can see the associated Security Group and IAMRole.
 
AWS Cloudformation
 

Conclusion

 
In this article, we have learned about the Amazon Web Services Cloud Formation services and how to create templates and ingest that in a stack, and how to setup a new Ec2 Instance along with Role and Security Group.


Similar Articles