Ansible And AWS EC2 Instance
****Ansible is tool which is used for Configuration Management (CM). In Ansible we only tell to "what to do". We don't need to tell "How to do" because Ansible already knows that "How to do this task" on each type of Operating System .
****AWS is a cloud which is provided by Amazon. Amazon Elastic Compute Cloud (Amazon EC2 ) is a part of Amazon.com's cloud-computing platform, Amazon Web Services, that allows users to rent virtual computers on which to run their own computer applications. AWS EC2 service provisions resources like RAM , HardDisk . CPU etc.
In this article I will Launch "AWS EC2 instance"
In Ansible If we want to do configuration we need Managed Node where we want to configuration , But any cloud doesn't provide Any OS for this
If we want to communicate with AWS then three ways available -
> WebUI
> CLI
> API
In this practical I will use API way. Ansible is purely build in python So , we need a python API which have capable to communicate with AWS . For this "boto" API is available.
Now To communicate AWS with API we need -
> "boto" API
> Access key and Private key (To login in aws)
** Install "boto" API with pip (pip3 install boto) -
** Access key and Private key -
> Go to IAM service in AWS -
> Add user - (Select Programmatic Access ) then Click Next -
>In my case I am providing "Administrator Access" All power to this user -
> Download .csv file - if you want to store you key in your harddisk.
Now I am creating a Ansible vault to store these keys - (varible file is "pass.yml")
Now For creating EC2 Instance We need these information -
> region: "ap-south-1"
> image: "ami-0ebc1ac48dfd14136"
> instance_type: "t2.micro"
> count: 1 (Number of Instance) and > vpc_subnet_id: "subnet-7acfbf36"
> volumes:
- volume_type: "gp2"
volume_size: 8
delete_on_termination: true
> group_id: "sg-757bbd11"
> key_name: "key"
Now We have gathered all information about ec2 instance. So for launching aws ec2 instance ansible has "ec2" module. To know more about this module visit this link -
So our Ansible playbook code -
We are running this playbook on localhost so we assign localhost in hosts, See there are no running instance on aws
Now run this playbook -
Now our code is successfully run - To checking I am going to aws WebUI -
Now our practical is successfully done.
Thank you.