# Configure the Azure provider
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~> 2.65"
}
}
required_version = ">= 0.14.9"
}
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "rg" {
name = "myTFResourceGroup"
location = "westus2"
}
- The
Terraform block
contains Terraform settings, including the required providers Terraform will use to provision your infrastructure.For each provider, the
Source
attribute defines:
- Optional Hostname
- Namespace
- Provider Type
Terraform installs providers from the
Terraform Registry
by default.
- In the above example config, the
azurem
provider's source is defined ashashicorp/azurem
, which is shorthand forregistry.terraform.io/hashicorp/azurem
.A version constraint can also be defined for each provider in
required_providers
block.The
version
attribute is optional, but is recommended to enforce the provider version. If it is not included, the Terraform will automatically use the latest version of the provider, which may cause breaking changes.
- The
provider
block configs the specified provider, in this exampleazurerm
.- A provider is a plugin that Terraform uses to create and manage your resources.
- You can define multiple provider blocks in a TF config to manage resources from diff providers.
Use
resource
blocks to define components of your infrastructure. A resource might be a physical component such as a server, or it can be a logical resource such as a Heroku application.Resource blocks have 2 strings before the block:
Resource Type
Resource Name
In this example, the resource type is
azurerm_resource_group
and the resource name isrg
.The prefix of the type maps to the name of the provider. In this example, TF manages
azurerm_resource_group
resource with theazurerm
provider.Together, the resource type and resource name form a unique ID for the resource.
- EX: Network ID would be -
azurerm_resource_group.rg
Resource blocks contain arguments which you use to config the resource. The Azure provider docs document supported resources and their config options, including
azurerm_resource_grou
and its supporting arguments.
- Initialize your
learn-terraform-init
directory in your terminal. Theterraform
command will work with any OS.- Your output should look similar to this:
$ terraform init
Initializing the backend...
Initializing provider plugins...
- Finding hashicorp/azurerm versions matching "~> 2.65.*"...
- Installing hashicorp/azurerm v2.65.0...
- Installed hashicorp/azurerm v2.65.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. All Terraform commands
should now work.
If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
- The
terraform fmt
command automatically updates configurations in the current directory for readability and consistency. This helps keep formatting consistent in all of your config files/- Terraform will print out the names of the files it modified when formatting