# Terraform & Ansible Scalable IaC Project

> A college infrastructure project that uses Terraform to provision Azure resources and Ansible to configure the Linux VM fleet.

- URL: https://mandalsuraj.com/blog/terraform-ansible-final-ccgc
- Author: Suraj Mandal (https://mandalsuraj.com)
- Published: 2023-09-01
- Project date: 2023-08-21
- Tags: automation, college, ccgc, terraform, ansible, devops, infrastructure
- Source: https://github.com/surajmandalcell/automation-final-terraform-ansible

![Terraform & Ansible Scalable IaC Project cover](https://mandalsuraj.com/images/blog/terraform-ansible-final-ccgc/cover.png)

This was the final project for the **2023 Humber College CCGC Automation course**. My classmates and I split cloud provisioning from server setup.

## Architecture

![Terraform and Ansible infrastructure project cover](https://mandalsuraj.com/images/blog/terraform-ansible-final-ccgc/cover.png)

```mermaid
flowchart TB
  accTitle: Terraform And Ansible Architecture
  accDescr: Terraform provisions the Azure network, Linux fleet, and shared services. Ansible configures the Linux hosts.
  terraform["<strong>Terraform Modules</strong><br/><small>Provision Azure</small>"] --> network["<strong>Network Boundary</strong><br/><small>Virtual Network And Security</small>"]
  terraform --> compute["<strong>Linux Compute</strong><br/><small>Virtual Machine Fleet</small>"]
  terraform --> services["<strong>Shared Services</strong><br/><small>Data Protection And Monitoring</small>"]
  network --> balance["<strong>Load Balancer</strong><br/><small>Route Public Traffic</small>"]
  compute --> hosts["<strong>Linux Hosts</strong><br/><small>Three Configured Servers</small>"]
  balance --> hosts
  ansible["<strong>Ansible Roles</strong><br/><small>Configure The Hosts</small>"] --> hosts
  services --> database["<strong>PostgreSQL</strong><br/><small>Managed Database</small>"]
  services --> storage["<strong>Storage</strong><br/><small>Shared Data</small>"]
  services --> monitoring["<strong>Monitoring</strong><br/><small>Logs And Health</small>"]
  services --> recovery["<strong>Recovery</strong><br/><small>Backup Services</small>"]
```

**Terraform** owns the Azure resources. **Ansible** turns the Linux hosts into usable servers. This split made each failure easier to find.

### Terraform Modules

| Module | Purpose |
|---|---|
| `rgroup-RandomID` | Creates the resource group. |
| `network-RandomID` | Creates the virtual network, subnet, and security group. |
| `common-RandomID` | Creates monitoring, recovery, and storage services. |
| `vmlinux-RandomID` | Creates Linux VMs across availability zones. |
| `datadisk-RandomID` | Attaches 10 GB data disks to the VM fleet. |
| `loadbalancer-RandomID` | Places the Linux VM fleet behind a public load balancer. |
| `database-RandomID` | Creates Azure Database for PostgreSQL. |

The root module joins these modules and prints their outputs.

### Ansible Roles

| Role | Purpose |
|---|---|
| `datadisk-n01537188` | Partitions, formats, and mounts data disks. |
| `profile-n01537188` | Updates system profile values and session timeouts. |
| `user-n01537188` | Manages users, groups, SSH keys, and sudo access. |
| `webserver-n01537188` | Installs Apache, deploys a page, and starts the service. |

## Demo

Additional Videos

## Run The Project

### Ansible

Install Ansible, then run the playbook from its project folder.

```bash
sudo apt update
sudo apt install ansible
ansible-playbook  --verbose n01537188-playbook.yaml
```

### Terraform

Run these commands from the Terraform root module.

```bash
terraform init
terraform validate
terraform plan
terraform apply
```

Use the detailed exit code in automation. Remove the lab resources when the work is complete.

```bash
terraform plan -detailed-exitcode
terraform destroy
```
