Friday, January 6, 2017

Steps to launch AWS EC2 instance using Ansible.

Here i am going to describe how to lauch EC2 machine on AWS Cloud using the Ansible automation & configuration management engine.

Required tools and Software.
  1. Oracle VirtualBox
  2. Ubuntu ISO installer.
  3. AWS access key and secret key.

Step 1: Installing VirtualBox and Ubuntu OS.

   Download Oracle virtual box from here and install, refer this for installation procedure.
   Download Ubuntu ISO installer from here, its free adjust the fields to zero and proceed to download.
   Fallow this link to install ubuntu operating system on above installed virtual box.

   Above installation steps are pretty easy, just fallow the wizard instructions.

   Verify

       After installation completed, you should be able to log on to ubuntu vm without any issue.

   Installing Packages required

      we need to install below packages on Ubuntu VM.
  • SSH (default)
  • Python (default)
  • Pip
  • Ansible
  • boto
  • wget/curl
     packages marked with default, might have already installed on VM verify first. Open terminal by right clicking on vm desktop and gain the root user access "#sudo su root" enter.

    Verifying SSH.  

    
  

   verify python 

   

      press ctrl+D to quit.

    Verify wget 

     

 If you see same output as shown in above screen shot in your case, well we good to proceed. If not install them using ubuntu package manager "apt"

    Install pip

     Issue below command on terminal with root privileges.

      root@vm-name:~#apt update
      root@vm-name:~#apt -y upgrade     --Note it take time to upgrade all packages.
      root@vm-name:~#apt install python-pip


   Install Pip

     Issue below command to install pip on ubuntu vm with root privileges.
     root@vm-name:~#pip install ansible

   verify pip installation by issue command "pip", output must be as shown below.

   
  

  Install Ansible

     Issue below command to install ansible on ubuntu VM with root privileges.
     root@vm-name:~#pip install ansible


    verify ansible installation by issue command "ansible" , output must be as show below. 
    

   Install boto

       issue below command to install boto on ubuntu VM with root privileges.
       root@vm-name:~#pip install boto   

   

Step 2: Setup AWS credentials.

   Log on to aws.amazon.com with your credentials and generate aws access key and secret key as mentioned here

   and export them as environment variables. 
    
    root@vm-name:~#export aws_access_key_id=<changeme>
    root@vm-name:~#export aws_secret_access_key=<changeme>
    

Step 3: Prepare playbook and run.

Prepare file with the name lauch_ec2.py in any location.  and copy the below code and paste it in the file.
        - hosts: localhost
          connection: local
          gather_facts: False

          vars:
              ami_id : "ami-06963965"
              region : "ap-southeast-1"
              group : "default"
              instance_type : "t2.micro"
          tasks:

              - name: Provision exactly on instance
                ec2:
                   group: "{{ group }}"
                   instance_type: "{{ instance_type }}"
                   image: "{{ ami_id }}"
                   region: "{{ region }}"
                   wait: true
                   exact_count: 1
                   count_tag:
                       Name: Demo
                   instance_tags:
                       Name: Demo
               register: ec2

      And then open terminal where you have created the lauch_ec2.py file and issue the below command to run the play book.
       root@vm-name:~#ansible-playbook launch_ec2.py

     Output should be like below and go back to aws console check ec2 instance dashboard you should find a vm running there with name "Demo"

  
       
  
  
UA-41474183-1