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.
- Project Description
- Project Setup
- How to Run Projects
- Project Folders
- CI/CD User and Credentials
- CI/CD Workflow
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.
Ensure the following tools are installed and configured on your local computer: - See here for guidiance.
- AWS CLI: Interact with AWS services from the command line.
- aws-vault: Manage AWS credentials securely
- Docker Desktop : Run containerized applications locally
- Docker Compose: Define and run multi-container Docker applications
- Terraform: Provision infrastructure as code
- AWS Account: Required for deploying resources
- AWS Identity Center User: With permissions to create aws resources in a
devenvironment.
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
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.
Run the following command in your terminal
export TF_WORKSPACE=devThis 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 showUse 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-approveFor 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- 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
- 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.
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
