Terraform module - unix1998/technical_notes GitHub Wiki
To provision both a development (DEV) and quality assurance (QA) environment using Terraform, reuse the same storage module while having separate configurations for the VMs and network settings. Below is a sample Terraform configuration demonstrating how to achieve this.
Directory Structure
.
├── main.tf
├── modules
│ └── storage
│ ├── main.tf
│ └── variables.tf
├── dev
│ ├── main.tf
│ └── variables.tf
└── qa
├── main.tf
└── variables.tf
Storage Module
modules/storage/main.tf
resource "azurerm_storage_account" "storage" {
name = var.storage_account_name
resource_group_name = var.resource_group_name
location = var.location
account_tier = "Standard"
account_replication_type = "LRS"
tags = {
environment = var.environment
}
}
modules/storage/variables.tf
variable "storage_account_name" {
description = "The name of the storage account"
type = string
}
variable "resource_group_name" {
description = "The name of the resource group"
type = string
}
variable "location" {
description = "The Azure location where the storage account will be created"
type = string
}
variable "environment" {
description = "The environment for the storage account (dev, qa, etc.)"
type = string
}
Development Environment Configuration
dev/main.tf
provider "azurerm" {
features {}
}
module "storage" {
source = "../modules/storage"
storage_account_name = "devstorageaccount"
resource_group_name = "dev-resource-group"
location = "East US"
environment = "dev"
}
# Additional resources for DEV environment
resource "azurerm_virtual_network" "vnet" {
name = "dev-vnet"
address_space = ["10.0.0.0/16"]
location = "East US"
resource_group_name = "dev-resource-group"
}
resource "azurerm_subnet" "subnet" {
name = "dev-subnet"
resource_group_name = "dev-resource-group"
virtual_network_name = azurerm_virtual_network.vnet.name
address_prefixes = ["10.0.1.0/24"]
}
resource "azurerm_network_interface" "nic" {
name = "dev-nic"
location = "East US"
resource_group_name = "dev-resource-group"
ip_configuration {
name = "internal"
subnet_id = azurerm_subnet.subnet.id
private_ip_address_allocation = "Dynamic"
}
}
resource "azurerm_linux_virtual_machine" "vm" {
name = "dev-vm"
location = "East US"
resource_group_name = "dev-resource-group"
size = "Standard_DS1_v2"
admin_username = "adminuser"
network_interface_ids = [azurerm_network_interface.nic.id]
os_disk {
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
}
source_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "18.04-LTS"
version = "latest"
}
}
Quality Assurance Environment Configuration
qa/main.tf
provider "azurerm" {
features {}
}
module "storage" {
source = "../modules/storage"
storage_account_name = "qastorageaccount"
resource_group_name = "qa-resource-group"
location = "West US"
environment = "qa"
}
# Additional resources for QA environment
resource "azurerm_virtual_network" "vnet" {
name = "qa-vnet"
address_space = ["10.1.0.0/16"]
location = "West US"
resource_group_name = "qa-resource-group"
}
resource "azurerm_subnet" "subnet" {
name = "qa-subnet"
resource_group_name = "qa-resource-group"
virtual_network_name = azurerm_virtual_network.vnet.name
address_prefixes = ["10.1.1.0/24"]
}
resource "azurerm_network_interface" "nic" {
name = "qa-nic"
location = "West US"
resource_group_name = "qa-resource-group"
ip_configuration {
name = "internal"
subnet_id = azurerm_subnet.subnet.id
private_ip_address_allocation = "Dynamic"
}
}
resource "azurerm_linux_virtual_machine" "vm" {
name = "qa-vm"
location = "West US"
resource_group_name = "qa-resource-group"
size = "Standard_DS1_v2"
admin_username = "adminuser"
network_interface_ids = [azurerm_network_interface.nic.id]
os_disk {
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
}
source_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "18.04-LTS"
version = "latest"
}
}
Summary
- Storage Module: Defined once and reused in both DEV and QA environments.
- Environment-specific Configuration: Separate configurations for DEV and QA for VMs and network settings.
- Directory Structure: Organized to keep modules and environment configurations separate, improving manageability.
By using this approach, you can easily provision and manage multiple environments with shared and distinct resources.