Skip to content

βš‘πŸ—οΈThis project demonstrates an automated infrastructure deployment pipeline built with Terraform and GitLab CI/CD. It provisions AWS resources like VPCs, subnets, security groups, and EC2 instances, ensuring efficiency, consistency, and scalability.

Notifications You must be signed in to change notification settings

SuranSandeepa/aws-terraform-gitlab-pipeline

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

9 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Terraform and GitLab CI/CD Pipeline for AWS Infrastructure πŸŒπŸ’»

Project Overview πŸ“–

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.

Features ✨

  • 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.

Project Structure πŸ—‚οΈ

β”œβ”€β”€ 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 Configuration πŸ”§

terraform {
  backend "s3" {
    bucket         = "example-s3-bucket"
    key            = "terraform/state"
    region         = "us-east-1"
    dynamodb_table = "terraform-state-lock"
  }
}

GitLab CI/CD Pipeline πŸš€

The .gitlab-ci.yml file automates Terraform workflows.

CI/CD Stages πŸ› οΈ

  1. Validate: Check Terraform syntax.
  2. Plan: Generate an execution plan.
  3. Apply: Apply changes (manual approval required).
  4. Destroy: Tear down resources (manual approval required).

Example .gitlab-ci.yml πŸ“

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

How to Use πŸ› οΈ

Prerequisites βœ…

  • 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

Steps 🏁

  1. Clone the repository.
  2. Initialize Terraform:
    terraform init
  3. Validate the configuration:
    terraform validate
  4. Deploy the infrastructure:
    terraform apply -auto-approve
  5. Commit and push changes to trigger the GitLab pipeline:
    git add .
    git commit -m "Initial commit"
    git push origin main

Lessons Learned πŸ’‘

  • Effective use of Terraform modules for scalability.
  • Importance of remote state management for collaboration.
  • Automating infrastructure deployment using CI/CD pipelines.

References πŸ“š


Author: Suran Sandeepa ✍️

Feel free to explore and contribute! πŸŽ‰

About

βš‘πŸ—οΈThis project demonstrates an automated infrastructure deployment pipeline built with Terraform and GitLab CI/CD. It provisions AWS resources like VPCs, subnets, security groups, and EC2 instances, ensuring efficiency, consistency, and scalability.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages