Terraform by itself knows nothing about AWS, Azure, or any cloud. It needs a "translator" that knows how to talk to each platform. That translator is the provider. In this subchapter, you'll understand what a provider is, how Terraform communicates with AWS, and how it is configured. It's the piece that connects your code to the real cloud.

What is a provider

A provider is a plugin that teaches Terraform how to communicate with a specific platform. The AWS provider knows how to create, read, modify, and delete AWS resources through its API.

Analogy: Terraform is like a person who wants to give instructions in many different countries. The provider is the interpreter/translator who knows the language of each country. The AWS provider "speaks AWS"; the Azure provider "speaks Azure." You give instructions in HCL and the provider translates them into the language of the corresponding cloud.

This is what makes Terraform multi-cloud (remember Chapter 9): the same Terraform uses different providers to talk to different platforms. There are hundreds of providers: AWS, Azure, GCP, Kubernetes, GitHub, Cloudflare, and many more.

How Terraform communicates with AWS

The flow, simplified, is this:

Your HCL code  →  Terraform  →  AWS Provider  →  AWS API  →  Your infrastructure
  (.tf)          (the engine)   (the translator)   (internet)      (real resources)
  1. You write what you want in HCL (resource "aws_instance"...).
  2. Terraform processes your code.
  3. The AWS provider translates that into AWS API calls (the same ones used by the web console behind the scenes).
  4. AWS creates/modifies/deletes the real resources.

Every resource that starts with aws_ (like aws_instance, aws_s3_bucket) is managed by the AWS provider.

How to declare the provider

In your code, you declare which provider you want to use and configure it. This usually goes in a terraform block (to set the version) and a provider block:

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

provider "aws" {
  region = "eu-west-1"
}

Let's break it down:

  • The terraform { required_providers { ... } } block declares that you need the AWS provider, where to download it from (hashicorp/aws), and which version (~> 5.0 means "version 5.x").
  • The provider "aws" { ... } block configures it: here you specify, for example, the region to work in (remember Chapter 3).

Why setting the version matters: providers are updated and sometimes change their behavior. Setting the version (~> 5.0) ensures your code keeps working the same even if new versions come out. It's a good practice to avoid surprises.

Authentication: how does Terraform prove who it is?

For AWS to allow it to create resources, Terraform needs credentials (remember IAM, Chapter 7). But, very importantly, credentials are not written in the code. There are secure ways to provide them:

Method How it works When to use it
Configured AWS CLI Terraform uses the credentials from your machine's AWS CLI Local development
Environment variables AWS_ACCESS_KEY_ID, etc. in the environment Local and automation
IAM Role The machine (EC2) or CI/CD system assumes a role Production and CI/CD (the best)

⚠️ Critical security rule: NEVER write access keys inside your .tf code. If you push it to Git, you would be leaking your credentials (remember the disasters from Chapter 7). Instead, use the AWS CLI, environment variables, or, best of all, IAM roles (subchapter 7.4). Terraform automatically picks them up from the environment, without them appearing in the code.

terraform init: downloading the provider

Before you can use a provider, you have to download it. This is done by the terraform init command, which is the first command you run in any Terraform project.

terraform init
  → downloads the AWS provider (and any others you use)
  → prepares the working directory
  → "Terraform has been successfully initialized!"

init reads your configuration, downloads the necessary providers, and gets everything ready for plan and apply (remember the cycle from subchapter 9.4). You run it:

  • The first time you start a project.
  • Every time you add a new provider or change its version.
  • (It also configures the state backend, which we'll see in subchapter 11.3.)

So, the complete Terraform cycle is actually: initplanapply → ... → destroy.

Multiple providers and aliases

For advanced cases, you can use multiple providers at once or the same provider with different configurations. For example, deploying to two AWS regions using two AWS provider configurations with aliases:

provider "aws" {
  region = "eu-west-1"      # default provider
}

provider "aws" {
  alias  = "us"
  region = "us-east-1"      # a second provider for another region
}

This is useful for multi-region architectures (Chapter 26) or multi-account (Chapter 30). Don't worry about this now; just know that it's possible.

What you should remember

  • A provider is the "translator" that allows Terraform to talk to a platform. The AWS provider translates your HCL into AWS API calls.
  • Thanks to providers, Terraform is multi-cloud (there are hundreds: AWS, Azure, GCP, Kubernetes…).
  • It is declared with required_providers (setting the version, good practice) and configured with the provider block (region, etc.).
  • Credentials NEVER go in the code: use AWS CLI, environment variables, or, better, IAM roles.
  • terraform init downloads the providers and is the first command you run in a project.

In the next subchapter, we'll see Terraform's most characteristic (and sometimes confusing) concept: the state file (terraform.tfstate) and why it is so important.

Cloud, AWS & Terraform — From Zero to Expert

Chapter 1 · What is cloud computing

Chapter 2 · The cloud market and major providers

Chapter 3 · Regions, availability zones and edge

Chapter 4 · Compute: EC2

Chapter 5 · Storage: S3

Chapter 6 · Networking: VPC

Chapter 7 · Identity and access: IAM

Chapter 8 · Managed databases

Chapter 9 · Why Infrastructure as Code

Chapter 10 · HCL: the Terraform language

Chapter 11 · Providers and state

Chapter 12 · Your first real infrastructure in Terraform

Chapter 13 · Load balancing and auto scaling

Chapter 14 · Serverless with Lambda

Chapter 15 · Messaging and events

Chapter 16 · Content delivery and DNS

Chapter 17 · Containers on AWS

Chapter 18 · Modules: reuse and composition

Chapter 19 · Workspaces and environment management

Chapter 20 · Remote backends and locking

Chapter 21 · Infrastructure testing

Chapter 22 · Terraform in CI/CD

Chapter 23 · Defense in depth

Chapter 24 · Observability: logs, metrics and traces

Chapter 25 · Cost optimization

Chapter 26 · High availability and disaster recovery

Chapter 27 · AWS Well-Architected Framework

Chapter 28 · Serverless architectures at scale

Chapter 29 · Data platforms on AWS

Chapter 30 · Multi-account and landing zones

Chapter 31 · Platform Engineering and Internal Developer Platform

Chapter 32 · Relevant AWS certifications

Chapter 33 · Projects to consolidate what you've learned

Chapter 34 · Resources and community

© Copyright 2024. All rights reserved