0
0
TerraformHow-ToBeginner · 4 min read

How to Use Provider from Registry in Terraform

To use a provider from the Terraform Registry, declare it in the terraform block with the required_providers argument specifying the source and version. Then configure the provider in a provider block to set any required settings.
📐

Syntax

The terraform block declares which providers your configuration needs, specifying the source from the Terraform Registry and the version. The provider block configures the provider settings like credentials or region.

  • terraform block: Defines required providers and versions.
  • provider block: Sets provider-specific configuration.
hcl
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.0"
    }
  }
}

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

Example

This example shows how to use the AWS provider from the Terraform Registry. It declares the provider source and version, then configures the AWS region.

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

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

resource "aws_s3_bucket" "example" {
  bucket = "my-unique-bucket-12345"
  acl    = "private"
}
Output
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
⚠️

Common Pitfalls

Common mistakes include:

  • Not specifying the source in required_providers, which causes Terraform to fail finding the provider.
  • Omitting the provider block configuration, leading to missing required settings like region or credentials.
  • Using an incorrect provider name or version syntax.

Always check the provider documentation for correct source and configuration options.

hcl
terraform {
  required_providers {
    aws = {
      version = "~> 4.0"  # Missing source causes error
    }
  }
}

# Correct way includes source:
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.0"
    }
  }
}
📊

Quick Reference

ConceptDescriptionExample
required_providersDeclares provider source and versionaws = { source = "hashicorp/aws" version = "~> 4.0" }
provider blockConfigures provider settingsprovider "aws" { region = "us-west-2" }
sourceRegistry address for provider"hashicorp/aws"
versionProvider version constraint"~> 4.0"

Key Takeaways

Always declare providers with source and version in the terraform block.
Configure provider settings like region in the provider block.
Missing source in required_providers causes Terraform to fail.
Check provider docs for correct source and configuration options.
Use version constraints to control provider updates safely.