Project: Automating AWS Infrastructure Provisioning using Terraform & Ansible

1. Project Overview

The goal of this project is to automate the provisioning of AWS infrastructure using Terraform and configure instances using Ansible. This will enable a scalable, repeatable, and efficient deployment process.

2. Tech Stack

3. Architecture Diagram

4. Implementation Plan

Step 1: Setup Terraform for Infrastructure Provisioning

  1. Install Terraform on your local machine or CI/CD pipeline.
  2. Define Terraform provider (AWS) in a provider.tf file.
  3. Create a VPC with CIDR block 10.0.0.0/16.
  4. Define public and private subnets:
  5. Setup an Internet Gateway & Route Table for public subnet access.
  6. Define Security Groups:
  7. Create EC2 Instances:
  8. Deploy RDS Instance (MySQL) in the private subnet.

Step 2: Use Terraform Modules for Reusability

Step 3: Store Terraform State in S3 with DynamoDB Locking

Example backend.tf file:

    terraform {
      backend "s3" {
        bucket         = "my-terraform-state"
        key            = "prod/terraform.tfstate"
        region         = "us-east-1"
        dynamodb_table = "terraform-lock"
      }
    }
    

Step 4: Deploy Infrastructure Using Terraform

5. Configuration Management with Ansible

Ansible Setup on the Public EC2 Instance

  1. Install Ansibleon the public EC2 instance.
  2. Generate an SSH Key Pair and distribute it to the private instances.
  3. Define an Inventory File (inventory.ini):
  4.     [app_servers]
        10.0.2.10 ansible_user=ec2-user ansible_ssh_private_key_file=~/.ssh/id_rsa
        
  5. Write Playbooks for Configuration
  6. Example Playbook (setup_app.yml)

    - hosts: app_servers
      become: yes
      tasks:
        - name: Install Nginx
          yum:
            name: nginx
            state: present
        
        - name: Start and enable Nginx
          service:
            name: nginx
            state: started
            enabled: yes
    
  7. Run the Playbook:
  8. ansible-playbook -i inventory.ini setup_app.yml
    

6. CI/CD Pipeline (Optional)

If you want to integrate this into a CI/CD pipeline:

7. Testing & Validation

8. Cleanup & Cost Optimization

Conclusion

This project provides a fully automated AWS Infrastructure setup using Terraform and Ansible, ensuring reusability, scalability, and security.