0
0
Terraformcloud~5 mins

Terraform provider ecosystem - Commands & Configuration

Choose your learning style9 modes available
Introduction
Terraform providers are plugins that let Terraform talk to different cloud services or tools. They help you create, change, and delete resources like servers, databases, or networks by understanding how to work with each service.
When you want to manage cloud resources like virtual machines or storage buckets with code.
When you need to automate infrastructure setup across multiple cloud platforms.
When you want to use Terraform to control services like DNS, databases, or monitoring tools.
When you want to share or reuse infrastructure configurations that depend on specific providers.
When you want to keep your infrastructure consistent and repeatable using Terraform modules.
Config File - main.tf
main.tf
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.0"
    }
  }
  required_version = ">= 1.0"
}

provider "aws" {
  region = "us-east-1"
}

resource "aws_s3_bucket" "example" {
  bucket = "my-terraform-example-bucket"
  acl    = "private"
}

This file tells Terraform which provider to use and its version under required_providers. The provider block configures the provider with settings like the AWS region. The resource block creates an S3 bucket using the AWS provider.

Commands
This command downloads the provider plugins and sets up the working directory for Terraform to run.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... - Finding hashicorp/aws versions matching "~> 4.0"... - Installing hashicorp/aws v4.60.0... - Installed hashicorp/aws v4.60.0 (signed by HashiCorp) Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure.
This command shows what Terraform will do to reach the desired state defined in the configuration, without making any changes yet.
Terminal
terraform plan
Expected OutputExpected
Refreshing Terraform state in-memory prior to plan... An execution plan has been generated and is shown below. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: # aws_s3_bucket.example will be created + resource "aws_s3_bucket" "example" { + acl = "private" + bucket = "my-terraform-example-bucket" + id = (known after apply) } Plan: 1 to add, 0 to change, 0 to destroy.
This command applies the changes to create or update resources as planned. The flag skips the confirmation prompt.
Terminal
terraform apply -auto-approve
Expected OutputExpected
aws_s3_bucket.example: Creating... aws_s3_bucket.example: Creation complete after 2s [id=my-terraform-example-bucket] Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
-auto-approve - Skip manual approval to apply changes immediately
This command lists all providers used in the current configuration and their versions.
Terminal
terraform providers
Expected OutputExpected
Providers required by configuration: . ├── provider["registry.terraform.io/hashicorp/aws"] 4.60.0
Key Concept

If you remember nothing else from this pattern, remember: Terraform providers are the bridge that lets Terraform manage resources in different services by understanding their APIs.

Common Mistakes
Not running 'terraform init' before other commands
Terraform won't download the required providers and will fail to run plans or applies.
Always run 'terraform init' first to set up providers and modules.
Using incompatible provider versions without specifying them
Terraform might download a version that doesn't work with your configuration, causing errors.
Specify provider versions in 'required_providers' to ensure compatibility.
Changing provider configuration without reinitializing
Terraform may use outdated provider settings leading to unexpected behavior.
Run 'terraform init' again after changing provider blocks to update plugins.
Summary
Use 'terraform init' to download and set up providers before managing infrastructure.
'terraform plan' shows what changes Terraform will make using the providers.
'terraform apply' creates or updates resources through the providers as defined.
'terraform providers' lists all providers and their versions used in your setup.