Ejemplo de módulo de Terraform
En mi ejemplo de un módulo de Terraform, estaré implementando una Red Virtual de Azure y subredes relevantes. Este será un módulo simple, para referenciar la estructura de carpetas y la configuración. Repositorio de muestra aquí.
Estructura de carpetas:
terraform-module-example
└── modules
└── vnet
└── vnet.tf
└── outputs.tf
└── variables.tf
└── main.tf
└── providers.tf
└── variables.tf
Si profundizamos en modules/vnet
vnet.tf contiene los recursos necesarios para crear una Red Virtual de Azure y subredes relevantes, incluido el Grupo de Recursos
resource "azurerm_resource_group" "vnet_resource_group" {
name = "${var.name}-rg"
location = var.location
tags = {
Environment = var.environment
}
}
resource "azurerm_virtual_network" "virtual_network" {
name = var.name
location = var.location
resource_group_name = azurerm_resource_group.vnet_resource_group.name
address_space = [var.network_address_space]
tags = {
Environment = var.environment
}
}
resource "azurerm_subnet" "aks_subnet" {
name = var.aks_subnet_address_name
resource_group_name = azurerm_resource_group.vnet_resource_group.name
virtual_network_name = azurerm_virtual_network.virtual_network.name
address_prefixes = [var.aks_subnet_address_prefix]
}
resource "azurerm_subnet" "appgw_subnet" {
name = var.appgw_subnet_address_name
resource_group_name = azurerm_resource_group.vnet_resource_group.name
virtual_network_name = azurerm_virtual_network.virtual_network.name
address_prefixes = [var.appgw_subnet_address_prefix]
}
variables.tf contiene las variables relevantes que el módulo requiere:
variable "name" { }
variable "location" {
default = "uksouth"
}
variable "network_address_space" { }
variable "aks_subnet_address_prefix" { }
variable "aks_subnet_address_name" { }
variable "appgw_subnet_address_prefix" { }
variable "appgw_subnet_address_name" { }
variable "environment" { }
outputs.tf contiene salidas que puedo devolver a mi configuración de Terraform.
output "aks_subnet_id" {
value = azurerm_subnet.aks_subnet.id
}
output "appgw_subnet_id" {
value = azurerm_subnet.appgw_subnet.id
}
output "vnet_id" {
value = azurerm_virtual_network.virtual_network.id
}
output "vnet_name" {
value = azurerm_virtual_network.virtual_network.name
}
output "resource_group" {
value = azurerm_resource_group.vnet_resource_group.name
}
output "resource_group_id" {
value = azurerm_resource_group.vnet_resource_group.id
}
Puedes hacer referencia a las salidas dentro de tu configuración principal de Terraform.
Mirando root/main.tf, conocido como el módulo raíz, puedes declarar el módulo secundario referenciado anteriormente:
module "vnet" {
source = "./modules/vnet"
name = var.vnet_name
location = var.location
network_address_space = var.network_address_space
aks_subnet_address_prefix = var.aks_subnet_address_prefix
aks_subnet_address_name = var.aks_subnet_address_name
appgw_subnet_address_prefix = var.appgw_subnet_address_prefix
appgw_subnet_address_name = var.appgw_subnet_address_name
environment = var.environment
}
providers.tf declara los proveedores de Terraform e información del backend.
provider "azurerm" {
version = "~> 2.0"
features {}
}
terraform {
backend "azurerm" {
resource_group_name = "platopsacad-tf-rg"
storage_account_name = "platopsacadazuredevops"
container_name = "terraform.tfstate`"
}
}
data "azurerm_client_config" "current" {}
variables.tf nuevamente, variables, pero ahora para todo el entorno. Al agregar más módulos, incluirías más variables en este archivo.
variable "location" {
type = string
description = "Location of Resources"
}
variable "vnet_name" {
type = string
description = "Virtual Network Name"
}
variable "network_address_space" {
type = string
description = "Virtual Network Address Space"
}
variable "aks_subnet_address_prefix" {
type = string
description = "AKS Subnet Address Prefix"
}
variable "aks_subnet_address_name" {
type = string
description = "AKS Subnet Name"
}
variable "appgw_subnet_address_prefix" {
type = string
description = "AppGW Subnet Address Prefix"
}
variable "appgw_subnet_address_name" {
type = string
description = "AppGW Subnet Name"
}
variable "environment" {
type = string
description = "Environment"
}