This project demonstrates how to use Terraform to deploy AWS infrastructure and integrate it with GitLab CI/CD for automated workflows. It includes the use of remote state management with S3 and DynamoDB for state locking, as well as a modular Terraform setup for efficient resource management.
- Infrastructure provisioning with Terraform modules.
- AWS resources like VPC, subnets, security groups, and EC2 instances.
- Remote state management using S3 and DynamoDB.
- GitLab CI/CD pipeline automation for validation, planning, and deployment.
βββ main.tf # Root module
βββ provider.tf # AWS provider configuration
βββ variables.tf # Global variable declarations
βββ vpc/ # VPC module
β βββ main.tf # VPC configuration
β βββ outputs.tf # VPC outputs
β βββ variables.tf # VPC variables
βββ web/ # EC2 module
β βββ main.tf # EC2 configuration
β βββ outputs.tf # EC2 outputs
β βββ variables.tf # EC2 variables
βββ backend.tf # Remote backend configuration
βββ .gitlab-ci.yml # GitLab CI/CD pipeline
terraform {
backend "s3" {
bucket = "example-s3-bucket"
key = "terraform/state"
region = "us-east-1"
dynamodb_table = "terraform-state-lock"
}
}
The .gitlab-ci.yml
file automates Terraform workflows.
- Validate: Check Terraform syntax.
- Plan: Generate an execution plan.
- Apply: Apply changes (manual approval required).
- Destroy: Tear down resources (manual approval required).
stages:
- validate
- plan
- apply
- destroy
validate:
image: hashicorp/terraform:latest
stage: validate
script:
- terraform init
- terraform validate
plan:
image: hashicorp/terraform:latest
stage: plan
script:
- terraform init
- terraform plan -out=tfplan
artifacts:
paths:
- tfplan
apply:
image: hashicorp/terraform:latest
stage: apply
script:
- terraform init
- terraform apply -auto-approve tfplan
when: manual
destroy:
image: hashicorp/terraform:latest
stage: destroy
script:
- terraform init
- terraform destroy -auto-approve
when: manual
- Terraform installed locally.
- AWS CLI configured with credentials.
- GitLab project with CI/CD variables:
AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY
AWS_DEFAULT_REGION
- Clone the repository.
- Initialize Terraform:
terraform init
- Validate the configuration:
terraform validate
- Deploy the infrastructure:
terraform apply -auto-approve
- Commit and push changes to trigger the GitLab pipeline:
git add . git commit -m "Initial commit" git push origin main
- Effective use of Terraform modules for scalability.
- Importance of remote state management for collaboration.
- Automating infrastructure deployment using CI/CD pipelines.
Author: Suran Sandeepa βοΈ
Feel free to explore and contribute! π