Storing modules remotely

We've stored the application module in the very same directory where our main Terraform template resides. It makes it impossible to reuse: if there is a new application in the company that requires the same infrastructure (meaning the same module), then we cannot easily use it.

Remember the source attribute of the module?

module "mighty_trousers" { 
  source = "./modules/application" 

Well, it turns out that it doesn't have to be a path to a local directory. In fact, there are multiple supported sources for modules:

  • GitHub
  • BitBucket
  • Generic Git and Mercurial repositories
  • HTTP URLs
  • S3 buckets

Storing modules in one of these destinations allows us to have a collection of reusable components. We can even version our modules, just like system packages or programming language libraries.

As we are deep into GitLab already, let's create yet another repository and call it packt-terraform-app-module. As always, all the code written in this chapter is available on GitLab at https://gitlab.com/Fodoj/packt-terraform-app-module.

Move everything inside the ./modules/application/ folder to packt-terraform-app-module and commit it. Then, remove the modules directory from the MightyTrousers project completely and specify the path to the remote module as follows:

module "mighty_trousers" { 
  source = "git::https://gitlab.com/Fodoj/packt-terraform-app-module.git" 
  vpc_id = "${data.terraform_remote_state.vpc.vpc_id}" 
   # ... 
} 

Replace the URL with your git repository (or just use the one previously specified, if you were too lazy to complete the exercise). Finally, run the terraform get command to pull the module. It will be stored inside the .terraform/ directory: now it makes more sense than symlink to a local directory.

It's a bad idea to reference master branch of a remote module because it can change at any moment. Let's tag the module with version 0.1 and reference it inside template.tf:

# from packt-terraform-app-module 
$> git tag v0.1 
$> git push origin master :tags 

Reference the earlier module in template.tf:

module "mighty_trousers" { 
  source = "git::https://gitlab.com/Fodoj/packt-terraform-app-module.git?ref=v0.1" 
  vpc_id = "${data.terraform_remote_state.vpc.vpc_id}" 
   # ... 
} 

Our packt-terraform repository, dedicated to the MightyTrousers application, is so tiny now. The main template is just 60 lines long, and it simply pulls some data from remote state files and configures a module, also stored remotely. If we want to add a new application, we can create a similar repository without almost any effort. Developers can work with application repository. Owners of modules collection can focus on their modules repositories. Administrators are free to change the global VPC and IAM configuration. This is an infrastructure collaboration dream come true.

Yet we still have one problem left to solve: locking of state file.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.31.240