I am trying to create 3 different vpcs in a single project but I am struggling to understand what is the best way to do this? Create 3 modules, one for each VPC? But how do you fill in tfvars then? Should you have multiple tfvars in a folder and call them when running terraform apply? Can you use a for expression and if so how do you go about that?
.tfvars files are just a way of supplying values to variables defined in a root module (using
variable blocks). So how you use them depends on what variables your root module defines.
There are many options for creating multiple VPCs. Making modules is useful if you have multiple resources that you want to group together behind an API abstraction, possibly with the ability to re-use the module elsewhere.
You could define a module with whatever resources you need and then have three separate calls to that module. The values needed for whatever the module requires could be hardcoded or passed in from variables in the root module, sourced from a .tfvars file.
Alternatively you could use
for_each with the module and a more complex variable format (for example a map). That would allow you to easily change to having 2 or 4 (or whatever) VPCs by just updating a
Finally you might decide that a module isn’t needed - if you only have one or two resources and have no need to re-use the module elsewhere it might be overkill. In which case you could just have multiple VPC resources in the root module (with different names) or use count/for_each as before. Again the values needed could be hardcoded, come from local variables or be sourced from variables that are populated from
The tfvars has variables that are used in the root module to complete the parameters of the vpc, so those will change per vpc, so is it better to just hardcode the parameters in the root module for each block of code for each vpc?
We can use this module, this easy to use
It is totally up to you. If you are already using a
.tfvars file for your values I’d probably continue to do the same, either adjusting things to be maps instead of strings or adding extra variables with some form of prefix/suffix.
I am using lists, but I am struggling to understand how to pass more than one value in a list. For instance each VPC requires 2 subnets, one for aza and one for azb. The tfvars file has a list of all 6 subnets [“subnet1”, “subnet2”, “subnet3”, etc etc]
So I am calling three identical modules (just copied my master two additional times.) and in the area for VPC I am using:
tfvars list = vpc_cidr = [“10.211.128.0/27”, “10.211.128.32/27”, “10.211.128.64/27”]
main.tf = environment-vpc = var.environment-vpc for vpc1
environment-vpc = var.environment-vpc for vpc2
environment-vpc = var.environment-vpc for vpc3
my subnets are listed as is:
tfvars = public_subnets_cidr = [“10.211.128.0/28”, “10.211.128.16/28”, “10.211.128.32/28”, “10.211.128.48/28”, “10.211.128.64/28”, “10.211.128.80/28”]
main.tf = public_subnets_cidr = var.public_subnets_cidr
This is where I cannot figure out how to pass two objects in the list to the first vpc, then the next two for the second vpc and the last two for the third vpc.