Create a Storage account in Azure using module using Terraform Part -3

Create a module in Terraform for Storage Account  in Azure part-3

 Prerequisite:-

   Create a Resource Group  in Azure using terraform. Part -1
       Youtube link:-
          https://www.youtube.com/watch?v=pUUYL2fKQcc&t=363s
        Blog
         https://terraformwithkushagra.blogspot.com/2020/04/how-to-create-resource-in-azure-using.html

    Create a module in Terraform  for a Resource Group in Azure part-2
       Youtube link:-
        https://www.youtube.com/watch?v=O1DNLPyudEY&t=80s

        Blog:-https://terraformwithkushagra.blogspot.com/2020/04/how-to-create-module-for-resource-group.html


   Now How to create a Storage account in Azure using module using Terraform


1. Go to Module Folder and create a subfolder storage_v1
2. create 3 Files

          storage.tf
        variable.tf
        output.tf

Code for storage.tf
===========================
 resource "azurerm_storage_account" "storage_account" {
  name                     = var.storage_account_Name
  resource_group_name      = var.resource_group_name
  location                 = var.location
  account_tier             = var.accounttier
  account_replication_type =var.accountreplicationtype
}

Code for variable.tf
-----------------------
variable "storage_account_Name" {
    type=string
    default =""
}

variable "resource_group_name" {
    type= string
    default =""
}

variable "location" {
     type=string
     default =""
 }

variable "accounttier" {
     type=string
     default =""
 }

variable "accountreplicationtype" {
     type =string
 
}

variable "tagging" {
    type = map

    default = {
      }
}

output.tf
============
output "azurerm_storage_account_name" {
    description = "the storage account name"
    value       = "${azurerm_storage_account.storage_account.name}"
    }

output "account_tier" {
    description = "the storage account tier"
    value       = "${azurerm_storage_account.storage_account.account_tier}"
    }

output "account_replication_type" {
    description = "account replication type"
    value       = "${azurerm_storage_account.storage_account.account_replication_type}"
    }

 Call the module from Project Folder
-------------------------------------------
Create a file with name "storage.tf" in hr project folder

module "sqlbackups" {
    source = "../modules/storage_v1"
    storage_account_Name= var.storageaccountname
    resource_group_name=module.trainingrg.azurerm_resource_group_name
    location =module.trainingrg.azurerm_resource_group_location
    accounttier=var.accountier
    accountreplicationtype=var.actrepltype
}


Create a file with name "Storagevariable.tf" in hr project folder

variable "storageaccountname" {
    type=string
    default="sqlstorage0102"
}

 variable "accountier" {
    type=string
default="Standard"
}
variable "actrepltype" {
    type=string
default="GRS"
}

Now go to the terminal and execute below commands

terraform init -backend-config="access_key=$(az storage account keys list --resource-group "Terraform-Training-Rg" --account-name "stgtrainingstorefortf" --query '[0].value' -o tsv)"

terraform plan

terraform apply

YouTube Link:-

https://youtu.be/gLAdTaV2leQ



 

Comments

Popular posts from this blog

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

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

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