Sometimes it is useful to create some “links” between stacks. Going through a small example with the public Magento stack.

If we look the Terraform sample file, one of the required args is vpc_id.

stack-magento/terraform/magento.tf.sample

module "magento" {
  source = "module-magento"
  env    = "($ environment $)"
  vpc_id = "<vpc-id>"
1
2
3
4

We can also see that the Magento stack expose several Terraform outputs in the outputs.tf file

Each Terraform outputs are stored in the remote tfstate file as we saw in the Troubleshooting with a manual run of terraform plan section.

Getting back on our vpc_id parameter. Consider that we have a stack infrastructure dedicated to provide VPC and expose VPC ID as Terraform output.

We can rely on Terraform remote state provider to link our Magento stack to get a vpc_id from the infrastructure stack https://www.terraform.io/docs/providers/terraform/d/remote_state.html

To create this link, get the bucket name (infrastructure-terraform-remote-state) and path (infrastructure/infra/infrastructure-infra.tfstate) of the remote tfstate file used by the infrastructure stack. And create the following provider.tf file:

stack-magento/terraform/provider.tf

// Connect this stack to the infra stack to be able to use directly outputs
data "terraform_remote_state" "infrastructure" {
  backend = "s3"

  config {
    bucket = "infrastructure-terraform-remote-state"
    key    = "infrastructure/infra/infrastructure-infra.tfstate"
    region = "${var.aws_region}"
  }
}
1
2
3
4
5
6
7
8
9
10

With this new file, you should be able to use outputs of the infrastructure stack as terraform data in the Magento stack or config like this:

stack-magento/terraform/magento.tf.sample

module "magento" {
  source = "module-magento"
  env    = "($ environment $)"
  vpc_id = “${data.terraform_remote_state.infrastructure.vpc_id}"
...
1
2
3
4
5