Friday, January 1, 2021

Terraform : Working with modules

Terraform modules is the best way to reuse your terraform resources. 

Create a following folder structure:

  • terraform-app
  • terraform-app/modules
  • terraform-app/dev
  • terraform-app/prod
So terraform-app/modules will have the template kind of code which you can reuse it in your dev environment & prod environment.

In this example we will create a VPC & Subnet template which can be reused in dev & prod.

lets create network.tf file inside terraform/modules, which will have VPC & Subnet templates, Will be using output to print and to associate it with Subnet.

Now lets create variables.tfvars file

Lets switch the directory and create a main.tf file inside terraform-app/dev which will make use of the modules which we have created above.

Also lets create another main.tf file inside terraform-app/prod

So the main.tf file under dev will create a VPC with CIDR block 192.168.0.0/16 and the main.tf file under prod will create a VPC with default CIDR block 10.0.0.0/16 which is defined under variables.tfvars 

No comments:

Post a Comment

Terraform Cheat Sheet [WIP]

Installing Terraform