../_images/login_button.png ../_images/signup_button.png



How do I get data and state from other workspaces?

It is common practice with Terraform that different elements of infrastructure might be deployed via separate Terraform configurations and have separate workspaces and state files. Examples of this are:

  • Deploy a VPC in one workspace, then deploy instances in other workspaces.

  • Deploy a Kubernetes cluster in one workspace, then deploy pods to the cluster in separate workspaces.

In these cases the dependent Terraform configurations (instances, pods) need to access data from the state file of the other workspaces.

This is easy to achieve in Scalr IaCP through the Terraform data source terraform_remote_state.

  1. Any workspace that will share it’s state must publish attributes/data as outputs. For example the Terraform configuration used in the VPC workspace called webapp-vpc would need to publish the ID of the VPC, so it would contain code like this.

output "the_vpc_id" {
  description = "The VPC ID for your subnets"
  value = aws_vpc.my_vpc.id
}
  1. The Terraform configuration that needs to access this data then includes a terraform_remote_state { } pointing to the workspace. Note the remote backend config needs the environment ID and workspace name where the VPC was created.

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

  config = {
    hostname = "my-account.scalr.io"
    organization = "org-xxxxxxxxxxx"
    workspaces = {
      name = "webapp-vpc"
    }
  }
}
  1. This Terraform configuration can now access the outputs from the other workspace with an expression like data.terraform_remote_state.vpc-1.outputs.the_vpc_id, e.g. when creating subnet.

resource "aws_subnet" "my_subnet" {
  vpc_id       = data.terraform_remote_state.vpc-1.outputs.the_vpc_id
  cidr_block   = "10.1.1.1/25"
  :
  :
}