Skip to content

tobbie/aws-infrastructure-with-terraform

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

37 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Hits

AWS Infrastructure Projects with Terraform

This repository contains a collection of AWS infrastructure projects built using Terraform. The projects demonstrate best practices for Infrastructure as Code (IaC) and cover various AWS services, CI/CD pipelines, and scalable deployments.


Table of Contents


Project Description

This project is a collection of AWS infrastructure templates managed with Terraform. It aims to:

  • Simplify the setup of cloud infrastructure using code.
  • Enable version-controlled and repeatable infrastructure deployments.
  • Demonstrate CI/CD workflows using GitHub Actions.

Project Setup

Prerequisites

Ensure the following tools are installed and configured on your local computer: - See here for guidiance.

AWS Resources Setup

The following AWS resources are required to be manually created in the same region :

  • S3 Bucket: Store Terraform remote state files
  • DynamoDB Table: Lock Terraform state files to prevent concurrent operations

The Setup Project

This setup project handles the creation and management of the CI/CD user.

  • Directory: setup
  • Key Resources Created: IAM roles and permissions for secure deployment

⚠️ Note: Rather than run terraform commands directly from my terminal, I opted to run terraform from a docker container. The benefits of this approach includes:

  • Consistency Across Environments: Running Terraform in a Docker container ensures that all team members use the same Terraform version and dependencies, eliminating version mismatches.
  • Isolation: Avoids conflicts with other Terraform versions or dependencies installed on your local machine
  • Ease of Setup: No need to manually install Terraform and its dependencies; simply pull and run the Docker container.
  • Reproducibility: The same container can be used across different machines and CI/CD pipelines, ensuring consistent behavior.
  • Security: Limits Terraform's access to only the mounted directories, reducing the risk of accidental modifications outside the intended scope.

Take a look at the doker compose file file in the project root.


Set local terraform workspace

Run the following command in your terminal

 export TF_WORKSPACE=dev

This makes the TF_WORKSPACE environment variable available to the docker container that runs our terraform manifests.

Alternatively, if you wish to persist the TF_WORKSPACE environment variable between terminal sessions, add export TF_WORKSPACE=dev to your .bashrc or .zshrc file in your home directory.

Run the following command to confirm your workspace is set to dev

  terrafrom workspace show

How to Run Projects

Use the following commands to deploy the infrastructure for a project:

# Initialize the Terraform project
docker compose run --rm terraform -chdir=<directory> init

# Format the configuration files
docker compose run --rm terraform -chdir=<directory> fmt

# Validate the configuration files
docker compose run --rm terraform -chdir=<directory> validate

# Plan the Terraform configuration
docker compose run --rm terraform -chdir=<directory> plan

# Apply the Terraform configuration
docker compose run --rm terraform -chdir=<directory> apply --auto-approve

For example, to deploy the infrastructure of the setup project, run the following commands:

docker compose run --rm terraform -chdir=setup init
docker compose run --rm terraform -chdir=setup fmt
docker compose run --rm terraform -chdir=setup validate
docker compose run --rm terraform -chdir=setup plan
docker compose run --rm terraform -chdir=setup apply --auto-approve

Project Folders

  • setup: Create and manage CI/CD user and IAM roles
  • network: AWS VPC with public, private and database subnets
  • ecs-with-fargate: AWS ECS service with Fargate option

CI/CD User and Credentials

  • The CICD user is required by github actions to perform deployments in your aws environments (staging, production).
  • This user is an IAM user with required permission/policies to create AWS resources
  • The CICD user is created with the setup project from your local computer by running the commands above
  • The credentials of the CICD user should be added as envrionment variables and secrets in your github repositiory if you want to automate the deployment pipeline.

CI/CD Workflow with GitHub Actions

The GitHub Actions workflow automates the following tasks:

  • Linting and validating Terraform configurations
  • Running plan and apply stages for infrastructure changes in the staging and production environments
  • Ensuring secure and repeatable deployments

CI/CD Workflow