A module is a container for multiple resources that are used together. Modules can be used to create lightweight abstractions, so that you can describe your infrastructure in terms of its architecture, rather than directly in terms of physical objects.
A Terraform module is very simple: any set of Terraform configuration files in a folder is a module.
Create a new folder called modules and move all the files (that is, main.tf, variables.tf, and outputs.tf) from the web server cluster into modules/services/webserver-cluster.
Open up the main.tf file in modules/services/webserver-cluster and remove the provider definition.
Providers should be configured by the user of the module and not by the module itself.
$ tree minimal-module/
.
├── README.md
├── main.tf
├── variables.tf
├── outputs.tf
resource "azurerm_resource_group" "example" {
name = "my-resources66"
location = "West Europe"
}
module "apache"{
source = "./modules/install_apache"
}
module "nginx"{
source = "./modules/install_nginx"
instances = ["${module.web.instance_ids}"]
}
module "network" {
source = "Azure/network/azurerm"
resource_group_name = azurerm_resource_group.example.name
address_spaces = ["10.0.0.0/16", "10.2.0.0/16"]
subnet_prefixes = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"]
subnet_names = ["subnet1", "subnet2", "subnet3"]
subnet_service_endpoints = {
"subnet1" : ["Microsoft.Sql"],
"subnet2" : ["Microsoft.Sql"],
"subnet3" : ["Microsoft.Sql"]
}
tags = {
environment = "dev"
costcenter = "it"
}
depends_on = [azurerm_resource_group.example]
}
Reference
- https://github.com/KensoDev/terraform-example-with-modules
- https://medium.com/@mitesh_shamra/module-in-terraform-920257136228
- https://www.terraform.io/docs/modules/index.html
- https://github.com/MiteshSharma/TerraformModules
- https://github.com/devopsschool-sample-projects/TerraformModules
I’m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I am working at Cotocus. I blog tech insights at DevOps School, travel stories at Holiday Landmark, stock market tips at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at I reviewed , and SEO strategies at Wizbrand.
Please find my social handles as below;
Rajesh Kumar Personal Website
Rajesh Kumar at YOUTUBE
Rajesh Kumar at INSTAGRAM
Rajesh Kumar at X
Rajesh Kumar at FACEBOOK
Rajesh Kumar at LINKEDIN
Rajesh Kumar at PINTEREST
Rajesh Kumar at QUORA
Rajesh Kumar at WIZBRAND