Loading...
Loading...
Configure providers and declare core infrastructure resources
A provider is a plugin that Terraform uses to interact with a cloud platform (Azure, AWS, GCP). Each provider exposes resource types (VMs, databases, networks) that you can declare.
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~> 3.0"
}
}
}
provider "azurerm" {
features {}
}| Block | What it does |
|---|---|
| `terraform {}` | Global Terraform settings |
| `required_providers` | Declares which providers and versions to use |
| `provider "azurerm" {}` | Configures the provider (auth, region defaults) |
| `features {}` | Required boilerplate for AzureRM provider |
# Provider configuration
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = ">= 3.0"
}
}
}
provider "azurerm" {
features {}
}
# Resources
resource "azurerm_resource_group" "main" {
name = "production-rg"
location = "East US"
}
resource "azurerm_storage_account" "data" {
name = "proddata123"
resource_group_name = azurerm_resource_group.main.name
location = azurerm_resource_group.main.location
account_tier = "Standard"
account_replication_type = "GRS"
}When you run terraform init:
When you run terraform plan:
| Wrong | Why | Right |
|---|---|---|
| Running `terraform apply` before `init` | Providers aren't downloaded yet | Always run `terraform init` first |
| Misspelling provider source | Provider not found in registry | `hashicorp/azurerm` not `azurerm/azure` |
| Hard-coding resource group name across resources | Breaks if you rename it | Always reference via `azurerm_resource_group.main.name` |
Create a Terraform config that:
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = ">= 3.0"
}
}
}
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "main" {
name = "my-storage-rg"
location = "East US"
}
resource "azurerm_storage_account" "data" {
name = "mystorage$(random_integer.suffix.result)"
resource_group_name = azurerm_resource_group.main.name
location = azurerm_resource_group.main.location
account_tier = "Standard"
account_replication_type = "LRS"
}