Effortless Cloud Deployment: Terraform on AWS, GCP, and Azure

Master raj
6 min readApr 3, 2024

--

Easily set up your cloud resources on AWS, GCP, and Azure using the same starting point. Just spin up a base VM on AWS, install Terraform, and you’re ready to deploy resources across all three platforms without any hassle.

I want you to set up an EC2 instance. Make sure it’s accessible to the public and allow SSH access to it. Also, use the Amazon Linux AMI for the setup.

Now SSH to that instance using aws console connect.

now Make 3 folders as shown below:

mkdir awslab
mkdir gcplab
mkdir azurelab

To install terraform see below link: It has explanation for All three cloud providers AWS , GCP and AZURE

after installation you check by version may vary

now cd awslab

we will create some basic resources just to check everything is good:

create below shown awslab.tf file inside awslab folder

# AWS Provider Configuration
provider "aws" {
region = "ca-central-1" # Change to your desired region
}



# Create AWS VPC
resource "aws_vpc" "my_vpc" {
cidr_block = "10.0.0.0/16"
}

# Create AWS Internet Gateway
resource "aws_internet_gateway" "my_igw" {
vpc_id = aws_vpc.my_vpc.id
}

# Create AWS Route Table
resource "aws_route_table" "my_route_table" {
vpc_id = aws_vpc.my_vpc.id

route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.my_igw.id
}
}

# Create AWS Subnet
resource "aws_subnet" "my_subnet" {
vpc_id = aws_vpc.my_vpc.id
cidr_block = "10.0.1.0/24"
availability_zone = "ca-central-1a"
}

# Associate AWS Subnet with Route Table
resource "aws_route_table_association" "my_subnet_association" {
subnet_id = aws_subnet.my_subnet.id
route_table_id = aws_route_table.my_route_table.id
}

Now Execute the commands Shown Below:

terraform init #this will initialize the terraform provider which you can see in above version check ss
terraform validate #it should show success message
terraform plan #this will check if you have required permission given to terraform or not

if permissions not given go to IAM in aws console create Key:

IAM>users>username inside this you will see Security credentials.

create key store it safe somewhere.

now run command shown below in EC2 terminal:

export AWS_ACCESS_KEY_ID="RKIA3tfhyruLL4HTYRKXN" #this is sample keys your key will look something like this
export AWS_SECRET_ACCESS_KEY="Ertn/PRu5lw4gtgy3vMK/g73HCOrtfgyz8T9Q4u7n" #same way here change this with your original keys

This will give access to your terraform to execute the commands in aws :

For learning purposes, let’s simplify the process and skip creating different roles. Let’s just set it up as described.

for IAC cloudformation project check my this blog:

Terraform plan #now this will success and you won`t get error
Terraform apply #boom you have resources created

Terraform destroy #now this will delete all resources which you created

now cd gcplab

in this first create service account as shown below:

in this create service account add key and download that key store it at safe place:

Now from your local terminal upload that .json key to ec2 instance so that our Terraform can have access to GCP account.

scp -i /Users/rajpatel/Documents/Key/key.pem /Users/rajpatel/Downloads/your.json ec2-user@3.96.312.211:~ #remove this paths and add your original path

now as you have your file in home directory of ec2 instance

create gcplab.tf file as shown below

# Google Cloud Provider Configuration
provider "google" {
credentials = file("/home/ec2-user/project.json") #this is going to be your json file path in ec2 instance
project = "thinking-anthem-401817" #this is your GCP google project ID
region = "us-central1"
}

# Create GCP Network
resource "google_compute_network" "my_network" {
name = "my-network"
}

# Create GCP Subnet
resource "google_compute_subnetwork" "my_subnet" {
name = "my-subnet"
region = "us-central1"
network = google_compute_network.my_network.self_link
ip_cidr_range = "10.0.2.0/24"
}

# Create GCP Route
resource "google_compute_route" "my_route" {
name = "my-route"
network = google_compute_network.my_network.name
dest_range = "0.0.0.0/0"
next_hop_gateway = "default-internet-gateway"
}

And now You can Follow The Commands:

terraform init #this will initialize the terraform provider which you can see in above version check ss
terraform validate #it should show success message
terraform plan #this will check if you have required permission given to terraform or not
terraform apply
terraform destroy #to delete all resources created

now cd azurelab

Here You need to install azure-cli in terminal to access azure resources from ec2

follow which ever is applicable for your system Yum commands or apt-get commands both wont execute.

sudo apt-get update
sudo apt-get install azure-cli
sudo yum install azure-cli
sudo apt-get update && sudo apt-get install --only-upgrade -y azure-cli
sudo tdnf install azure-cli
curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash
sudo apt-get install azure-cli
sudo yum install azure-cli
sudo rpm --import https://packages.microsoft.com/keys/microsoft.asc
sudo apt-get update
sudo yum update
sudo sh -c 'echo -e "[azure-cli]
name=Azure CLI
baseurl=https://packages.microsoft.com/yumrepos/azure-cli
enabled=1
gpgcheck=1
gpgkey=https://packages.microsoft.com/keys/microsoft.asc" > /etc/yum.repos.d/azure-cli.repo'
sudo yum install azure-cli
az login

az login will give you link and code to enter so open that link and enter code given in link like below

now create file azurelab.tf and paste code below:

# Azure Provider Configuration
provider "azurerm" {
features {}
}

# Create Azure Resource Group
resource "azurerm_resource_group" "my_resource_group" {
name = "my-resource-group"
location = "East US" # Change to your desired Azure region
}

# Create Azure Virtual Network
resource "azurerm_virtual_network" "my_virtual_network" {
name = "my-vnet"
address_space = ["10.3.0.0/16"]
location = azurerm_resource_group.my_resource_group.location
resource_group_name = azurerm_resource_group.my_resource_group.name
}

And run the same commands above.

NOTE: Dont forget to do terraform destroy this will destroy all the resources you have created.

Thank you so much If you liked the read do follow and clap that motivates to right good content for you.

#azure #gcp #aws #incloudbyrk #soonincloud #terraform

— — — — — — — — — — — — — — — The End — — — — — — — — — — — — —

If you enjoyed reading this blog, please share it with your friends and make sure to subscribe to our YouTube channel for more exciting content. Help us spread the word about our expertise in MERN stack development, cloud computing, React Native, and Next.js, and stay tuned for more informative articles to come. Together, we can take the tech world by storm!

In the Mern Stack Projects section you can find tutorials, tips, cheat sheets, and other projects. Our talks also focus on React Frameworks like NextJs,AWS Cloud So join us today to keep up with the latest technology🩷

📠 🏅:- Mern Stack Projects

🎦 🥇:- Jara diary — YouTube 🚦

🎦 🥈 :- Errormania — YouTube 🚦

On GITHUB :- raj-28 (Raj) (github.com)

💌 Do Subscribe To Our Mailing List To stay Updated With All New Blogs 👍

…………🚦…🛣… ……………🚧🛑………………..▶……………….️

Use Emojis From Here In Case You Need Any….🚦🛣️🥇🥈🥉🏅🎖️🎦👍

--

--

Master raj

Certified MERN Stack Developer from Infosys, now sharing expertise on #InCloudByRK. Proficient in MERN Stack, AWS, GCP, Azure DevOps. Let's level up!