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.
vpc
module:module "child-module"
source path to it. – Marcin