Hey Infra Coders, Let’s Talk Providers!
Welcome back, Infra coders! You’ve already written your first Terraform configuration and created some cool AWS resources like an S3 bucket and an EC2 instance. Now, let’s take it up a notch by exploring providers—the magic behind Terraform’s ability to work with different cloud platforms. In this article, we’ll learn how to manage providers for AWS, Azure, and Google Cloud Platform (GCP) in the same Terraform project. Think of providers as your bridge to different clouds, and we’re about to become bridge-building experts!
We’ll set up a configuration that creates resources across AWS, Azure, and GCP, and I’ll walk you through each step in plain English. Ready? Let’s dive in!
What Are Providers in Terraform?
A provider in Terraform is like a translator that lets Terraform talk to a specific platform, like AWS, Azure, or GCP. Each provider has its own plugin that knows how to create, update, or delete resources on that platform. Terraform supports hundreds of providers, from cloud platforms to tools like Kubernetes or GitHub, but today we’ll focus on the big three: AWS, Azure, and GCP.
You can use multiple providers in one Terraform project, which is super powerful for hybrid or multi-cloud setups. For example, you might store files in an AWS S3 bucket, run a virtual machine in Azure, and manage a database in GCP—all with one Terraform configuration.
Step 1: Set Up Your Project
Let’s create a new folder for this project called multi-cloud
. Inside it, create a file named main.tf
. This will hold our configuration for all three providers. You’ll need accounts for AWS, Azure, and GCP, but if you don’t have all of them, you can still follow along and test with the ones you have.
Prerequisites
Before we start, make sure you have:
- Terraform installed (check with
terraform -version
). - An AWS account with access keys set up in
~/.aws/credentials
. - An Azure account with a service principal (we’ll cover how to set this up).
- A GCP account with a service account key (we’ll set this up too).
- A text editor like VS Code.
Step 2: Configure the AWS Provider
Let’s start with AWS, since you’re already familiar with it. Add this to your main.tf
:
provider "aws" {
region = "us-east-1"
}
This sets up the AWS provider in the us-east-1
region. Your credentials in ~/.aws/credentials
will be used automatically. We’ll create an S3 bucket later in the configuration.
Step 3: Configure the Azure Provider
Next, let’s add Azure. To use Terraform with Azure, you need a service principal for authentication. Here’s how to set it up:
Get Azure Credentials
- Log into the Azure Portal.
- Go to Azure Active Directory → App registrations → New registration.
- Create an app, then note the Application (client) ID and Directory (tenant) ID.
- Create a Client secret under Certificates & secrets and save it.
- Go to Subscriptions, note your Subscription ID, and assign your app Contributor role under Access control (IAM).
Now, add the Azure provider to main.tf
:
provider "azurerm" {
features {}
subscription_id = "your-subscription-id"
client_id = "your-client-id"
client_secret = "your-client-secret"
tenant_id = "your-tenant-id"
}
Replace the placeholders with your actual Azure credentials. The features {}
block is required for the Azure provider.
Step 4: Configure the GCP Provider
For GCP, you need a service account key. Here’s how to get it:
Get GCP Credentials
- Go to the GCP Console.
- Navigate to IAM & Admin → Service Accounts → Create Service Account.
- Give it a name, grant Editor role (for this tutorial), and create a JSON key.
- Download the JSON key file and save it securely (e.g.,
gcp-key.json
).
Add the GCP provider to main.tf
:
provider "google" {
project = "your-gcp-project-id"
region = "us-central1"
credentials = file("gcp-key.json")
}
Replace your-gcp-project-id
with your GCP project ID, and make sure gcp-key.json
is in your multi-cloud
folder.
Step 5: Create Resources Across Providers
Now, let’s create one resource in each cloud to see providers in action:
- AWS: An S3 bucket.
- Azure: A resource group.
- GCP: A Cloud Storage bucket.
Add this to main.tf
:
# AWS S3 Bucket
resource "aws_s3_bucket" "my_aws_bucket" {
bucket = "my-unique-bucket-12345"
}
# Azure Resource Group
resource "azurerm_resource_group" "my_azure_group" {
name = "my-terraform-group"
location = "East US"
}
# GCP Cloud Storage Bucket
resource "google_storage_bucket" "my_gcp_bucket" {
name = "my-unique-gcp-bucket-12345"
location = "US"
}
What’s happening here?
- AWS: Creates an S3 bucket (use a unique name).
- Azure: Creates a resource group, a container for Azure resources.
- GCP: Creates a Cloud Storage bucket (use a unique name).
Pro Tip
Bucket names in AWS and GCP must be globally unique, so add random numbers or your name to avoid conflicts.
Step 6: Run Your Terraform Commands
Let’s bring this configuration to life. In your multi-cloud
folder, run these commands:
Initialize Terraform
terraform init
This downloads plugins for AWS, Azure, and GCP providers.
Preview the Plan
terraform plan
Terraform will show a plan to create one resource in each cloud. Double-check the output.
Apply the Configuration
terraform apply
Type yes
to confirm, and Terraform will create the resources. Check your AWS, Azure, and GCP consoles to see the bucket, resource group, and storage bucket.
Step 7: Clean Up
To avoid costs, clean up by running:
terraform destroy
Type yes
to delete all resources. Verify in each cloud console that they’re gone.
Your Complete main.tf File
Here’s the full main.tf
for reference:
provider "aws" {
region = "us-east-1"
}
provider "azurerm" {
features {}
subscription_id = "your-subscription-id"
client_id = "your-client-id"
client_secret = "your-client-secret"
tenant_id = "your-tenant-id"
}
provider "google" {
project = "your-gcp-project-id"
region = "us-central1"
credentials = file("gcp-key.json")
}
resource "aws_s3_bucket" "my_aws_bucket" {
bucket = "my-unique-bucket-12345"
}
resource "azurerm_resource_group" "my_azure_group" {
name = "my-terraform-group"
location = "East US"
}
resource "google_storage_bucket" "my_gcp_bucket" {
name = "my-unique-gcp-bucket-12345"
location = "US"
}
Tips for Managing Multiple Providers
You’re now managing multiple clouds—nice work! Here are some tips to keep things smooth:
- Secure Credentials: Never hardcode secrets in
.tf
files. Use files like~/.aws/credentials
or environment variables. - Organize Providers: For complex projects, put each provider’s resources in separate
.tf
files (e.g.,aws.tf
,azure.tf
). - Check Free Tiers: Use free-tier resources to avoid unexpected costs.
- Use Version Control: Save your code in Git to track changes and collaborate.
What’s Next?
You just built a multi-cloud setup with Terraform—pretty awesome, right? In the next article, we’ll dive into Terraform state management, learning how Terraform keeps track of your infrastructure and how to manage it safely. Keep your multi-cloud
folder handy, and let’s keep building, Infra coders!
The post Managing Providers in Terraform: AWS, Azure, and GCP appeared first on TecAdmin.
Source: Read More