Use-case
- Using Terraform I want to create different preprod env's (like: dev, qa, uat env.. so on). Resource definition and module usage will be same, only difference will be the name prefix, so that it creates separate resources for each of the mentioned env's, but keeping the VPC common for all.
Terraform Version: v0.13.5
Directory Structure
├── dev
│ ├── dev.tfvars
│ ├── main.tf
│ ├── outputs.tf
│ ├── provider.tf
│ └── variables.tf
├── qa
│ ├── qa.tfvars
│ ├── main.tf
│ ├── outputs.tf
│ ├── provider.tf
│ └── variables.tf
└── preprod-common
├── main.tf
├── outputs.tf
├── provider.tf
└── variables.tf
preprod-common
main.tf
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
name = var.vpc_name
cidr = var.vpc_cidr
azs = var.vpc_azs
private_subnets = var.vpc_private_subnets
public_subnets = var.vpc_public_subnets
}
Output.tf
output "vpc_id" {
description = "The ID of the VPC"
value = module.vpc.vpc_id
}
dev
main.tf
module "security-group" {
source = "terraform-aws-modules/security-group/aws"
name = ${var.prefix}-${var.sg-name}
vpc_id = <vpc_id created from preprod-common>
}
prefix - is the environment name. This way it creates separate resource for each environment, as per prefix value which will be like: (dev, qa or uat..so on).
qa
main.tf
module "security-group" {
source = "terraform-aws-modules/security-group/aws"
name = ${var.prefix}-${var.sg-name}
vpc_id = <vpc_id created from preprod-common>
}
and so on.. for other environments.
FYI - I already ran preprod-common configuration and it has created a new AWS VPC. The question is, how can I refer the vpc_id created from preprod-common into dev, qa and other lower environments?
Note: - I am aware about workspaces as well but this is how I want to implement.
Before answering the question I just want to note some terminology: I think what you are calling a "template" here is what is actually called a Terraform module. I'm noting that not to be pedantic but just because knowing the correct terminology will be helpful when referring to the Terraform documentation or asking other questions in the future.
With that said, the pattern you are following here, of calling two modules and passing the output of one into another, is called Module Composition and the documentation about that pattern has a number of different examples of it.
For your specific case, you can pass the
vpc_id
output from yourvpc
module into thesecurity-group
module like this: