Scalable Terraform Configuration Design · Topic 4 of 4
Providers and the Terraform MCP Server
Terraform Professional 002: provider requirements, aliases passed to modules, lock file behavior and connecting AI agents with the Terraform MCP server.
- 5 min
required_providersholds source and version;providerblocks hold configuration.- Provider arguments can use variables, but their values must be known before apply.
- An alias needs the
providermeta-argument on a resource and theprovidersmap on a module.
What it is
A provider is the plugin that talks to an API. Configuring one means three separate things: declaring which plugin and version you need, setting its arguments, and deciding which resources use which configuration.
The Terraform MCP server gives AI coding agents real-time access to Terraform registry data, so generated code can follow current provider documentation. It can also reach HCP Terraform and Terraform Enterprise when given a token.
How it works
Requirements, configuration and selection
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 6.0"
}
}
}
| Piece | Where it lives | Notes |
|---|---|---|
| Source and version constraint | required_providers | Every module declares what it needs; requirements are not inherited |
| Region, credentials and so on | provider block in the root module | Values must be known before apply, so no computed resource attributes |
alias | provider block | Creates an extra configuration, referenced as aws.west |
provider meta-argument | Resource or data source | Selects the alias; without it the default configuration is used |
The version argument in a provider block is deprecated and only produces a warning. Use required_providers.
If every block for a provider has an alias, Terraform creates an empty default configuration. Resources that use it fail when the provider requires arguments.
Passing an alias into a module
Default configurations pass to child modules implicitly. An alias must be passed explicitly, and the child must declare that it expects one.
module "replica" {
source = "./replica"
providers = {
aws = aws
aws.west = aws.west
}
}
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
configuration_aliases = [aws.west]
}
}
}
The first block is in the parent and the second is in the child module. Inside the child, a resource then sets provider = aws.west.
Keep provider blocks out of shared modules. A module that defines its own provider configuration cannot be used with count, for_each or depends_on.
Version constraints and the lock file
| Constraint | Allows |
|---|---|
"5.40.0" | Exactly that version |
">= 5.0" | That version and anything newer, including new major versions |
"~> 5.40.0" | 5.40.x only |
"~> 5.40" | 5.40 and later 5.x |
.terraform.lock.hcl records the selected provider version, its constraints and package hashes. Commit it, and never edit it by hand. It does not pin module versions.
| Situation | What terraform init does |
|---|---|
| Lock file exists and satisfies constraints | Reuses the locked version, even if a newer one exists |
| No lock file | Selects the newest allowed version and writes the file |
| Constraint changed and lock conflicts | Fails with "does not match configured version constraint"; run terraform init -upgrade |
-upgrade | Ignores the recorded selections and picks the newest allowed |
A team on several operating systems records hashes for each platform, so that init does not fail on another machine.
terraform providers lock -platform=linux_amd64 -platform=darwin_arm64
The Terraform MCP server
MCP (Model Context Protocol) is an open standard that connects AI models to tools and data. The server runs locally or self-hosted, and HashiCorp also offers a remote server for HCP Terraform users.
docker run -i --rm hashicorp/terraform-mcp-server
| Toolset | Reaches | Token |
|---|---|---|
registry | Public providers, modules and policies | None |
registry-private | Private registry modules and providers | TFE_TOKEN |
terraform | HCP Terraform and Enterprise workspaces, runs, variables | TFE_TOKEN |
| Setting | Meaning |
|---|---|
| Transport | STDIO by default; Streamable HTTP is the alternative |
TFE_ADDRESS | Defaults to https://app.terraform.io |
ENABLE_TF_OPERATIONS | Defaults to false; gates destructive tools and auto approved runs |
For security, run a local server at 127.0.0.1 and limit the token's permissions. A remote deployment needs controls such as TLS, authentication and IP allowlisting. Treat what an agent generates as a draft, and run it through the usual validate, plan and review steps.
How the exam asks about this
- "An AI assistant keeps generating outdated provider arguments." Connect it to the Terraform MCP server, which needs no token for public registry data.
- "A module must deploy to two regions." Declare configuration_aliases in the child and pass aliases with the providers map.
- "init fails with a constraint mismatch after a version change." Run terraform init -upgrade, then review the lock file and the plan.
What to remember for the exam
required_providersholds source and version;providerblocks hold configuration.- Provider arguments can use variables, but their values must be known before apply.
- An alias needs the
providermeta-argument on a resource and theprovidersmap on a module. - The lock file pins provider versions and hashes, not modules; commit it.
- The MCP server defaults to STDIO, needs no token for the public registry, and needs
TFE_TOKENfor private and workspace tools.
Installing a newer provider does not help an AI agent. Give it current documentation through a tool connection such as the MCP server.
Common mistakes
Deleting the lock file to force an upgrade. It throws away the recorded hashes as well as the versions. Change the constraint if needed, and run terraform init -upgrade.
Assuming a child module inherits aliases. Only default configurations pass implicitly. Without configuration_aliases and the providers map, referencing the alias raises an error.
Giving the MCP server a broad token by default. Public registry lookups need no token at all. Add TFE_TOKEN only when workspace or private registry tools are needed, and limit its permissions.
Check yourself
Where should a provider version constraint go?
In the
required_providersblock insideterraform. Theversionargument in aproviderblock is deprecated and only produces a warning.Why does
terraform initkeep an older provider when a newer one satisfies the constraint?The lock file records the selected version, and init reuses it.
terraform init -upgradeselects the newest allowed version.What does the Terraform MCP server need to look up public provider documentation?
No token.
TFE_TOKENis needed only for the private registry and for HCP Terraform or Terraform Enterprise tools.
Topic 4 of 4 in Scalable Terraform Configuration Design
Test yourself on Scalable Terraform Configuration Design
The full HashiCorp Terraform Infrastructure Engineer Professional question bank has 600 questions, with timed practice exams and an explanation for every answer.
This study guide is independent and is not affiliated with, endorsed by, or sponsored by HashiCorp. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.