Course contentsShow
← Cloud and Infrastructure
Lesson 727 of 2,98914. Terraform FundamentalsFree lesson

Terraform Block and Required Providers

Configuring the terraform block to specify required Terraform version and provider requirements.

Terraform Block and Required Providers

What you'll learn: How to configure the terraform block to declare your Terraform version and provider requirements, ensuring consistent infrastructure deployments.


What Is the Terraform Block?

Think of the terraform block as the settings panel for your entire Terraform configuration. It's a special block where you define global requirements—specifically which version of Terraform itself should run your code, and which providers (like AWS, Azure, or Google Cloud) you'll need with their version constraints.

This block sits at the top level of your configuration files (not inside a resource or module) and acts as a contract: "This infrastructure code needs these tools to work correctly."

Why Required Providers Matter

Without declaring provider requirements, Terraform would have to guess which provider versions to download. Different versions might behave differently, causing unexpected results. By explicitly stating requirements, you ensure:

  • Consistency across team members and environments
  • Compatibility with your existing code
  • Stability by preventing accidental upgrades that break things

Basic Structure

terraform {
  required_version = ">= 1.0"

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

Here, required_version ensures Terraform itself is at least version 1.0, while required_providers specifies you need the AWS provider from HashiCorp's registry, version 5.x (the ~> means "at least 5.0, but below 6.0").

The source tells Terraform where to download the provider from—think of it as the provider's address.

Key Takeaway: The terraform block is your configuration's foundation, declaring version constraints for both Terraform and providers to ensure predictable, reproducible infrastructure deployments.