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
├── playbook.yml
├── publicip.txt
├── terraform.tfstate
├── terraform.tfstate.backup

Ansible playbook

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

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

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

[root@server terraform]# cat 
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 
${web_public_ip} #${web_id}

[root@server terraform]# cat 
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.

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