In this project, we will create a multi-cloud deployment using Terraform. This project will help you understand how to manage resources across different cloud providers (AWS, Azure, and GCP) using a single Terraform configuration. By the end of this project, you will have a working multi-cloud infrastructure.

Objectives

  • Understand the basics of multi-cloud deployment.
  • Configure Terraform to work with multiple cloud providers.
  • Deploy resources on AWS, Azure, and GCP.
  • Manage and maintain the state of multi-cloud resources.

Prerequisites

  • Basic knowledge of Terraform.
  • Accounts on AWS, Azure, and GCP.
  • Installed and configured Terraform CLI.

Step-by-Step Guide

Step 1: Setting Up Providers

First, we need to configure Terraform to use multiple providers. Create a new directory for your project and a file named main.tf.

# main.tf

# Configure the AWS provider
provider "aws" {
  region = "us-west-2"
}

# Configure the Azure provider
provider "azurerm" {
  features {}
}

# Configure the GCP provider
provider "google" {
  project = "your-gcp-project-id"
  region  = "us-central1"
}

Step 2: Defining Resources

Next, we will define resources for each cloud provider. In this example, we will create an S3 bucket on AWS, a storage account on Azure, and a storage bucket on GCP.

# AWS S3 Bucket
resource "aws_s3_bucket" "example" {
  bucket = "my-terraform-bucket"
  acl    = "private"
}

# Azure Storage Account
resource "azurerm_storage_account" "example" {
  name                     = "examplestorageacct"
  resource_group_name      = "example-resources"
  location                 = "West US"
  account_tier             = "Standard"
  account_replication_type = "LRS"
}

# GCP Storage Bucket
resource "google_storage_bucket" "example" {
  name     = "my-terraform-bucket"
  location = "US"
}

Step 3: Initializing and Applying the Configuration

Initialize the Terraform configuration and apply it to create the resources.

# Initialize the configuration
terraform init

# Apply the configuration
terraform apply

Step 4: Verifying the Deployment

After applying the configuration, verify that the resources have been created in each cloud provider's console.

Step 5: Managing State

Terraform manages the state of your infrastructure in a state file. For multi-cloud deployments, it's crucial to ensure that the state file is properly managed and stored securely.

Remote State Storage

To store the state file remotely, you can use a backend like AWS S3, Azure Blob Storage, or GCP Cloud Storage. Here is an example of configuring an S3 backend:

# Configure the backend in main.tf
terraform {
  backend "s3" {
    bucket = "my-terraform-state-bucket"
    key    = "global/s3/terraform.tfstate"
    region = "us-west-2"
  }
}

Step 6: Cleaning Up

To clean up the resources created by this project, run the following command:

terraform destroy

Practical Exercise

Exercise 1: Adding More Resources

  1. Add an EC2 instance on AWS, a Virtual Machine on Azure, and a Compute Engine instance on GCP to the existing configuration.
  2. Apply the configuration and verify the resources.

Solution

# AWS EC2 Instance
resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
}

# Azure Virtual Machine
resource "azurerm_virtual_machine" "example" {
  name                  = "example-vm"
  location              = "West US"
  resource_group_name   = "example-resources"
  network_interface_ids = [azurerm_network_interface.example.id]
  vm_size               = "Standard_DS1_v2"

  storage_os_disk {
    name              = "example-os-disk"
    caching           = "ReadWrite"
    create_option     = "FromImage"
    managed_disk_type = "Standard_LRS"
  }

  storage_image_reference {
    publisher = "Canonical"
    offer     = "UbuntuServer"
    sku       = "18.04-LTS"
    version   = "latest"
  }

  os_profile {
    computer_name  = "hostname"
    admin_username = "adminuser"
    admin_password = "Password1234!"
  }

  os_profile_linux_config {
    disable_password_authentication = false
  }
}

# GCP Compute Engine Instance
resource "google_compute_instance" "example" {
  name         = "example-instance"
  machine_type = "f1-micro"
  zone         = "us-central1-a"

  boot_disk {
    initialize_params {
      image = "debian-cloud/debian-9"
    }
  }

  network_interface {
    network = "default"
    access_config {
      // Ephemeral IP
    }
  }
}

Conclusion

In this project, you learned how to set up a multi-cloud deployment using Terraform. You configured multiple providers, defined resources for each provider, and managed the state of your infrastructure. This project provides a solid foundation for managing multi-cloud environments using Terraform. In the next module, we will explore more advanced Terraform features and best practices.

© Copyright 2024. All rights reserved