# Create a Resource Group in Azure

In 
Published 2022-12-03

This tutorial explains how we can create a Resource Group in Azure using Terraform.

A resource group is a container that holds related resources for an Azure solution. The resource group can include all the resources for the solution, or only those resources that you want to manage as a group.

Here are the steps to follow in order to deploy a Resource Group in Azure using Terraform:

1) Create a folder where we keep our project

To be well organized we need to create a folder where we keep our project. In my case this is D:\terraform\azure\resource-group.

2) Create the configuration file

Now is the time to tell Terraform what to do.

We create main.tf file with the following content:

#Set the Azure Provider source and version being used
#----------------------------------------------------
terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "=3.0.1"
    }
  }
}

# Configure the Microsoft Azure Provider
#---------------------------------------
provider "azurerm" {
  features {}
}

###############################
#   Create a resource group   #
##############################################
resource "azurerm_resource_group" "my-new-resource-group" {
  name     = "my-new-resource-group"
  location = "West Europe"
}

So, we configure the provider and the resource group.

3) Initialize the project

Run the terraform init command to initialize a working directory that contains a Terraform configuration. This command is run under the working/project directory.

terraform init

You will see the following in your console:

Initializing the backend...

Initializing provider plugins...
- Finding hashicorp/azurerm versions matching "3.0.1"...
- Installing hashicorp/azurerm v3.0.1...
- Installed hashicorp/azurerm v3.0.1 (signed by HashiCorp)

Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.

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.

4) Login to the Azure account we work with using Azure CLI

az login

You will be prompted to login from the browser. Once the username/password are verified, you are connected to Azure in the console.

Run the following command and change the subscription name if you are using multiple subscriptions:

az account set --subscription "MyAzureSubscription1"

5) Create the Resource Group in Azure

Now it is the time to create the Resource Group in Azure and this is done very simple using the following command:

terraform apply

You can see the result on the screen:

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the
following symbols:
+ create

Terraform will perform the following actions:

# azurerm_resource_group.my-new-resource-group will be created
+ resource "azurerm_resource_group" "my-new-resource-group" {
  + id       = (known after apply)
  + location = "westeurope"
  + name     = "my-new-resource-group"
    }

Plan: 1 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.

Enter a value: yes

azurerm_resource_group.my-new-resource-group: Creating...
azurerm_resource_group.my-new-resource-group: Creation complete after 1s [id=/subscriptions/69045ea7-6500-45f7-adc6-b4c86ce7233b/resourceGroups/my-new-resource-group]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

If you run terraform apply again you will see the following :

azurerm_resource_group.my-new-resource-group: Refreshing state... [id=/subscriptions/69045ea7-6500-45f7-adc6-b4c86ce7233b/resourceGroups/my-new-resource-group]

No changes. Your infrastructure matches the configuration.

Terraform has compared your real infrastructure against your configuration and found no differences, so no changes are
needed.

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

That means terraform apply command is idempotent.