How to create a Resource group in Azure using Terraform Part -1

Using Terraform with Azure

------------------------------------------------------------

1. Prerequisite for Working on Terraform
2. How to launch a Visual Studio code
3. How to make a connection to Azure with Terraform and provision azure resources


A.Prerequisite :-

1. AZ CLI
https://docs.microsoft.com/en-us/cli/azure/install-azure-cli-windows?view=azure-cli-latest
-- Download MSI Installer
-- Install

2. Terraform.exe
https://www.terraform.io/downloads.html
-- Download Windows 64 BIT and just
copy the terraform.exe into %USERPROFILE%\AppData\Local\Microsoft\WindowsApps
Benefit : you can call terraform.exe from any folder and any drive.
refer blog -- https://superuser.com/questions/284342/what-are-path-and-other-environment-variables-and-how-can-i-set-or-use-them

old release of terraform is present here

https://releases.hashicorp.com/terraform/

3. Visual Studio Code
https://code.visualstudio.com/download
-- Windows 10
-- User installer 64 BIT
-- Download and install

B. Best way to launch a Visual Studio code
-- Start --> run >> cmd >> open the cmd as run as administrator >> go to working drive and create a Folder example =>Rakesh
cd Rakesh
code .

C. How to Authenticating to Azure using Azure CLI with terraform remote state file Architecture

Terraform supports a number of different methods for authenticating to Azure,here i will use backend.tf where TF State file will store
into Azure storage account
so that multiple user can work simultaneously.

D.Terraform using Visual Studio code and connect to Azure portal

Create a Backend.tf
===================
terraform {
backend "azurerm" {
resource_group_name = "terraform-rg"
storage_account_name = "stgstoragefortf"
container_name = "myterraformstate"
key = "tfstatefile.json"
}
}
Create a Version.tf
====================
provider "azurerm" {
# whilst the `version` attribute is optional, we recommend pinning to a given version of the Provider
version = "~>2.0.0"
features {}
}

provider "null" {
version = "= 2.1.1"
}

provider "template" {
version = "= 2.1.1"
}
provider "azuread" {
version= "~> 0.7"
}
provider "random" {
version = "~> 2.2"

}

Create a execute.ps1
====================
az account set -s "Visual Studio Enterprise"
terraform init -backend-config="access_key=$(az storage account keys list --resource-group "Terraform-Training-Rg" --account-name "stgtrainingstorefortf" --query '[0].value' -o tsv)"
Create a resource_group.tf
====================

resource "azurerm_resource_group" "resource_group" {
name = var.resourcegroup
location = var.location
tags = var.tagging
}

Create a Variable.tf
==========================
variable "resourcegroup" {
type =string
default = "rak-eastus-resource"
}

variable "location" {
type =string
default = "eastus"
}
variable "tagging" {
type = map

default = {
Environment = "Development"
Dept = "training"
}
}

In order to execute above code you need to go to terminal of Visual Studio code and execute below commands
-------------------------
1. Initilization
===============
terraform init -backend-config="access_key=$(az storage account keys list --resource-group "Terraform-Training-Rg" --account-name "stgtrainingstorefortf" --query '[0].value' -o tsv)"

2.plan
==========
terraform plan

3. Apply
============
terraform apply

How to create a basic resource group using terraform
===================================================
 Demo:-


Comments


  1. Thank you for sharing wonderful information with us to get some idea about that content.
    Workday Training Online
    Workday Training

    ReplyDelete

Post a Comment

Popular posts from this blog

How to use Custom Script Extensions for windows using Azure PowerShell - AZ CLI and from Terraform

Error inspecting states in the "azurerm" backend: storage: service returned error: StatusCode=403, ErrorCode=AuthenticationFailed