What are Terraform modules and how do they work?

Many beginners skip configuring Terraform modules to ease the customization process. At least they think so, that they made it easier for themselves. Let's take a look at what Terraform modules are and how they work.







I assume that you already know some of the basics of Terraform and have even tried using it before. If not, check out this Terraform overview and this video tutorial before continuing.







Please note, I am deliberately not using real code examples with some specific vendors like AWS or Google for ease of understanding.







Terraform modules



You are already writing modules



Even if you don't intentionally create a module, if you are using Terraform, you are already writing a module — a so-called "root" module.







Any Terraform ( .tf



) configuration file in a directory, even one, forms a module.







What does a module do?



The Terraform module allows you to create logical abstraction on top of a set of resources. In other words, a module allows you to group resources together and reuse that group later, possibly many times.







Let's say we have a virtual server with some features hosted in the cloud. What set of resources can this server describe? For example:







  • ,













  • IP-,







  • ,







  • , , . .















, . . , ?







, , «» .







« » .







5 «», ( ):







module "server" {

    count         = 5

    source        = "./module_server"
    some_variable = some_value
}
      
      





Terraform "" , 0.13.







:



, , , . :







  • , (VPC)







  • (.. bucket)



















  • - ,









, : «» «» . «» :







module "server" {
    source        = "./module_server"
    some_variable = some_value
}

module "network" {  
    source              = "./module_network"
    some_other_variable = some_other_value
}
      
      





,







, «» . , , .













:













  • Terraform — , Docker,







  • Git ( GitHub/BitBucket)







  • HTTP URL- .zip









?







. , «» , «»?







.









Terraform : .







Scope ( )



, , , . , «A» «B».







, , , . , 5 «»:







module.server[0].resource_type.resource_name
module.server[1].resource_type.resource_name
module.server[2].resource_type.resource_name
...
      
      





, - count







, :







module "server-alpha" {    
    source        = "./module_server"
    some_variable = some_value
}
module "server-beta" {
    source        = "./module_server"
    some_variable = some_value
}
      
      





— ,







:







module.server-alpha.resource_type.resource_name

module.server-beta.resource_type.resource_name
      
      







, .







«» , «».













output



«» , .







«» variable



, :













output



variable



, .







— ( ) — «» , .







, «» , «» «»:



network_id = module.network.network_id
      
      





'network_id



' — ,







:







module "server" {
    count         = 5
    source        = "./module_server"
    some_variable = some_value
    network_id    = module.network.network_id
}

module "network" {  
    source              = "./module_network"
    some_other_variable = some_other_value
}
      
      





5 , .









, .







Terraform, .







HashiCorp, Terraform, : "Organize Configuration".







In addition, there is an excellent comprehensive tutorial covering everything from beginner to advanced Terraform concepts: Study Guide - Terraform Associate Certification .







The modular structure of your code makes your configuration more flexible and easier for others to understand. The latter is especially useful for the team.







If you liked the article, follow me on Twitter (@ vasylenko ), where I occasionally share my takeaways and tips on Terraform, AWS, Ansible, and other DevOps-related technologies.








All Articles