AWS Configuration with VPC, Subnets, Security grp etc using Ansible
Companies have invested a significant 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 a service, which proved to be more efficient and safe. Integrating an IT automation tool like Ansible, which will quickly 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 Configuration
What We’ll Do?
- Creating a VPC with Subnet, Internet Gateway, Route table etc.
- Creating a Security Group and an SSH Key for EC2 Instance.
- Provisioning an EC2 instance with Apache Webserver Configured.
- Retrieving the IP of Apache Server.
- Every Minor Step is Done Using Ansible Playbook
Why Ansible?
Ansible is not meant for provisioning cloud Instances, it’s intended to configure systems, yet AnsibleAnsible has a capability to provisioning it adequately. 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:
- Ansible is based on an agentless architecture, unlike Chef and Puppet.
- Ansible accesses its host through SSH, which makes the communication between servers and hosts feel like a snap.
- No custom security infrastructure is needed.
- Configuring playbooks and modules is super easy as it follows YAML format.
- Has a wide range of modules for its customers
- Allows complete configuration management, orchestration, and deployment capability
- Ansible Vault keeps the secrets safe.
For Ansible Installation and Configuration Visit Here
Prerequisite :
- AWS Access Key and Secret Key. More Info
- Ansible Configured.
Let Go!
First, let’s Create vars.yml file for storing Variable and secure.yml file for storing AWS credentials.
Secure.yml file:
vars.yml file:
Creating more Variable makes your Code dynamic and developer-friendly.
Now obviously we don’t want any other developer so get your access key and secret key, and for that, we create an ansible-vault and store secure.yml data in that, which creates ASE256 encryption.
Creating a vault is easy.
This will create an ASE256 Encryption in secure.yml data and is Secured by a Password. To decrypt it Use: replace decrypt in place of encrypt.
Now let’s Create a Playbook for AWS Configuration.
Creating VPC
Here, I used Host as My PC(localhost) and Added the Variables File. In tasks, for passing the Variable we can use {{ }} as shown in the above picture.
Creating Subnet In Our VPC
>>For Passing VPC ID. I used the Register tag, which saves the output of the Task in a variable in JSON format.
Creating Internet Gateway for internet-routable traffic
>>Internet Gateway in AWS provides a target in your VPC route tables for internet-routable traffic, and to perform network address translation (NAT) for instances that have been assigned public IPv4 addresses.
Creating a Routing Table
>>A route table contains a set of rules, called routes, that are used to determine where network traffic from your subnet or gateway is directed.
Our VPC is configured. 👍
Creating a Security Group
Allow HTTP and SSH only as Inbound and All Traffic as Outbound.
Creating Key and Saving in Localhost
This Will Create a key and save it to the keys folder(Create One).
Creating EC2 Instance and Configuring Apache Server in it.
It Creates a Preconfigured EC2 Instance with Apache Server. Then the Debug Module Will Print all the Details of the Instance in JSON format.
Get this Complete Code in my Github Repo: Here
For Running this playbook
We need to Pass vault id because we are using a file from the vault and after that, it will prompt for a password and after entering it only it will execute.
Output:
Done!!
Thanks For reading Hope it helps You! ❤
Any Suggestion or Query. Ping Me
LinkedIn: Here
GitHub Repo: Here