Sunday, 26 July 2020

CloudFormation

It is a one of the good feature in AWS cloud computing.

Scenario: when you are having to create multiple AWS resources in a short while and create and stop when its required is a bit tedious task. To manage our schedule and creating all the AWS resources every time is bit time taking activity.

So, to save our time and create AWS resources with a simple template where you can write once and use it many times when there is a similar requirement or you can use the same template by modifying the template according to your task.This template is called "AWS cloudformation template".

This template you can write it in either JSON or YAML language.

This template has the below features.

  1. Simplify infrastructure management:For Ex: you have a web application which required a database and auto scaling features and load balancing tasks.To create all the AWS resources , you need to spend bit more time and you need to connect all together to achieve the required benefits.But When you have the AWS cloudformation template is ready with all the AWS resources properties and which have the dependencies on other AWS resources, its a one time task , with this template you just create and execute the stack , this Stack will take care all the resources creation including it dependencies.
  2. Quickly replicate your infrastructure:When you want to replicate your app in multiple regions, you have the overhead task that you have to replicate all the AWS resources along with your app and if you have app in multiple regions you can have an advantage that if one region fails other region will be used for your app. So, With AWS cloud-formation template , you can just change the regions ans AZs info and use the same template to provision the resources with minimal amount of time.
  3. Easily Control and track changes to your infrastructure:Since the template is in text file format, you can easily compare and do the changes easily.Also you can rollback the changes if your AWS resources are not functioning properly with your template changes. You can maintain version control tool to track the template versions to easy track.
Template:
This file is in various extenstions  JSON,YAML,.template or .txt file .But this is written in either json or Yaml scripting laungage.

For ex JSON:(Source : AWS Documentation)
{
  "AWSTemplateFormatVersion" : "2010-09-09",
  "Description" : "A sample template",
  "Resources" : {
    "MyEC2Instance" : {
      "Type" : "AWS::EC2::Instance",
      "Properties" : {
        "ImageId" : "ami-0ff8a91507f77f867",
        "InstanceType" : "t2.micro",
        "KeyName" : "testkey",
        "BlockDeviceMappings" : [
          {
            "DeviceName" : "/dev/sdm",
            "Ebs" : {
              "VolumeType" : "io1",
              "Iops" : "200",
              "DeleteOnTermination" : "false",
              "VolumeSize" : "20"
            }
          }
        ]
      }
    }
  }
}

For ex YAML(Source AWS Documentation)

AWSTemplateFormatVersion: "2010-09-09"
Description: A sample template
Resources:
  MyEC2Instance:
    Type: "AWS::EC2::Instance"
    Properties:
      ImageId: "ami-0ff8a91507f77f867"
      InstanceType: t2.micro
      KeyName: testkey
      BlockDeviceMappings:
        -
          DeviceName: /dev/sdm
          Ebs:
            VolumeType: io1
            Iops: 200
            DeleteOnTermination: false
            VolumeSize: 20


Stacks: You created a template and which includes all the AWS resources , AWS treat it is Stack. Where you can create, update, delete when and where its required.

Change sets: This set has a changes which you want to update the Running resources of the stack which you already created.For ex, If you want to update a tag of the AWS S3 bucket , which was used in various places and your change will affect if you update in between.

So, If you use the Change sets , it will take care of the changing the respective places where its required by taking a backing up your old changes. This will be smooth handling of the AWS resources updation.

How AWS cloud formation works:

Make a note that , when you create a stake with your template , if any resource fails to create , the status of the stack will be failed with automatic roll back failed feature.which will delete the resources which are created to save the cost and unnecessary resources.



Updating stack with change sets:

Source: AWS official documentation







Template basics:
The Resources object contains list of resources objects,A resource declaration contains the list of resources attributes which are themselves declared as child objects.
Type: Must have a resource type means what type of AWS resource you want to create.

Syntax: AWS::productIdentifier::ResourceType

For ex, for EC2 creation , we need to write as AWS::EC2::Instance.

Resource declarations uses a properties attribute to specify the information used to create the resource.Sometimes we no need to specify access for S3 buckets as it will take automatically default access like this for some of the AWS resources we no need to provide all the parameters which required , AWS stack will set default attributes for the resources.

Fn::GetAtt: Takes two names , one is logical name of the resource and the name of the attribute you want to retrive.

Ref: This is a function , where you can refer the created resource from which you can assign the values to other AWS resource by referring to the created AWS resource.

For some of the parameters like AWS AMI , we need to specify the region in the template where you want launch EC2 in that region.If you have no idea where to launch EC2 instance , you can specify a conditional logic wherever you launch the EC2 it can take that region to create AM,because as we know AMIs are region specific.

Fn:FindInMap: Which will be used to map the name of the parameter with a value and this will used in case you want to create AMIs with region specific.

Fn:Join: takes two parameters, a delimiter that separates a value you want to concatenate and an array of values you want them to appear in an order.

Whenever you update the stack with your parameters there is a function: cfn:hup:deamon will be executed for every 15 mins to pickup the change. So usually it will take 15 mins of time to get your stack updated.

When you update the AWS resources manually in AWS console , make sure that the same resource parameters are changes parallely, so that it will be in sync with your updated stack.

Updating autoscaling groups

Please note that updating EC2 instance will take an immediate impact as cft-hup function will sync immediately but Updating auto scaling group will not take affect immediately because cft-hup deamon will be running independently on each EC2 instance and it will take time to get it effect with small duration.


Before updating the template analyse few parameters as below

  1. Whether the change will effect impact any running services like alarm to your application, because when these kind of services are implemented in your app, it may impact during updating of the stack.
  2. Consider the resources are mutable ot immutable , so that it will not change immediate and takes some amount of time to affect the changes.
Lets see an example
Create below AWS resources with template and execute the stack to create the resources as per the template.

AWS resources
  • VPC
  • Subnets(Private and public)
  • Internet gateway
  • Private and Public route tables
  • NAT gateway
How to 

Open AWS management console , Search for Cloud formation, and select "Create Stack"



The window appears as below


Here you will have multiple options
  • If you have uploaded your template in S3 bucket , you can use that URL to create the the stack.
  • If not you can upload the template by selecting "upload a template file"
When you upload a template , by default it will upload to S3 bucket and the same URL you will get as below

S3 Bucket:



Select "View in Designer", you will architect diagram to see your resources how they will connect each other once your stack creates, In my case as below


Select "Create Stack"


Select Desired AZs


Update Keypair name and NAT instance details according to your convenient and select "Next".
Under "Review Form" you can see AWS resources which we are going to create from the template.


Verify and select "Create stack button" if you are OK else update the template if something is missing.


The option: Rollback on failure will be used when stack creation fails , it automatically rollback the resources which it created.This will save the time and money without creating resources.

aws-vpc.template , which is written in YAML.


When you select "Creare Stack", it will start creating all the resources ,The time taken to create the AWS resources is depends on Resrources.

While creating stack , you can see the stack creation progress and status of the stack.

Under resources: You can see what are the resources are started creating and resources are completed creating.Based on that you can figure out the resources are created . Once done you can see status as "Creation_complete" else "creation_failed" with specific reason.

In my case it took just 5 mins and the stack is ready.

You can see below pictures for my case.

VPC:
Subnets(private and Public)


Route tables


NAT Gateway


NACL


Internet gateway



So,Now you can launch EC2 instances in Public and private subnets and communicate each other by using the keypair which you have used in your template.

Hope this helps in understanding of the cloud formation concept how and why we are using in AWS. 


Thank you for reading!!!!👍👍👍👍👍👍👍👍👍👍