Monday, December 28, 2020

How to create EC2 instance using Ansible

Description: Here I have explained, How to create EC2 instance using Ansible

Create an IAM user  from AWS console

  • Open AWS console and navigate to IAM service 


Install Require Packages on Ansible Machine

  • Once User created successfully install below require things on Ansible machine 
Ansible

# yum install ansible -y

Python 

# yum install python python-devel python-pip

Boto [Boto is the python package which provides the interface to AWS] install using pip

#  pip install boto


Create Ansible Playbook to Create EC2 instance

  • Add localhost in ansible host file for creating the connection to AWS console

[webserver]
localhost


  • Create SSH Key for localhost and copy to authorization
    # ssh-keygen -t rsa


  • Once key file created copy to authorized_keys and paste as follow
  • Create a playbook for EC2 instance and paste below content in yaml file 
# vi ec2.yaml

---
  - name: Launching the AMS instance
    hosts: localhost
    tasks:
      - name: Launching the AMS instance
        ec2:
          key_name: ansible
          region: us-east-1
  instance_type: t2.micro
  image: ami-0c582118883b46f4f
          group: Ansible
  count: 2
  aws_access_key: WODAJGU3OHZ7RDKG4TPQ
  aws_secret_key: OD3I2mgh/ynyrJJ9Y/bLQto6JLII3gyGBFYJ+w7

  Description of Playbook 
    key_name: Key created in EC2  instance 
    region: The region on which you want to create new instance
    instance_type : Instance Type in EC2 
    image: Image you want to use to create new instance, You can get image id from EC2 launch                             console

   group: Security Group name which you want to place for VM
   count : Number of EC2 instance which you want to create
   aws_access_key: Access key from the user IAM user which we have created on beginning
   aws_secret_key: Secret key from the IAM user 

  • You will get both the keys from AWS IAM console

  • Test playbook content using below command 
# ansible-playbook -C ec2.yaml


  • Once result showing OK you can run playbook using the ansible-playbook command 

# ansible-playbook ec2.yaml


  • You can see new 2 new instances on the list as follow



7 comments:

  1. Thanks for the interesting post, keep continue your good job.



    How to Install Docker on Ubuntu 20.04 Easily

    ReplyDelete