Terragrunt: Use resources from other enviroments

Created on 19 Sep 2019  ยท  2Comments  ยท  Source: gruntwork-io/terragrunt

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.

question

All 2 comments

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

antonbabenko picture antonbabenko  ยท  3Comments

zachwhaley picture zachwhaley  ยท  3Comments

GauntletWizard picture GauntletWizard  ยท  3Comments

jtai-omniex picture jtai-omniex  ยท  3Comments

MacFlurry picture MacFlurry  ยท  4Comments