Terraform
Sharing Modules
- Modules can be shared with others using
- network drives (nfs)
- git
- Terraform Cloud
Lets create a Module Repository
- We have create a Module repo with one module to create virtual network Refer Here
- This module is hosted on github Refer Here for url
Lets use this module
- Refer Here for the changes done
-
We have used the module
- by using a source with reference to a tag and subfolder in git repo
source = "github.com/dummyrepos/lttfmodules//azure/vnet?ref=v1.0.1" - configured azurerm backend
- by using a source with reference to a tag and subfolder in git repo
-
Exercise: use the module to create 4 subnets in your azure account using module at
https://github.com/dummyrepos/lttfmodules
Multiple Environment Creation
- When terraform template is configured with a backend it allows only one user to execute and stores the state in remote location
- Now lets assume we have a usecase, where we are using the same template for multiple environments then backend is not enough
- Generally template refers to one enivornment, (apply => environment)
- To handle multiple environments from same template and same folder with same backend terraform uses workspaces.
- Terraform workspaces are enabled by default, if your are not using workspaces you are indirectly using default workspace.
Best Practices for multiple environments
- Create a backend
- Create a tfvars file per environment
- create workspaces with names equivlaent to your environments (dev, qa, staging, prod)
- Now from CI/CD pipelines select the workspace post init and apply using a tfvars file using auto apporove
- Ideally terraform template should call reusable modules
- Ensure lint and security scans are followed and errors are fixed.
Exercise:
- Learn markdown format to document your modules.
