End-to-End Database Server Configuration Task

This Task automates the installation and configuration of MySQL or PostgreSQL using Terraform and Ansible. It sets up the database server on an AWS EC2 instance, provisions necessary resources, and configures the database with users and permissions.

📌 Task Overview

1️⃣ Goals

Task Workflow

1. Infrastructure Provisioning with Terraform

2. Database Installation and Configuration using Ansible

3. Database & User Creation

4. Security & Access Control

5. Testing and Validation

6. Monitoring and Logging Setup

🔧 Tools & Technologies

📂 Task Structure

├── terraform/
│   ├── main.tf
│   ├── variables.tf
│   ├── outputs.tf
│   ├── terraform.tfvars
│   └── provider.tf
├── ansible/
│   ├── inventory.ini
│   ├── playbook.yml
│   ├── roles/
│   │   ├── db_install/
│   │   │   ├── tasks/main.yml
│   │   │   ├── handlers/main.yml
│   │   │   ├── templates/
│   │   │   ├── files/
│   │   │   └── vars/main.yml
│   │   ├── db_config/
│   │   │   ├── tasks/main.yml
│   │   │   ├── handlers/main.yml
│   │   │   ├── templates/
│   │   │   ├── files/
│   │   │   └── vars/main.yml
└── README.md
    

🔹 Implementation Steps

Step 1️⃣: Terraform - Provision Infrastructure

Use Terraform to:

📌 Terraform Configuration (`main.tf`)


provider "aws" {
  region = "us-east-1"
}

resource "aws_instance" "db_server" {
  ami           = "ami-12345678" # Ubuntu 20.04 LTS
  instance_type = "t2.medium"
  security_groups = [aws_security_group.db_sg.name]
  key_name        = "db-key-pair"
  tags = {
    Name = "DatabaseServer"
  }
}

resource "aws_security_group" "db_sg" {
  name        = "db_security_group"
  description = "Allow MySQL/PostgreSQL access"
  ingress {
    from_port   = 3306  # Use 5432 for PostgreSQL
    to_port     = 3306
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]  # Restrict to known IPs
  }
}
    

Run Terraform:

terraform init
terraform apply -auto-approve

Step 2️⃣: Ansible - Install and Configure Database

Once Terraform provisions the EC2 instance, Ansible is used for:

📌 Ansible Inventory (inventory.ini)

[database]
db_server ansible_host= ansible_user=ubuntu ansible_ssh_private_key_file=~/.ssh/id_rsa

📌 Ansible Playbook (`playbook.yml`)


- name: Configure Database Server
  hosts: database
  become: true
  roles:
    - db_install
    - db_config
    

📌 Role 1: Install Database

(roles/db_install/tasks/main.yml)

- name: Install MySQL Server
  apt:
    name: mysql-server
    state: present
  when: "'mysql' in ansible_facts['distribution']"

- name: Install PostgreSQL Server
  apt:
    name: postgresql
    state: present
  when: "'postgresql' in ansible_facts['distribution']"

- name: Ensure MySQL is running
  service:
    name: mysql
    state: started
    enabled: yes
  when: "'mysql' in ansible_facts['distribution']"

- name: Ensure PostgreSQL is running
  service:
    name: postgresql
    state: started
    enabled: yes
  when: "'postgresql' in ansible_facts['distribution']"

📌 Role 2: Configure Database

- name: Create Database
  mysql_db:
    name: my_database
    state: present
  when: "'mysql' in ansible_facts['distribution']"

- name: Create PostgreSQL Database
  postgresql_db:
    name: my_database
    state: present
  when: "'postgresql' in ansible_facts['distribution']"

- name: Create Database User
  mysql_user:
    name: db_user
    password: db_pass
    priv: "my_database.*:ALL"
    host: "%"
    state: present
  when: "'mysql' in ansible_facts['distribution']"

- name: Create PostgreSQL User
  postgresql_user:
    db: my_database
    name: db_user
    password: db_pass
    state: present
  when: "'postgresql' in ansible_facts['distribution']"

👉 Run Ansible:

ansible-playbook -i inventory.ini playbook.yml

Run Ansible:

ansible-playbook -i inventory.ini playbook.yml

🔐 Security Best Practices

🚀 Deployment Flows

📌 Expected Outputs

📜 Next Steps

📚 Summary

This project provides an end-to-end automated database setup using Terraform (for infrastructure) and Ansible (for configuration). It installs either MySQL or PostgreSQL, creates databases, users, and permissions, and secures access.