This repository contains the Terraform code and CI/CD pipelines to build and manage the AWS infrastructure for the "Automation Lab" project. It uses a modular, multi-environment approach to automatically deploy development (dev) and production (prod) environments.
-
Infrastructure as Code: Terraform
-
CI/CD: GitHub Actions
-
State Management & Secrets: Terraform Cloud
-
Cloud Provider: Amazon Web Services (AWS)
-
.github/workflows/: Contains the GitHub Actions workflow files.-
terraform.yml: The main CI/CD pipeline for planning and applying infrastructure changes. -
terraform-destroy.yml: A manually-triggered workflow to destroy infrastructure.
-
-
terraform/: The root directory for all Terraform code.-
main.tf: The main entry point that calls the modules. -
variables.tf: Defines the input variables for the root module. -
modules/: Contains reusable, modular infrastructure components.vpc/: A module to create a Virtual Private Cloud (VPC), subnets, and basic routing.
-
The deployment process is fully automated based on Git branches:
-
devEnvironment: Pushing a commit to thedevbranch will automatically trigger aterraform applyfor the development environment. -
prodEnvironment: Pushing a commit to themainbranch will automatically trigger aterraform applyfor the production environment. -
Pull Requests: Opening a pull request against the
mainbranch will trigger aterraform planto show the expected changes, but it will not apply them.
This project uses Terraform Cloud Workspaces to manage separate environments. This is the standard and safest way to ensure dev and prod state files are completely isolated.
-
The
devbranch maps to theautomationLab-devworkspace in Terraform Cloud. -
The
mainbranch maps to theautomationLab-prodworkspace in Terraform Cloud.
All infrastructure configuration variables are securely stored in Terraform Cloud. You must configure the following in your organization (automationLab):
-
Two Workspaces:
automationLab-devandautomationLab-prod. -
Variables: For each workspace, navigate to the Variables tab and set the required Terraform variables (e.g.,
vpc_cidr,environment, etc.). The values should be different for each environment to ensure network isolation.
Navigate to Settings > Secrets and variables > Actions in your GitHub repository and configure the following secrets:
-
TF_API_TOKEN: An API token generated from your Terraform Cloud user account. -
TF_CLOUD_ORGANIZATION: Your Terraform Cloud organization name (e.g.,automationLab). -
AWS_ACCOUNT_ID: Your 12-digit AWS Account ID, used by the pipeline to assume the correct IAM Role.
To tear down an environment, a separate, manual workflow is provided to prevent accidental deletion.
-
Go to the Actions tab in the repository.
-
Select the Terraform Destroy workflow.
-
Click Run workflow and choose either
devorprodfrom the dropdown menu. -
Confirm by clicking the green Run workflow button.
Warning: This operation is irreversible and will permanently delete all resources in the selected environment.