Total Pageviews


December 15, 2018

AWS DevOps - Part 1 - Provisioning an AWS EC2 Instance in different ways

by 4hathacker  |  in AWS DevOps at  1:46 AM

Hi folks!!!

This is the era of DevOps. After Waterfall and Agile, DevOps has appeared as a never-ending process of continual improvement in application development and deployment lifecycles. In this series of articles, we will be discussing a number of DevOps scenarios with AWS and other tools.

With this first post, we will be starting a DevOps learning journey with AWS. In this article, we will be covering a very easy day to day job in a DevOps engineer's life while working with AWS. Most of the cloud infrastructure is all about Compute, Network and Storage. If I combine all these aspects into one stage, it will bring us to an EC2 instance. AWS Elastic Compute Cloud or EC2 service is a way to launch your own virtual infra with Compute, Network and Storage capabilities. With EC2 service, we can launch virtual machines over cloud which are called AWS EC2 Instances. There are a number of steps to launch an EC2 instance via the AWS Management Console.

These includes:
1. Choose AMI - A number of OS images are defined with pre-built configurations by the AWS known as AMIs. Select the image as per requirement.
2. Choose Instance Type - For different use cases, AWS has a wide variety of instances e.g. t2.micro, m4.large, c5.xlarge, etc.
3. Configure Instance - A number of basic and advance configurations can be provided to EC2 instance like no. of instances to be launched, tenancy, virtual network (vpc), shutdown behavior, etc.
4. Add Storage - Storage matters a lot and options provided are EBS volumes or Instance Store volumes.
5. Add Tags - For efficient management of EC2 resource, tagging can be done with a meaningful <K,V> pair association.
6. Configure Security Groups - Last but not the least, AWS takes care of secure access to EC2 instances using Security Group Inbound/Outbound rules well defined with ports, protocols and IP addresses.
7. Review - This is the final step to review all the above mentioned actions before launching an EC2 instance.

Feel free to launch your own instance with AWS Free tier account following the above steps. Also refer the aws documentation for help.

Till now, we have discussed the most common way of launching an EC2 instance. However, this is not the best way to maintain, launch, create, stop or terminate instances using the AWS Management Console. Lets find another way out. One way is to use AWS CLI. AWS CLI is good for simple commands and here we have some parameters to pass as arguments which will again consume more time. There are a number of configuration management tools like Chef, Puppet, Ansible, etc. for handling orchestration, provisioning and automation for AWS Infrastructure. We went through the basics of Ansible in some of the earlier articles. Lets create an ansible-playbook to provision an EC2 instance and check whether this is efficient enough or not.

To launch an EC2 instance using Ansible, there are a number of prerequisites:
1. python 2.7 and boto library

2. ansible >= 2.6.0 version

1. Create a ~/.boto file to save the access key and security access key information that Ansible will use internally to connect to your AWS account user.

2. Then, create a yaml format ansible playbook file with details regarding your EC2 instance launch.

The vars (variable) section includes region, instance_type, ami, keypair and security group details.  Notice that, we can configure all the details that are present in the AWS Management Console for EC2 Instance. However, these details are enough to launch an instance in a default VPC providing public-ip on its own. To stop/terminate the instance, we can add a rollback ansible playbook.

We found this is much better as we have rolled out a few steps like:
- Logging in to AWS Console
- Dashboard Clicks
- Time Consumption for request-response

Is there any way out, which can be better than this? I have heard about CloudFormation which is known as IaaC in AWS. IaaC is Infrastructure as Code. We have to write a template in JSON or YAML format and AWS will save it in S3. Lets see how we can launch an instance using CloudFormation. There is only one mandatory section in CloudFormation Template i.e., Resources. Defining only the ImageId w.r.t. region and instance-type will create an EC2 instance with public-ip in a default VPC subnet.

The advantage with CloudFormation is the quick rollback. We just have to run one command with Stack name as argument and all the resources will be dismantled.

There are some more ways for launching and managing EC2 instances like AWS Lambdas with boto3 library and AWS SDK with boto3. In the upcoming articles, we will cover most of the DevOps tasks using CloudFormation, AWS Lambdas, AWS SAM, Ansible, etc.

Note: In this article, we have analyzed different ways for provisioning an EC2 instance. There might be the case, if we have not followed the best practices. 


Like Our Facebook Page

Nitin Sharma's DEV Profile
Proudly Designed by 4hathacker.