Terraform for Multi-Cloud Deployments

Task Overview

This task demonstrates how to deploy and manage cloud workloads across AWS, Azure, and GCP using a single Terraform configuration. The project includes provisioning compute instances, storage, networking, and security policies in each cloud while ensuring consistency and automation using Terraform.

Objectives

Technology Stack

High-Level Architecture

                  +---------------------------------+
                  |         GitHub/Jenkins         |
                  +---------------------------------+
                               |
           ---------------------------------------------------
           |                     |                        |
       AWS VPC               Azure VNet               GCP VPC
      (EC2, S3)             (VM, Blob)              (GCE, GCS)
           |                     |                        |
      Security Groups       NSG & RBAC              Firewall Rules
    

Implementation Steps

Step 1: Setup Terraform for Multi-Cloud

  1. Install Terraform
        curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
        sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
        sudo apt-get update && sudo apt-get install terraform
        terraform -version
                
  2. Setup Provider Credentials
  3. Initialize Terraform Providers
        terraform {
          required_providers {
            aws = { source = "hashicorp/aws" }
            azurerm = { source = "hashicorp/azurerm" }
            google = { source = "hashicorp/google" }
          }
        }
                

Step 2: Configure Remote State Backend

Use S3/DynamoDB, Azure Blob Storage, or GCP Cloud Storage for state management.

  1. AWS S3 Backend Example:
  2.     terraform {
          backend "s3" {
            bucket         = "terraform-state-multicloud"
            key            = "global/terraform.tfstate"
            region         = "us-east-1"
            encrypt        = true
            dynamodb_table = "terraform-lock"
          }
        }
        
  3. Azure Blob Backend Example:
  4.     terraform {
      backend "azurerm" {
        resource_group_name   = "terraform-backend"
        storage_account_name  = "tfstateaccount"
        container_name        = "tfstate"
        key                   = "terraform.tfstate"
      }
    }
    
        

Step 3: Define Multi-Cloud Infrastructure

  1. AWS EC2 Instance
  2.     provider "aws" {
      region = "us-east-1"
    }
    
    resource "aws_instance" "web" {
      ami           = "ami-12345678"
      instance_type = "t2.micro"
      tags = {
        Name = "AWS-WebServer"
      }
    }
        
  3. Azure Virtual Machine
  4. provider "azurerm" {
      features {}
    }
    
    resource "azurerm_virtual_machine" "web" {
      name                = "AzureVM"
      location            = "East US"
      resource_group_name = "terraform-rg"
      vm_size             = "Standard_B1s"
    }
    
  5. GCP Compute Engine
  6. provider "google" {
      project = "my-gcp-project"
      region  = "us-central1"
    }
    
    resource "google_compute_instance" "web" {
      name         = "GCP-WebServer"
      machine_type = "e2-medium"
      zone         = "us-central1-a"
    }
    

Step 4: Networking and Security

AWS VPC with Security Group

   resource "aws_vpc" "main" {
  cidr_block = "10.0.0.0/16"
}

resource "aws_security_group" "allow_ssh" {
  vpc_id = aws_vpc.main.id
  ingress {
    from_port   = 22
    to_port     = 22
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }
}

    

Azure NSG (Network Security Group)

resource "azurerm_network_security_group" "nsg" {
  name                = "azure-nsg"
  location            = "East US"
  resource_group_name = "terraform-rg"
}

GCP Firewall Rule

resource "google_compute_firewall" "allow-ssh" {
  name    = "allow-ssh"
  network = "default"
  allow {
    protocol = "tcp"
    ports    = ["22"]
  }
}

Step 5: Apply Terraform Configuration

  1. Initialize Terraform
  2. terraform init
  3. Validate Configuration
  4. terraform validate
  5. Plan Infrastructure
  6. terraform plan
  7. Apply Infrastructure
  8. terraform apply -auto-approve

Step 6: CI/CD Pipeline for Terraform Deployment

Using GitHub Actions for automation.

    name: Terraform Multi-Cloud Deployment

on:
  push:
    branches:
      - main

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Code
        uses: actions/checkout@v2

      - name: Setup Terraform
        uses: hashicorp/setup-terraform@v1

      - name: Terraform Init
        run: terraform init

      - name: Terraform Plan
        run: terraform plan

      - name: Terraform Apply
        run: terraform apply -auto-approve

    

Monitoring Setup

Project Outcomes

Next Steps

Conclusion

This project provides a production-ready Terraform setup for deploying and managing workloads across AWS, Azure, and GCP with a unified IaC approach. It ensures scalability, security, and automation, making multi-cloud management seamless. 🚀