How to User Terraform code to create Azure resource (Part -8)

Posted by

Simple examples to create resource group

Create Azurerm provider

terraform {
  required_providers {
    azurerm = {
      source = "hashicorp/azurerm"
      version = "3.105.0"
    }
  }
}

provider "azurerm" {
  # Configuration options
}

Under configuration option

Use below available options:

Azure Provider: Authenticating using a Service Principal with a Client Secret

Terraform supports a number of different methods for authenticating to Azure:

I am using Authenticating to Azure using a Service Principal and a Client Secret (which is covered in this guide)

Added below configuration

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

  client_id       = "00000000-0000-0000-0000-000000000000"
  client_secret   = "000000000000000000000000000000000000000"
  tenant_id       = "10000000-0000-0000-0000-000000000000"
  subscription_id = "20000000-0000-0000-0000-000000000000"
}

Final provider code will be

terraform {
  required_providers {
    azurerm = {
      source = "hashicorp/azurerm"
      version = "3.105.0"
    }
  }
}

provider "azurerm" {
  features {}

  client_id       = "d1dxxxxxxxx39-4f88-9c02-362294a6cf24"
  client_secret   = "TW9xxxxxxxxxxxxxxxxxx_ht3nFvmPLM1Cc5wbds"
  tenant_id       = "8becxxxxx-55ff-478f-94e0-a96f126aa7a6"
  subscription_id = "1xxxxx-a49f-496e-91c2-0dad1860b1de"
}

To get above details please go to default directory –> App registration –> All application (or create new registration) –> you will get client Id

Tenant ID:

For client secret — go to client credentials and create it

subscription_id –> from subscription

Now add code in .tf file to create resource , here we going to create resource group

resource "azurerm_resource_group" "example" {
  name     = "example-resources"
  location = "West Europe"
}

Run Terraform init

then , Terraform plan

Terraform validate:

Terraform apply

Terraform show

validate at azure portal

Now destroy resource group

guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x