Terraform Code For Creating Resource Group and VNET and VM in Azure using Modules
Terraform is an open-source infrastructure as code software tool created by HashiCorp. Users define and provide data center infrastructure using a declarative configuration language known as HashiCorp Configuration Language, or optionally JSON.
Below codes is for Creating Resource Group and VNET and VM in Azure using Modules and it is tested in Azure Platform:
Variables.tf
variable "subscription_id" {
description = "Enter Subscription ID for provisioning resources in Azure"
}
variable "client_id" {
description = "Enter Client ID for Application in Azure AD"
}
variable "client_secret" {
description = "Enter Client Secret for Application in Azure AD"
}
variable "tenant_id" {
description = "Enter Tenant ID / Dirctory ID of your Azure AD. Run Get-AzureSubscription"
}
provider.tf
terraform {
required_version = "1.0.2"
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~>2.0"
}
}
}
provider "azurerm" {
features {}
subscription_id = var.subscription_id
client_id = var.client_id
client_secret = var.client_secret
tenant_id = var.tenant_id
}
main.tf
resource "azurerm_resource_group" "rg" {
name = "mylinuxresource"
location = "centralindia"
}
module "vnet" {
source = "./modules/vnet"
# source = "Azure/vnet/azurerm"
resource_group_name = azurerm_resource_group.rg.name
address_space = ["10.0.0.0/16"]
subnet_prefixes = ["10.0.1.0/24"]
subnet_names = ["subnet1"]
depends_on = [azurerm_resource_group.rg]
}
module "createvm" {
source = "./modules/vm"
resource_group_name = azurerm_resource_group.rg.name
resource_group_location = azurerm_resource_group.rg.location
vnet_name = module.vnet.vnet_name
// vnet_id = module.vnet.vnet_id
subnet_id = module.vnet.vnet_subnets[0]
host_name = "mylinux"
host_admin = "myadmin"
host_password = "Password1234"
depends_on = [azurerm_resource_group.rg]
}
VM
main.tf
data "azurerm_resource_group" "vnet" {
name = var.resource_group_name
}
# Create public IPs -1
resource "azurerm_public_ip" "mypublicip" {
name = "myPublicIP"
location = var.resource_group_location
resource_group_name = data.azurerm_resource_group.vnet.name
allocation_method = "Dynamic"
tags = {
Owner = "Brajesh"
}
}
resource "azurerm_network_interface" "vm_interface" {
name = "vm_nic"
location = var.resource_group_location
resource_group_name = data.azurerm_resource_group.vnet.name
ip_configuration {
name = var.vnet_name
subnet_id = var.subnet_id
public_ip_address_id = azurerm_public_ip.mypublicip.id
private_ip_address_allocation = "Dynamic"
}
}
resource "azurerm_linux_virtual_machine" "linuxm" {
name = "ublinux"
resource_group_name = data.azurerm_resource_group.vnet.name
location = var.resource_group_location
size = "Standard_B1s"
computer_name = var.host_name
admin_username = var.host_admin
admin_password = var.host_password
disable_password_authentication = false
network_interface_ids = [azurerm_network_interface.vm_interface.id]
os_disk {
caching = "ReadWrite"
storage_account_type = "Premium_LRS"
}
source_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "18.04-LTS"
version = "latest"
}
}
output.tf
variables.tf
variable "resource_group_name" {
description = "Name of the resource group to be imported."
type = string
}
variable "resource_group_location" {
description = "Location of the resource group to be imported."
type = string
}
/*variable "vnet_id" {
description = "vnet id form module"
type = string
}
*/
variable "vnet_name" {
description = "vnet name form module"
type = string
}
variable "subnet_id" {
description = "subnet id name form module"
type = string
}
variable "host_name" {
description = "Linux host name"
type = string
}
variable "host_admin" {
description = "Linux host admin"
type = string
}
variable "host_password" {
description = "Linux host password"
type = string
}
VNET
main.tf
# src: https://github.com/Azure/terraform-azurerm-vnet/blob/master/main.tf
#Azure Generic vNet Module
data "azurerm_resource_group" "vnet" {
name = var.resource_group_name
}
resource "azurerm_virtual_network" "vnet" {
name = var.vnet_name
resource_group_name = data.azurerm_resource_group.vnet.name
location = data.azurerm_resource_group.vnet.location
address_space = var.address_space
}
resource "azurerm_subnet" "subnet" {
count = length(var.subnet_names)
name = var.subnet_names[count.index]
resource_group_name = data.azurerm_resource_group.vnet.name
virtual_network_name = azurerm_virtual_network.vnet.name
address_prefixes = [var.subnet_prefixes[count.index]]
}
locals {
azurerm_subnets = {
for index, subnet in azurerm_subnet.subnet :
subnet.name => subnet.id
}
}
output.tf
output "vnet_id" {
description = "The id of the newly created vNet"
value = azurerm_virtual_network.vnet.id
}
output "vnet_name" {
description = "The Name of the newly created vNet"
value = azurerm_virtual_network.vnet.name
}
output "vnet_location" {
description = "The location of the newly created vNet"
value = azurerm_virtual_network.vnet.location
}
output "vnet_address_space" {
description = "The address space of the newly created vNet"
value = azurerm_virtual_network.vnet.address_space
}
output "vnet_subnets" {
description = "The ids of subnets created inside the newl vNet"
value = azurerm_subnet.subnet.*.id
}
variable.tf
variable "vnet_name" {
description = "Name of the vnet to create"
type = string
default = "acctvnet"
}
variable "resource_group_name" {
description = "Name of the resource group to be imported."
type = string
}
variable "address_space" {
type = list(string)
description = "The address space that is used by the virtual network."
default = ["10.0.0.0/16"]
}
variable "subnet_prefixes" {
description = "The address prefix to use for the subnet."
type = list(string)
default = ["10.0.1.0/24"]
}
variable "subnet_names" {
description = "A list of public subnets inside the vNet."
type = list(string)
default = ["subnet1", "subnet2", "subnet3"]
}
command.sh
# make sure terraform CLI is installed
terraform
# format the tf files
terraform fmt
# initialize terraform Azure modules
terraform init
# validate the template
terraform validate
# plan and save the infra changes into tfplan file
terraform plan -out tfplan
# show the tfplan file
terraform show -json tfplan
terraform show -json tfplan >> tfplan.json
# Format tfplan.json file
terraform show -json tfplan | jq '.' > tfplan.json
# show only the changes
cat tfplan.json | jq -r '(.resource_changes[] | [.change.actions[], .type, .change.after.name]) | @tsv'
cat tfplan.json | jq '[.resource_changes[] | {type: .type, name: .change.after.name, actions: .change.actions[]}]'
# apply the infra changes
terraform apply tfplan
# delete the infra
terraform destroy
# cleanup files
rm terraform.tfstate
rm terraform.tfstate.backup
rm .terraform.lock.hcl
rm tfplan
rm tfplan.json
rm -r .terraform/
Key Terms:
- azure ,
- Open Source Software ,
- Terraform