I have created a simple terraform infrastructure that will created some resource on aws.I have each environment variables in a separate tfvars
. However I do have shared resources like security groups that would be used across environments.
How can I structure the code in such a way that I can create/destroy environments without destroying the shared resources?. I could create separate workspaces but since all resources are defined in same repo, if i destroy one environment, I am going to destroy the shared resources in the process.
A simplified version of my directory structure is as follows:
/
/main.tf
/vars/dev.tfvars
/vars/qa.tfvars
/vars/prod.tfvars
Moreover when using modules how can i specify to only delete part of it during a terraform destroy
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
name = "my-vpc"
cidr = "10.0.0.0/16"
azs = ["eu-west-1a", "eu-west-1b", "eu-west-1c"]
private_subnets = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"]
public_subnets = ["10.0.101.0/24", "10.0.102.0/24", "10.0.103.0/24"]
enable_nat_gateway = true
enable_vpn_gateway = true
tags = {
Terraform = "true"
Environment = "dev"
}
}
For example if i delete dev environment , i just want the subnets to be deleted and not the vpc since i have one vpc shared between all environments.