Create a Virtual Network in Azure using Terraform with modular approach part -4
Deploy a module in Terraform for creation of Virtual Network in Azure part-4
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 we will create a module for Virtual Network
===================================
Inside your module Folder create a subFolder named virtualnetwork_v1
Create 3 Files
1.virtual_network.tf
2.variable.tf
3.output.tf
1. in virtual_network.tf
==============================================
resource "azurerm_virtual_network" "virtual_network" {
name = var.vnet_name
location = var.resource_group_location
address_space = var.address_space
resource_group_name = var.resource_group_name
#dns_servers = var.dns_server
tags = var.tags
}
2. in variable.tf
==================
variable "resource_group_name" {
}
variable "vnet_name" {
}
variable "resource_group_location" {
}
variable "address_space" {
description = "The address space that is used by the virtual network."
default = []
}
variable "dns_server" {
description = "The DNS servers to be used with vNet."
default = []
}
variable "tags" {
type = "map"
}
3. in output.tf
=================
output "azurerm_virtual_network_name" {
value = "${azurerm_virtual_network.virtual_network.name}"
}
Go to HR Folder and create a file with name virtualnetwork.tf
module "trainingvnet" {
source="../modules/virtualnetwork_v1"
vnet_name=""
resource_group_name=module.trainingrg.azurerm_resource_group_name
location =module.trainingrg.azurerm_resource_group_location
address_space = var.address_space
# dns_server = var.dns_server
tags = var.tags
}
in the HR Folder create a a file with name variable.tf
variable "address_space" {
default=["10.0.0.0/16"]
}
variable "address_prefix" {
default = "10.0.1.0/24
}
<#
variable "dns_server" {
default=["10.0.0.0/16"]
}
#>
variable "vmsubnet_name" {
default = "tableausubnet01"
}
resource "azurerm_subnet" "vmsubnet" {
name = var.vmsubnet_name
virtual_network_name = module.trainingvnet.azurerm_virtual_network_name
resource_group_name = module.trainingrg.azurerm_resource_group_name
address_prefix = var.address_prefix
# network_security_group_id = "var.nsg_id
service_endpoints = ["Microsoft.Sql","Microsoft.Storage","Microsoft.KeyVault"]
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Now if you have to initialize the terraform because we are introducing new module
go to visualstudio code terminal and execute below command
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
Comments
Post a Comment