liveBook · Manning

Required_providers block

As explained in Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu

A required_providers block is a Terraform configuration block that declares the providers a configuration requires and can constrain their versions.

A required_providers block declares the providers a Terraform configuration needs, including each provider’s source address and acceptable version. It belongs inside the terraform settings block and acts like a dependency declaration. The provider block remains separate: required_providers controls installation, while provider configures details such as the AWS region. section 2.1.3 section 2.3

Listing 2.1 Configuring required providers
terraform {                               #1
  required_providers {                    #2
    aws = {                               #3
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
  }
}
 
provider "aws" {                          #4
  region = "us-east-1"                    #5
}

A typical AWS configuration pairs an explicit provider source and version constraint with a separate region configuration. section 2.1.3

Use explicit declarations to make provider selection predictable:

  • Set source to identify the provider, such as hashicorp/aws. section 2.4.2
  • Set version to constrain compatible releases, such as ~> 5.0 or ~> 4.0. section 2.1.3 section 2.4.2
  • Declare providers even when Terraform can infer them from resource names; inference assumes the resource’s first name is a local provider name and defaults the namespace to hashicorp. section 2.4.2 section 2.1.3
  • Keep provider settings, such as region, in a provider block rather than treating them as installation requirements. section 2.1.3

Configuring a reusable module’s test workspace

Place the required_providers block in the root-level workspace when a reusable module no longer contains provider blocks. Combine it with a region variable and provider block, then run terraform init followed by terraform apply after reviewing the plan. section 3.8.4

How Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu covers it

How deep it goes
Defines provider sources and version constraints, while leaving operational settings such as region to provider blocks.
Examples
Uses AWS declarations with hashicorp/aws and ~> 4.0 or ~> 5.0 constraints, plus region configuration and module testing.
What it assumes
Assumes familiarity with Terraform HCL blocks and resource names, while teaching provider dependencies and provider configuration separately.
What you can do after
Operate/configure Terraform with explicit provider installation requirements and predictable version selection.
How it connects
Connects the terraform block with provider blocks, resources, data sources, modules, and input variables.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu. The section links point to the parts of the book it draws on.