{"id":38559,"date":"2016-07-29T15:20:53","date_gmt":"2016-07-29T09:50:53","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=38559"},"modified":"2016-07-30T06:14:55","modified_gmt":"2016-07-30T00:44:55","slug":"launching-and-configuring-an-aws-ec2-instance-using-ansible","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/launching-and-configuring-an-aws-ec2-instance-using-ansible\/","title":{"rendered":"Launching and Configuring an AWS EC2 instance using Ansible"},"content":{"rendered":"<p>Ansible is a configuration management tool which configures and manages systems for multi-node software deployment. It is an orchestration tool which prevents an agent from running continuously on a server to fetch the\u00a0desired configurations. Unlike <a title=\"Chef and Puppet DevOps\" href=\"http:\/\/www.tothenew.com\/devops-chef-puppet-docker\">Chef and Puppet<\/a>, it uses a push mechanism to push the desired changes on the servers using ssh-agent.<\/p>\n<p>Here in this blog, we will learn how to launch an AWS EC2 instance using ansible. We will write an Ansible playbook to launch the instance. The playbooks are written in &#8220;.yml&#8221; format.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone  wp-image-38564\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/07\/aaa.png\" alt=\"Ansible EC2\" width=\"224\" height=\"162\" \/><\/p>\n<p>First of all, we will discuss the basic requirements that need\u00a0to be initialized to launch an EC2 instance. We will need the following details:<\/p>\n<ul>\n<li>region =&gt; The region in which the instance needs to be launched.<\/li>\n<li>security group =&gt; The security group to be associated with the instance.<\/li>\n<li>image-id =&gt; The AMI id by which the instance is to be launched.<\/li>\n<li>instance-type =&gt; The type of the instance.<\/li>\n<li>key-pair =&gt; The Pem file to authenticate the login process.<\/li>\n<li>count =&gt; The number of instances to be launched.<\/li>\n<li>Role =&gt; The IAM role to be associated with the EC2 instance.<\/li>\n<li>volume-size =&gt; The size of the EBS volumes to be attached.<\/li>\n<\/ul>\n<p>After setting the variables we are good to go. The given below are the tasks of the ansible playbook which we are going to create:<\/p>\n<p><strong>1. Launching an EC2 instance<\/strong><\/p>\n<p>Ansible uses its ec2 module to launch the instance. The following is the task:<\/p>\n<p>[js]<br \/>\n&#8211; name: Launching an EC2 Instance<br \/>\n  local_action: ec2<br \/>\n       instance_type={{ instance_type}}<br \/>\n       image={{ ami }}<br \/>\n       region={{ region }}<br \/>\n       keypair={{ pem }}<br \/>\n       count={{count}}<br \/>\n       instance_profile_name={{ instance_profile_name }}<br \/>\n       group={{ security_group }}<br \/>\n       wait=true<br \/>\n       volumes={{volumes}}<br \/>\n register: ec2<br \/>\n[\/js]<\/p>\n<p>The value of the variables will be passed when executing the playbook. The &#8220;{{ }}&#8221; is being used to evaluate the value of the variable. The statement\u00a0<b>&#8220;<\/b>wait=true&#8221; is used to let ansible wait for the instance to come. The statement &#8220;register: ec2&#8221; register the output in ec2 variable so that we can run the\u00a0query to find out different properties of the instance.<\/p>\n<p><strong>2. Allocating Elastic IP to EC2 instance<\/strong><\/p>\n<p>[js]<br \/>\n&#8211; name: Associating after allocating elastic IP<br \/>\n        eip:<br \/>\n          in_vpc: yes<br \/>\n          reuse_existing_ip_allowed: yes<br \/>\n          state: present<br \/>\n          region: &quot;{{ region }}&quot;<br \/>\n          device_id: &quot;{{ ec2.instance_id[0] }}&quot;<br \/>\n  register: elastic_ip<\/p>\n<p>[\/js]<\/p>\n<p>This task is used to allocate Elastic IP to the instance. Here, the elastic IP is associated with the instance and set in the variable &#8220;elastic_ip&#8221;.<\/p>\n<p><strong><strong>3. Waiting for the instance to come<\/strong><\/strong><\/p>\n<p>[js]<br \/>\n&#8211; name: Waiting for the instance to come<br \/>\n        local_action: wait_for<br \/>\n                      host={{ item.private_ip }}<br \/>\n                      state=started<br \/>\n                      port=22<br \/>\n        with_items: ec2.instances<br \/>\n[\/js]<\/p>\n<p>This playbook task is used to wait for the instance to come. The instance check is done until the instance comes in the\u00a0available state. Here &#8220;with_items: ec2.instances&#8221; is used to create a loop. The ansible waits for the instance to come and become available\u00a0by looping at port 22.<\/p>\n<p><strong>4. Adding tags to the EC2 instance<\/strong><\/p>\n<p>[js]<br \/>\n&#8211; name: Adding tags to the EC2 Instance&quot;<br \/>\n        local_action: ec2_tag<br \/>\n                      region={{ region }}<br \/>\n                      resource={{ item.id }}<br \/>\n                      state=present<br \/>\n        with_items: ec2.instances<br \/>\n        args:<br \/>\n          tags:<br \/>\n            Name: &quot;{{ name }}&quot;<br \/>\n            Env: &quot;{{ Env }}&quot;<br \/>\n            Type: microservice<br \/>\n  register: tag<br \/>\n[\/js]<\/p>\n<p>This task is used to add tags to the instances. The local action\u00a0<strong>ec2_tag\u00a0<\/strong>is used. The item ec2.instances is used to pick out instance id and region. The tags are added as\u00a0Name: &#8220;{{ name }}&#8221;, where the value of the name will be passed from outside. The tags added will be initialized to by using &#8220;register: tag&#8221;.<\/p>\n<p>By using these above tasks in the \u00a0ansible playbook the instance will be created and configured. Make sure the host from which you are running the playbook must have enough permissions to launch the EC2 instance.<\/p>\n<p>The complete ansible playbook to launch instance using above tasks is as follows:<\/p>\n<p>[js]<\/p>\n<p>&#8212;<br \/>\n&#8211; name: Configuring the EC2 Instance<br \/>\n  hosts: localhost<br \/>\n  connection: local<br \/>\n  vars:<br \/>\n       count: {{ count }}<br \/>\n       volumes:<br \/>\n               &#8211; device_name: \/dev\/sda1<br \/>\n                 volume_size: {{ volume-size }}    <\/p>\n<p>&#8211; name: Launching an EC2 Instance<br \/>\n  local_action: ec2<br \/>\n  instance_type={{ instance_type}}<br \/>\n  image={{ ami }}<br \/>\n  region={{ region }}<br \/>\n  keypair={{ pem }}<br \/>\n  count={{count}}<br \/>\n  instance_profile_name={{ instance_profile_name }}<br \/>\n  group={{ security_group }}<br \/>\n  wait=true<br \/>\n  volumes={{volumes}}<br \/>\n  register: ec2<\/p>\n<p>&#8211; name: Associating after allocating elastic IP<br \/>\n  eip:<br \/>\n      in_vpc: yes<br \/>\n      reuse_existing_ip_allowed: yes<br \/>\n      state: present<br \/>\n      region: &quot;{{ region }}&quot;<br \/>\n      device_id: &quot;{{ ec2.instance_id[0] }}&quot;<br \/>\n      register: elastic_ip<\/p>\n<p>&#8211; name: Waiting for the instance to come<br \/>\n  local_action: wait_for<br \/>\n              host={{ item.private_ip }}<br \/>\n              state=started<br \/>\n              port=22<br \/>\n  with_items: ec2.instance<\/p>\n<p>&#8211; name: Adding tags to the EC2 Instance&quot;<br \/>\n  local_action: ec2_tag<br \/>\n              region={{ region }}<br \/>\n              resource={{ item.id }}<br \/>\n              state=present<br \/>\n  with_items: ec2.instances<br \/>\n  args:<br \/>\n   tags:<br \/>\n       Name: &quot;{{ name }}&quot;<br \/>\n       Env: &quot;{{ Env }}&quot;<br \/>\n       Type: microservice<br \/>\n  register: tag<\/p>\n<p>[\/js]<\/p>\n<p>The playbook has been created. Now, for example, the playbook should be run as:<\/p>\n<p><strong>ansible-playbook playbook_name.yml &#8211;extra-vars volume-size=10 -e instance_type=t2.micro -e region=us=east-1 -e keypair=sample.pem -e count=1\u00a0<\/strong><\/p>\n<p>The EC2 instance is now created. I will write more blogs on configuring <a title=\"DevOps AWS\" href=\"http:\/\/www.tothenew.com\/devops-aws\">AWS services<\/a> using Ansible.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ansible is a configuration management tool which configures and manages systems for multi-node software deployment. It is an orchestration tool which prevents an agent from running continuously on a server to fetch the\u00a0desired configurations. Unlike Chef and Puppet, it uses a push mechanism to push the desired changes on the servers using ssh-agent. Here in [&hellip;]<\/p>\n","protected":false},"author":163,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":16},"categories":[1174,2348,1],"tags":[3835,1933,3833,3832,3834],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/38559"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/163"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=38559"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/38559\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=38559"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=38559"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=38559"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}