AWS-Terraform-Ansible

Creating AWS infrastructure ( CloudFront + S3+ EC2 Instances) using the Terraform tool with the HCL(HashiCorp Language) scripts and ansible engine is used for infrastructure configuration management.

Pre-requisites:- Preconfigured AWS CLI, ansible engine, Terraform CLI, IAM-user with administrative powers.

[root@server terraform]# aws configure
AWS Access Key ID [********************]:
AWS Secret Access Key [********************]:
Default region name [ap-south-1]:
Default output format [None]:

So, our tree and ansible.cfg file looks like.

[root@server terraform]# tree 
.
├── ansible.cfg
├── image.png
├── inventory
├── inventory.template
├── mykeypair.pem
├── my.png
├── outputs.tf
├── playbook.yml
├── providers.tf
├── publicip.txt
├── README.md
├── resources.tf
├── terraform.tfstate
├── terraform.tfstate.backup
└── variables.tf

Ansible playbook

[root@server terraform]# cat playbook.yml 
---
- name: installing and starting service of httpd
hosts: webserver
become: yes
gather_facts: no

variables.tf

[root@server terraform]# cat variables.tf 
variable "profile" {
default = "mumbai-iam-user"
}

providers.tf

[root@server terraform]# cat providers.tf 
provider "aws" {
profile = "${var.profile}"
region = "${var.region}"

outputs.tf

[root@server terraform]# cat outputs.tf 
output "AWS-instance-Public-IP" {
value = "${aws_instance.web.public_ip}"
}

Here, we are using a template file for creating our inventory file of the ansible hosts so that we can create inventory file as per our requirement.

[root@server terraform]# cat inventory.template 
[webserver]
${web_public_ip} #${web_id}

resources.tf

[root@server terraform]# cat resources.tf 
resource "aws_security_group" "allow_ssh_http" {
name = "allow_ssh_http_sg"
description = "Allow inbound SSH and HTTP"
vpc_id = "${var.vpc}"

So, now we can create our resources to build our infrastructure.

[root@server terraform]# terraform init

As terraform work on the declarative approach, so we can create or destroy our infrastructure from the same Infrastructure-as-code [IAC].

For destroying everything created by Terraform.

[root@server terraform]# terraform destroy

For any correction and any query, follow our Github repo.

https://github.com/A4ANK/AWS-terraform-ansible-end-to-end-automation

I'm a computer science undergraduate and my primary area of work is under Linux, CloudComputing, DevOps culture, and various open-source tools and technologies