Hands-on Challenge Day - Multi-Cloud Deployment with Terraform
Terraform is an open-source infrastructure as code software tool created by HashiCorp. It allows users to define and provision a cloud infrastructure using a high-level configuration language known as HashiCorp Configuration Language (HCL), or optionally JSON. Terraform supports multiple cloud service providers, enabling a multi-cloud strategy for infrastructure management.
A multi-cloud strategy involves using two or more cloud computing services from different providers. This approach can help reduce reliance on any single vendor, increase flexibility through more choice, and improve redundancy and disaster recovery capabilities.
"GlobalWeb Inc." is a digital media company that serves a global audience with high-quality content. To ensure high availability and adhere to data sovereignty regulations in different regions, GlobalWeb Inc. has decided to adopt a multi-cloud strategy, deploying resources across AWS and Google Cloud Platform (GCP).
Problem Statement: GlobalWeb Inc. needs to deploy a static website on AWS S3 and a Google Cloud Storage (GCS) bucket to serve content to users in different regions, ensuring high availability and compliance with regional data laws.
Your Mission: Use Terraform to automate the deployment of a static website on both AWS S3 and Google Cloud Storage. Ensure that the website is publicly accessible and that the deployments are managed as code for easy replication and management.
Configure Provider Credentials:
Define the Infrastructure with Terraform:
main.tf
):# AWS Provider Configuration
provider "aws" {
region = "us-east-1"
}
# GCP Provider Configuration
provider "google" {
credentials = file("<PATH_TO_YOUR_GCP_CREDENTIALS_JSON>")
project = "<YOUR_GCP_PROJECT_ID>"
region = "us-central1"
}
# AWS S3 Bucket for Static Website
resource "aws_s3_bucket" "aws_static_website" {
bucket = "globalweb-static-site-aws"
website {
index_document = "index.html"
error_document = "error.html"
}
policy =<<POLICY
{
"Version": "2012-10-17",
"Statement": [{
"Sid": "PublicReadGetObject",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::globalweb-static-site-aws/*"
}
]
}
POLICY
}
# GCP Cloud Storage Bucket for Static Website
resource "google_storage_bucket" "gcp_static_website" {
name = "globalweb-static-site-gcp"
location = "US"
force_destroy = true
website {
main_page_suffix = "index.html"
not_found_page = "error.html"
}
}
<PATH_TO_YOUR_GCP_CREDENTIALS_JSON>
with the path to your GCP credentials JSON file and <YOUR_GCP_PROJECT_ID>
with your GCP project ID.Deploy the Infrastructure:
terraform init
terraform apply
Prepare and Upload the Static Website:
index.html
and error.html
files to serve as the static website content.index.html
:<!DOCTYPE html>
<html>
<head>
<title>Welcome to GlobalWeb</title>
</head>
<body>
<h1>Hello, World! Welcome to GlobalWeb Inc.</h1>
</body>
</html>