How to reference aws resources created in a Terraform cloud workspace in another workspace?

I would like to split terraform in multiple cloud workspaces. In one workspace “A” i have created AWS VPC, subnet, IGW etc. I have created another workspace “B” for a specific project to spin up some VM’s.

How do i reference the VPC id, subnet id created in workspace “A” in Workspace “B” with out copy pasting the values ?

Thanks for your time …

Hi,

You can use the Terraform Remote State Data stanza. This allows you to read the state of other workspaces. As long as your API key has read access to the referenced remote workspace you should be good to go.

data "terraform_remote_state" "vpc" {
  backend = "remote"

  config = {
    organization = "hashicorp"
    workspaces = {
      name = "vpc-prod"
    }
  }
}

# Terraform >= 0.12
resource "aws_instance" "foo" {
  # ...
  subnet_id = data.terraform_remote_state.vpc.outputs.subnet_id
}

# Terraform <= 0.11
resource "aws_instance" "foo" {
  # ...
  subnet_id = "${data.terraform_remote_state.vpc.subnet_id}"
}

Kind regards,
Nic

Thanks Nic. It works.

Nic, how do I get Workspace B to run after Workspace A has run … Similar to a Pipeline in Jenkins ? Is this possible in TFE?

I believe you have to run it manually one after the other or trigger a run via the api …