Lets Make the templates which we have developed reusable
- Reusability is essential as we follow principle DRY (Dont Repeat Yourself)
- In Terraform, if we need reusability we need to create modules.
Activity: Lets use module which is developed by some one else
- Terraform has lot of modules developed by communities and terraform stores them in terraform registry Refer Here
- Lets create a vpc from terraform module Refer Here
- Refer Here for the sample module used for terraform registry
What are the things required to make our template reusable
- Outputs: Refer Here
- Lets add some outputs to Azure network
- Added outputs section. Refer Here for outputs.tf
- We have made a small change to display subnet count. Refer Here for the changes
- Now destory whatever is created
- Now to make this as a module,
- Module is not supposed to have provider
- Modules generally will not have tfvars as this is something that belongs to template
- Refer Here for the module usage syntax
-
We have created a demo to use the module from local path Refer Here for the changes
-
Now lets try creating the same from differnt source
- git
git::https://github.com/asquarezone/ntier-azure-vnet.git
- github
github.com/asquarezone/ntier-azure-vnet
- git
- Refer Here for the changes to pick the module from git repository
- We can also use other sources Refer Here
Datasource
- Datasource help in fetching the information about some resources in the provider.
- Lets try to query the aws to get the vpc id of the default vpc
- Refer Here for the usage syntax of datasources
- Refer Here for the datasource of aws vpc
- The following changeset is used to pull the defult vpc id of mumbai region and subnetids of any vpc from az ap-south-1a Refer Here
- Now lets add datasource to get all the resources of a specfic type (vnet) in azure. Refer Here for the datasource
- Refer Here for the changes.