DevOps Engineer Masters Program (4 Blogs) Become a Certified Professional
AWS Global Infrastructure

DevOps

Topics Covered
  • DevOps (16 Blogs)
  • Mastering Git and GitHub (7 Blogs)
  • Docker (8 Blogs)
  • DevOps Engineer Masters Program (4 Blogs)
SEE MORE

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-widget-banner-bg

Ansible for AWS – Managing Cloud Made Easy

1.8K Views
8 / 8 Blog from Ansible

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assement report and video recording

Companies have invested a large amount of time and money developing and installing software to improve their operations. The introduction to cloud computing offered their business to access software on the internet as service which proved to be more efficient and safe. Integrating an IT automation tool like Ansible which will easily provision and manage your cloud infrastructure like AWS is like hitting the jackpot. And that’s what we’re going to talk about in this Ansible for AWS blog.

Agenda:

If you wish to master DevOps, this course would be your go-to option.

Why Companies Migrate To The Cloud?

As mentioned earlier, Could Computing lets companies access servers like software over the internet. To make it clear, Cloud Computing is like plugging into a central power grid instead of generating your own power. Cloud has become the new normal and this ends up saving a lot of time and money. Let’s have a look at a few advantages of why companies migrate to the cloud.

1. Flexibility:

Business growth is never static. Cloud-based services are suitable for growing and fluctuating business demands. A feature to scale up and scale down your deployment based on the requirement makes it very flexible.

2. Disaster Recovery:

Every business should have invested in disaster recovery. Every fortune company ends up investing a ton lot on disaster recovery. Startups and low budget companies lack the money and the required skill for this and are unable to have a proper functional disaster recovery trait. Cloud provides disaster recovery solutions for the customers to develop robust and cost-effective plans.

3. Automatic Software Updates:

As you already know, the cloud is the service provided by the internet and hence all the servers are out of your reach or rather not your headache. Suppliers take care of them which includes updating when required and running regular security check-ups. This again ends up saving a lot of time and money.

4. Reduced Costs:

Establishing a data center from scratch can get expensive. Running and maintaining adds up to the expenses. You need the right technology, right hardware, right staff with the right knowledge and experience which just sounds like a lot of work to me. Also, not very promising, there are a million ways this could go wrong. Migrating to the cloud gives you this plus point.

5. Scalability:

The traditional way of planning for unexpected growth is to purchase and keep additional servers, storage, and licenses. It may take years before you actually use them. Cloud platforms allow you to scale up these resources as in when needed. This dynamic scaling goes perfectly for unpredictable growth.

6. Data Security:

Most of the times, it’s better to keep your data on the cloud over storing them on a physical device like laptops or hard disks. There are high chances of these physical devices getting stolen or shattered. Cloud allows you to remotely either remove the data or transfer them to another server making sure that data remains intact and safe.

7. Increased Collaboration:

Using cloud platforms allows the team to access, edit and share documents anytime, anywhere. They are able to work together hence increasing the efficiency. This also provides real-time and transparent updates.

Ansible Features

Ansible has some unique features and when such features collaborate with Amazon Web Services, leaves a mark. Let’s have a look at these incredible features:

  1. Ansible is based on an agentless architecture, unlike Chef and Puppet
  2. Ansible accesses its host through SSH which is makes the communication between servers and hosts feel like a snap
  3. No custom security infrastructure is needed
  4. Configuring playbooks and modules is super easy as it follows YAML format
  5. Has a wide range of modules for its customers
  6. Allows complete configuration management, orchestration, and deployment capability
  7. Ansible Vault keeps the secrets safe

Why Use Ansible For AWS?

Now that we’ve gone through the benefits of using a Cloud Platform like AWS and unique features of Ansible, let’s have a look at the magic created by integrating these two legends. 

1. Cloud As Group Of Services

Cloud is not just a group of servers on someone else’s data center but much more than that. You’ll realize that once you’ve deployed your services on it. There are many services available that let you rapidly deploy and scale your applications. Ansible automation helps you manage your AWS environment like a group of services rather than using them as a group of servers.

2. Ansible Modules Supporting AWS

Ansible is used to define, deploy and manage a wide variety of services. Most complicated AWS environments can be provisioned very easily using a playbook. The best feature is, you create a server-host connection and then run the playbook on just one system and provision multiple other systems with an option to scale up and scale down as per requirement.

Ansible has hundreds of modules supporting AWS and some of them include:

  • Autoscaling groups
  • CloudFormation
  • CloudTrail
  • CloudWatch
  • DynamoDB
  • ElastiCache
  • Elastic Cloud Compute (EC2)
  • Identity Access Manager (IAM)
  • Lambda
  • Relational Database Service (RDS)
  • Route53
  • Security Groups
  • Simple Storage Service (S3)
  • Virtual Private Cloud (VPC)
  • And many more

3. Dynamic Inventory

In a development environment, hosts keep spinning up and shutting down with diverse business requirements. In such a case, using static inventory might not be sufficient. Such situations call for using Dynamic Inventory. This lets you map hosts based on groups provided by inventory scripts, unlike normal inventory which forces you to map hosts manually which is very tedious.

4. Safe Automation

Assume that you have a team of 5 people and each of them has two subordinates under them who are not completely skilled. You wouldn’t want to give them complete access to the entire deployment process. That’s when you realize the need for restricting the authorization.

Ansible Tower delivers this feature to restrict authorizations. So basically, you chose who can do what, which makes it easier to moderate. Also, Ansible Tower encrypts credentials and other sensitive data and you only give the subordinates access to relevant resources while restricting their access to irrelevant ones.

Demo: Automate The Provisioning Of An EC2 Instance Using Ansible

In this Demo section, I’m going to demonstrate how Ansible supports AWS by showing how to automate the starting and provisioning of an EC2 instance. Let’s get started.

Step 1:

Install Ansible on your server node and make an SSH connection between your server and the client nodes on AWS. In this case, I have created two EC2 instances, one server on which Ansible is installed and the other is the client.

Step 2: 

Now make sure you have all the requirements installed. According to the documentation, these are the following requirements:

  • Python >= 2.6
  • boto

Install python using the following command:

$ sudo apt install python

Install boto using the following command:

$ sudo apt install python-pip
$ pip install boto

Boto is a python interface for using Amazon Web services. You’ll have to import it using the following command:

$ python
$ import boto
$ exit()

boto - Ansible For AWS - Edureka

Step 3:

You have to configure your AWS. Use the following command for the same:

$ aws configure

And add your AWS access key id, secret key and default region(which is optional).

Step 4:

Write a playbook to start and provision an EC2 instance.

$ sudo vi /etc/ansible/launch.yml

---

- name: Create an ec2 instance
  hosts: web
  gather_facts: false

  vars:
      region: us-east-1
      instance_type: t2.micro
      ami: ami-05ea7729e394412c8
      keypair: priyajdm

  tasks:

    - name: Create an ec2 instance
      ec2:
         aws_access_key: '********************'
         aws_secret_key: '****************************************'
         key_name: "{{ keypair }}"
         group: launch-wizard-26
         instance_type: "{{ instance_type }}"
         image: "{{ ami }}"
         wait: true
         region: "{{ region }}"
         count: 1
         vpc_subnet_id: subnet-02f498e16fd56c277
         assign_public_ip: yes
    register: ec2

It’s a good practice to know what the code does before actually executing it. Let me explain this playbook for better understanding.

Name: It can be literally anything. A good practice is to keep a name that gives a basic description of the task it performs.

Host: Mentions the name of the host list against which the playbook needs to be executed. In my case it’s web.

gather_facts: This parameter tells Ansible to gather all the relevant facts, variables and other data for future reference. In our case, we’ve set it to false because we have no use of collecting facts(IP addr., Hostname, etc).

vars: This section defines and initializes all the variables that we’ll be using in this playbook. We have four variables here:

  • region defines the region in which the EC2 instance needs to come up
  • instance_type defines the type of instance we’re trying to bring up. In our case, we are using t2.micro
  • ami defines the AMI of the instance we’re trying to bring up

ami - Ansible fFor AWS - Edureka

  • keypair defines the keypair that we’re going to use to bring up the instance

ec2: This is a module provided by Ansible used to start or terminate an EC2 instance.

This module has certain parameters that we’ll be using to specify other funtionalities of the EC2 instance that we’re trying to start.

  • We start by mentioning AWS access key id and secret key using the parameters aws_access_key and aws-secret_key.
  • key_name: pass the variable that defines the keypair being used here
  • group: mention the name of the security group. This defines the security rules of the EC2 instance we’re trying to bring up
  • instance_type: pass the variable that defines the type of instance we’re using here
  • image: pass the variable that defines the AMI of the image we’re trying to start
  • wait: This has a boolean value of either true or false. If true, it waits for the instance to reach the desired state before returning
  • region: pass the variable that defines the region in which an EC2 instance needs to be created.
  • count: This parameter specifies the number of instances that need to be created. In this case, I’ve only mentioned only one but this depends on your requirements.
  • vpc_subnet_id: pass the subnet id in which you wish to create the instance
  • assign_public_ip: This parameter has a boolean value. If true like in our case, a public IP will be assigned to the instance when provisioned within VPC.

Step 5:

Now that you’ve understood every line in the playbook, let’s go ahead and execute it. Use the following command:

$ ansible-playbook /etc/ansible/launch.yml


Ansible playbook - Ansible For AWS - Edureka

Once you’ve executed the playbook, you’ll see an instance being created.

ec2 - Ansible For AWS - Edureka

And TADA! You’ve successfully automated the provisioning of an EC2 instance. The same way you can also write a playbook to stop the EC2 instance.

This brings us to the end of Ansible For AWS blog. If you find this article helpful, check out the DevOps course offered by Edureka. It covers all the tools that have made the IT industry efficient.

Comments
0 Comments

Browse Categories

Subscribe to our Newsletter, and get personalized recommendations.