Terra Towns Project

Terraform Bootcamp by ExamPro

Terra Towns Project

Overview

This documentation provides an in-depth exploration of the four-week Terraform Bootcamp project, focusing on hosting a static website using Terraform on AWS cloud platform using services like S3 Bucket and CloudFront. The project also involved the development of a custom Terraform provider, named Terratowns. The tools utilized in this project were carefully selected to ensure efficiency and collaboration. Selected tools/stacks include; Gitpod (Cloud-IDE), GitHub (Version Control), Terraform Cloud (Remote Backend), Golang (Building Custom Terraform Provider), Ruby (Mock Server), AWS S3 Bucket (Hosting), AWS CloudFront (Content Delivery).
Project Repo: https://github.com/ceeepath/terraform-beginner-bootcamp-2023

Week 0: Environment Setup and Preparation

The inaugural week of the Terraform Bootcamp was dedicated to preparing our development environment and ensuring that all required tools are installed in the environment. Specifically, we had to configure our gitpod.yml file for installing Terraform and AWS-CLI whenever we stop and restart gitpod.

[gitpod.yml]

tasks:
  - name: terraform_installation
    before: |
      source ./bin/install_terraform.sh
  - name: aws-cli
    env:
      AWS_CLI_AUTO_PROMPT: on-partial
    before: |
      source ./bin/install_aws_cli.sh

vscode:
  extensions:
    - amazonwebservices.aws-toolkit-vscode
    - hashicorp.terraform

Week 1: Project Setup and Resource Building

This week centered around creating our modules and laying the groundwork for a structured and scalable Terraform codebase.

module "terrahouse_aws" {
  source = "./modules/terrahouse_aws"
  # Creating the S3 Bucket
  user_uuid   = var.user_uuid
  bucket_name = var.bucket_name
}

Leveraging Terraform's capabilities, the module was used in the deployment of S3 Bucket and CloudFront distribution, showcasing an adherence to Infrastructure as Code principles. Additionally, I implemented an efficient asset upload process to the S3 Bucket utilizing Terraform's for_each functionality. Security considerations were paramount as I implemented CloudFront Origin Access Control (OAC) for our Content Delivery Network, and fortified our infrastructure by creating a bucket policy, granting read-only permissions to the CloudFront Origin Access Control (OAC) for the S3 Bucket.

resource "aws_s3_object" "terratown" {
  for_each = var.file_path
  bucket = aws_s3_bucket.terratown.bucket
  key    = "${each.key}.html"
  source = each.value
  content_type = local.content_type
  etag = filemd5(each.value)
  lifecycle {
    replace_triggered_by = [ terraform_data.content_version.output ]
    ignore_changes = [ etag ]
  }
}

resource "aws_s3_object" "terratown_assets" {
  for_each = fileset(var.assets_path,"*.{jpg,png,gif}")
  bucket = aws_s3_bucket.terratown.bucket
  key    = "assets/${each.key}"
  source = "${var.assets_path}/${each.key}"
  etag = filemd5("${var.assets_path}/${each.key}")
  lifecycle {
    replace_triggered_by = [terraform_data.content_version.output]
    ignore_changes = [etag]
  }
}


resource "aws_s3_bucket_policy" "terratown" {
  bucket = aws_s3_bucket.terratown.bucket
  policy = local.policy
}

Week 2 - Connecting to Terratowns

The week commenced with a strategic utilization of Ruby to set up a Sinatra mock web server. This server played a pivotal role in the testing of our custom Terraform provider. To install the required Ruby packages (gem) and run the server we made use of bundler.

bundle install
bundle exec ruby server.rb

Subsequently, the spotlight shifted to Golang, where we delved into the implementation of CRUD (Create, Read, Update, Delete) operations for the Terraform provider. This intricate process demanded extensive hours dedicated to debugging and rigorous testing, ensuring the reliability and functionality of our custom solution.

provider "terratowns" {
  endpoint = var.terratowns_endpoint
  user_uuid= var.teacherseat_user_uuid
  token= var.terratowns_access_token
}

The culmination of these efforts was the successful establishment of our custom Terraform provider. With this crucial component finally operational, we seamlessly deployed our static website to Terratowns, marking a significant milestone in the project.

module "football_manager" {
  source = "./modules/terrahouse_aws"
  teacherseat_user_uuid = var.teacherseat_user_uuid
  public_path = var.football_manager.public_path
  content_version = var.football_manager.content_version
}

resource "terratowns_home" "football_manager" {
  name = "Reviving the Red Devils: A Football Manager 2023 Saga"
  description = <<DESCRIPTION
Football Manager 2023 is a simulation game that lets you take 
charge of a football club and lead them to glory.
A die-hard Manchester United fan, Pathfinder, takes charge of his beloved club in Football Manager 2023. 
Frustrated with the team's lackluster performances, he embarks on a virtual journey to transform the club, 
instilling a possession-based style, promoting youth, and making shrewd, budget-friendly signings.
DESCRIPTION
  #domain_name = module.terrahouse_aws.cloudfront_url
  domain_name = module.football_manager.cloudfront_domain_name
  town = "gamers-grotto"
  content_version = var.football_manager.content_version
}

The journey through Week 2 was characterized by intense problem-solving sessions, continuous refinement, and the satisfaction of witnessing the fruits of our labor as our custom Terraform provider took center stage in the deployment process. This week not only underscored the depth of our technical proficiency but also affirmed the resilience and collaborative spirit fostered throughout the Bootcamp.

Take-Aways

The Terraform Bootcamp provided a comprehensive learning experience over four weeks, where I gained practical expertise in deploying and managing AWS resources using Terraform. The program emphasized Infrastructure as Code (IaC) principles, guiding me through the creation of modular and scalable code for S3 buckets, CloudFront distributions, and security configurations.

A significant highlight was the development of a custom Terraform provider using Golang, involving the setup of a Sinatra mock web server for rigorous testing using Ruby. The capstone project, deploying a static website to Terratowns using our custom Terraform provider, showcased the practical application of the acquired skills, reinforcing a problem-solving mindset essential for cloud infrastructure management.