Implementation of Gitlab CE with Gitlab runner. And CICD to create an infrastructure for 3 tier application using terraform.

Architecture Diagram

Docker compose for Gitlab and Gitlab Runner

version: '3.7'
services:
  web:
    image: 'gitlab/gitlab-ce:latest'
    restart: always
    hostname: 'localhost'
    container_name: gitlab-ce
    environment:
      GITLAB_OMNIBUS_CONFIG: |
        external_url 'http://localhost'
    ports:
      - '80:80'
      - '443:443'
    volumes:
      - '$GITLAB_HOME/config:/etc/gitlab'
      - '$GITLAB_HOME/logs:/var/log/gitlab'
      - '$GITLAB_HOME/data:/var/opt/gitlab'
    networks:
      - gitlab
  gitlab-runner:
    image: gitlab/gitlab-runner:alpine-v15.3.3
    container_name: gitlab-runner    
    restart: always
    depends_on:
      - web
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - '$GITLAB_HOME/gitlab-runner:/etc/gitlab-runner'
    networks:
      - gitlab

networks:
  gitlab:
    name: gitlab-network

Terraform Basic Code to create EC2, S3 and RDS :

Provider.tf :

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.0"
    }
  }
}

# Configure the AWS Provider
provider "aws" {
  shared_credentials_files = ["~/.aws/credentials"]
  profile                  = "default"
  region                   = var.region
}

EC2.tf :

resource "aws_instance" "web" {
  ami           = var.ami_id
  instance_type = "t2.micro"

  tags = {
    Name = "EC2"
  }
}

rds.tf :

resource "aws_db_instance" "default" {
  allocated_storage    = 10
  db_name              = "mydb"
  engine               = "mysql"
  engine_version       = "5.7"
  instance_class       = "db.t3.micro"
  username             = var.rds_username
  password             = var.rds_password
  parameter_group_name = "default.mysql5.7"
  skip_final_snapshot  = true
}

S3.tf :

resource "aws_s3_bucket" "example" {
  bucket = "myfuturehubtestingbucket"

  tags = {
    Name        = "My bucket"
    Environment = "Dev"
  }
}

Variables.tf :

variable "ami_id" {
  type        = string
  default     = "ami-007855ac798b5175e"
  description = "AMI ID for EC2 Instance"
}

variable "region" {
  type        = string
  default     = "us-east-1"
  description = "Region to create Infrastructure"
}

variable rds_username {
  type        = string
  default     = "foo"
  description = "Default username"
}

variable rds_password {
  type        = string
  default     = "foo123456789"
  description = "description"
}

Commands to run Terraform code :

terraform init
terraform plan
terraform apply

To destroy the infra :
terraform destroy

Leave a Comment

MFH IT Solutions (Regd No -LIN : AP-03-46-003-03147775)

Consultation & project support organization.

Contact

MFH IT Solutions (Regd)
NAD Kotha Road, Opp Bashyam School, Butchurajupalem, Jaya Prakash Nagar Visakhapatnam, Andhra Pradesh – 530027