I asked this question on Stackoverflow but didnt get an answer so i ask it here:
I want to use resources, in this case the output of the vpc module, in another environment. Goal is to reduce the costs for the customer with resources of stage and dev in the same vpc. Stage and dev have seperate ecs-cluster, asg, lc, different docker images in ecr etc but should be in the same vpc with the same load balancer and then a host header listener to forward to the specific target group. Both should use the same database and the same load balancer.
Requirement was to have n Customer each with stage, dev and prod environments. All Customer folders should contain the three environments.
My folder structure is:
โโโ Terraform
โ โโโ Customer1
โ โโโ Customer2
โ โโโ Customer3
โ โโโ Customer4
โ โโโ Customer5
โ โโโ Global
โ โ โโโ iam
โ โ โ โโโ terragrunt.hcl
โ โโโ README.md
โ โโโ Customer6
โ โโโ non-prod
โ โ โโโ eu-central-1
โ โ โ โโโ dev
โ โ โ โ โโโ cloudwatch
โ โ โ โ โ โโโ terragrunt.hcl
โ โ โ โ โโโ ec2
โ โ โ โ โ โโโ terragrunt.hcl
โ โ โ โ โโโ ecs
โ โ โ โ โ โโโ terragrunt.hcl
โ โ โ โ โโโ lambda
โ โ โ โ โ โโโ terragrunt.hcl
โ โ โ โ โโโ rds
โ โ โ โ โ โโโ terragrunt.hcl
โ โ โ โ โโโ terragrunt.hcl
โ โ โ โ โโโ vars.hcl
โ โ โ โ โโโ vpc
โ โ โ โ โโโ terragrunt.hcl
โ โ โ โโโ region.hcl
โ โ โ โโโ stage
โ โ โ โโโ cloudwatch
โ โ โ โ โโโ terragrunt.hcl
โ โ โ โโโ ec2
โ โ โ โ โโโ terragrunt.hcl
โ โ โ โโโ ecs
โ โ โ โ โโโ terragrunt.hcl
โ โ โ โโโ lambda
โ โ โ โ โโโ terragrunt.hcl
โ โ โ โโโ rds
โ โ โ โ โโโ terragrunt.hcl
โ โ โ โโโ terragrunt.hcl
โ โ โ โโโ vars.hcl
โ โ โ โโโ vpc
โ โ โ โโโ terragrunt.hcl
โ โ โโโ terragrunt.hcl
โ โโโ prod
โ โโโ eu-central-1
โ โโโ prod
โ โ โโโ cloudwatch
โ โ โ โโโ terragrunt.hcl
โ โ โโโ ec2
โ โ โ โโโ terragrunt.hcl
โ โ โโโ ecs
โ โ โ โโโ terragrunt.hcl
โ โ โโโ lambda
โ โ โ โโโ terragrunt.hcl
โ โ โโโ rds
โ โ โ โโโ terragrunt.hcl
โ โ โโโ terragrunt.hcl
โ โ โโโ vars.hcl
โ โ โโโ vpc
โ โ โโโ terragrunt.hcl
โ โโโ region.hcl
โโโ Modules
โโโ cloudwatch
โ โโโ Main.tf
โ โโโ Outputs.tf
โ โโโ Variables.tf
โโโ ec2
โ โโโ Main.tf
โ โโโ Outputs.tf
โ โโโ Variables.tf
โโโ ecs
โ โโโ Main.tf
โ โโโ Outputs.tf
โ โโโ Variables.tf
โโโ iam
โ โโโ Main.tf
โ โโโ Outputs.tf
โ โโโ Variables.tf
โโโ lambda
โ โโโ Main.tf
โ โโโ Outputs.tf
โ โโโ Variables.tf
โโโ rds
โ โโโ Main.tf
โ โโโ Outputs.tf
โ โโโ Variables.tf
โโโ vpc
โ โโโ Main.tf
โ โโโ Outputs.tf
โ โโโ Variables.tf
โโโ vpc-stage
โโโ Main.tf
โโโ Outputs.tf
โโโ Variables.tf
I've read about data terraform_remote_state but that's on module layer. For me it's not a good approach to do this in the module layer cause it's only for the stage enviroment. Is there a way to get the output from the remote state in the terragrunt.hcl in the stage folder from the dev environment to use this as input for the ec2 module?
I've used
dependency "vpc" {
config_path = "../vpc"
}
and then
vpc_id = dependency.vpc.outputs.vpc_id
for the input of ec2 module but that's only if it's in the same enviroment.
Best regards.
Assuming the dev and stage environments are in the same account (which it sounds like it is if it is in the same VPC), dependency blocks should work. AFAIK, this is the only limitation of dependency.
So there should be nothing preventing you from reading the stage environment vpc output using dependency blocks and using it in the dev environment. Are you seeing an error when you try that?
Hi yorinasub17 and thank you for your reply. Yes that works totally fine. I thought that dependencies are only per enviroment. Didn't think about referencing the vpc of the other enviroment. Just so simply and effective but didn't see that.
Thanks