MODULES IN TERRAFORM (PART-6)

Updated: Aug 9, 2020




Hello Readers, This blog is the Part-6 of our Terraform course series. In case you missed previous blogs please check out part one of it before going further.Also we appreciate your feedback on the articles which would help us in improving .

In this article we are going to talk about modules in terraform. Modules help in making the code reusable. For example let's consider VPC service in AWS, there can be several VPCs in a project so modules let us write VPC code once and use it several times and provide us the possibility to change values as per the requirement.





DRY principle in Software Development

DRY stands for “Don’t Repeat Yourself”

We should aim at reducing the repetitive code and use a modular approach instead.

In the earlier lecture, we were making static content into variables so that there can be a single source of information.





Generic Scenario

We do repeat multiple times various terraform resources for multiple projects.

Sample EC2 Resource

resource "aws_instance" "myweb" {
   ami = "ami-bf5540df"
   instance_type = "t2.micro"
   security_groups = ["default"]
  }

Instead of repeating a resource block multiple times, we can make use of a centralized structure.

Centralized Structure




Code : modules_demo


Terraform Registry

The Terraform Registry is a repository of modules written by the Terraform community.

The registry can help you get started with Terraform more quickly

These are ready made modules that we can use to make our task easy.




Module Location

If we intend to use a module, we need to define the path where the module files are present.

The module files can be stored in multiple locations, some of these include:

  • Local Path

  • GitHub

  • Terraform Registry

  • S3 Bucket

  • HTTP URLs

Verified Modules In Terraform Registry

Within Terraform Registry, you can find verified modules that are maintained by various third-party vendors.

These modules are available for various resources like AWS VPC, RDS, ELB, and others.




Verified modules are reviewed by HashiCorp and actively maintained by contributors to stay up-to-date and compatible with both Terraform and their respective providers.

The blue verification badge appears next to modules that are verified.

Module verification is currently a manual process restricted to a small group of trusted HashiCorp partners.


URL for Terraform Registry





Code: terraform_registry_demo





Terraform Workspaces

Terraform allows us to have multiple workspace, with each of the workspace we can have a different set of environment variables associated.





  • Terraform starts with a single workspace named "default".

  • This workspace is special both because it is the default and also because it cannot ever be deleted.

  • If you've never explicitly used workspaces, then you've only ever worked on the "default" workspace.

  • Workspaces are managed with the terraform workspace set of commands.

  • To create a new workspace and switch to it, you can use terraform workspace new; to switch workspaces you can use terraform workspace select; etc.

  • Note that tfstate files for each workspace is managed separately.

Workspace Demo






Code : terraform_workspace_demo






When we are using workspaces, different tfstate files are created for each workspace., As we have created a resource in prod workspace , we will have a tfstate file for the same.




Code is available in GitHub repository

Hope this was helpful for you :)








489 views1 comment
 

Subscribe Form

©2020 by Linux Advise