Day 77 of 100 Days : Terraform Providers 🎉

·

3 min read

Hello, fellow DevOps enthusiasts! Today, we’re diving into the world of Terraform providers. This journey is all about understanding how Terraform interacts with the services we’re building infrastructure on. Let’s explore this exciting topic with some simple, fun examples and explanations!

In the Terraform universe, a provider is like a translator that allows Terraform to communicate with a service’s API. Whether it’s a cloud provider like AWS or Azure, or platforms like Kubernetes, providers are the bridge between Terraform and the magic of infrastructure automation.

  • aws: Manages AWS resources.

  • azurerm: Handles Azure services.

  • google: Works with Google Cloud Platform.

  • kubernetes: Manages Kubernetes clusters.

  • openstack: Connects to OpenStack.

  • vsphere: Interacts with VMware vSphere.

With Terraform, you can use these providers to define, manage, and automate your infrastructure efficiently.


Terraform allows you to set up providers in several ways based on your project’s needs. Here are the key methods:

The most common method. The provider block resides in the root module, making it accessible to all resources. For example:

provider "aws" {
  region = "us-east-1"
}

When you need to reuse a provider’s configuration in multiple modules, define it within a child module:

provider "azurerm" {
  features = {}
}

To ensure consistency, specify providers and their versions explicitly:

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.0"
    }
    azurerm = {
      source  = "hashicorp/azurerm"
      version = ">= 3.0, < 4.0"
    }
  }
}

Terraform lets you use multiple providers seamlessly in a single project. Here’s how you can define and use them:

provider "aws" {
  region = "us-east-1"
}

provider "azurerm" {
  subscription_id = "your-azure-subscription-id"
  client_id       = "your-azure-client-id"
  client_secret   = "your-azure-client-secret"
  tenant_id       = "your-azure-tenant-id"
}

resource "aws_instance" "web" {
  ami           = "ami-12345678"
  instance_type = "t2.micro"
}

resource "azurerm_virtual_machine" "vm" {
  name     = "example-vm"
  location = "eastus"
  size     = "Standard_B1ls"
}

In this example, we’ve defined AWS and Azure providers to manage resources across both platforms.


To build infrastructure in multiple regions, Terraform uses the alias keyword:

provider "aws" {
  alias  = "us-east"
  region = "us-east-1"
}

provider "aws" {
  alias  = "us-west"
  region = "us-west-2"
}

resource "aws_instance" "east_instance" {
  ami           = "ami-12345678"
  instance_type = "t2.micro"
  provider      = aws.us-east
}

resource "aws_instance" "west_instance" {
  ami           = "ami-12345678"
  instance_type = "t2.micro"
  provider      = aws.us-west
}

Now, Terraform knows how to create resources in multiple regions effortlessly.


Here’s a complete example using the required_providers block:

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.0"
    }
  }
}

provider "aws" {
  region = "us-east-1"
}

resource "aws_instance" "example" {
  ami           = "ami-12345678"
  instance_type = "t2.micro"
}

This ensures Terraform uses the specified AWS provider version to maintain compatibility.


Terraform providers are the backbone of your infrastructure automation. With them, you can:

  • Interact with various APIs.

  • Manage multi-cloud and multi-region setups.

  • Ensure consistency and stability with version constraints.

As you practice, remember to experiment with multiple providers and configurations to see their power in action. And don’t forget to have fun while building your infrastructure — Terraform makes it as simple as writing a story about your resources. 🌟

Stay tuned for more Terraform adventures tomorrow. Until then, happy coding! 🚀