2023-03-16

Using Outputs from Different Terraform Directories

Using Outputs from Different Terraform Directories

In order to use the output from a different Terraform directory, you have two main methods at your disposal:

  • Hardcoding the Results of Another Directory's Apply
    The simplest method is to hardcode the output results from one directory into another directory's variable (var). This involves copying the output results and pasting them directly into your Terraform configuration file.

  • Using Remote State
    The second method involves using the terraform_remote_state data source to access the state data stored in a different directory. This can be particularly useful if your state files are stored in a remote backend like an S3 bucket.

Hardcoding

Hardcoding is the practice of directly inserting the output results from one directory into another directory's variable. In other words, it's copying and pasting the data you need directly into your Terraform configuration files. This approach requires no additional setup or configuration beyond obtaining the needed data.

Consider the following example:

dir_a/main.tf
resource "aws_vpc" "main" {
  cidr_block       = "10.0.0.0/16"
  instance_tenancy = "default"

  tags = {
    "Name" = "my-vpc-dir1"
  }
}

output "vpc_id" {
  value = aws_vpc.main.id
}

In this example, main.tf file at dir_a creates a VPC and outputs its ID. We can then hardcode this output into dir_b/main.tf as follows:

dir_b/main.tf
variable "vpc_id" {
  default = "vpc-abcdefgh"  # Hardcoded VPC ID from Directory A
}

resource "aws_subnet" "main" {
  vpc_id     = var.vpc_id  # Using the hardcoded VPC ID
  cidr_block = "10.0.1.0/24"

  tags = {
    "Name" = "my-subnet-dir2"
  }
}

The advantage of hardcoding is its simplicity. It is straightforward, requires little to no extra configuration, and is generally efficient for smaller to medium-sized projects.

However, hardcoding may not be suitable for larger scale development due to its lack of scalability.

Remote State

In Terraform, the "state" is a record of your infrastructure resources. By default, Terraform stores state locally in a file named terraform.tfstate. When working with larger projects or teams, however, storing state information in a remote backend is recommended. Remote backends allow teams to store state files in a shared, centralized location, often with versioning, locking, and other important features.

A remote state is a tfstate file that is stored in a remote data store, such as an S3 bucket. Terraform provides the terraform_remote_state data source that allows you to access the state data stored in a different directory, which can be particularly useful in larger projects or when trying to organize your Terraform configurations.

To utilize the terraform_remote_state data source, you need to define a data block in your Terraform configuration file and specify the backend type and its configuration.

Consider the following example:

data "terraform_remote_state" "vpc" {
  backend = "s3"

  config = {
    bucket = "my-terraform-state"
    key    = "vpc/terraform.tfstate"
    region = "us-west-2"
  }
}

output "vpc_id" {
  value = data.terraform_remote_state.vpc.outputs.vpc_id
}

In this example, we're accessing the remote state file stored in an S3 bucket. The bucket name is my-terraform-state, and the state file is stored under the key vpc/terraform.tfstate. The output block then references the vpc_id stored in that remote state.

Let's consider an example where two directories, dir_a and dir_b, each have their own Terraform configurations.

dir_a/main.tf
resource "aws_vpc" "main" {
  cidr_block       = "10.0.0.0/16"
  instance_tenancy = "default"

  tags = {
    "Name" = "my-vpc-dir1"
  }
}

output "vpc_cidr" {
  value = aws_vpc.main.cidr_block
}

In dir_a, a VPC is created and its CIDR block is output. Now, we want to create a subnet within this VPC in dir_b:

dir_b/main.tf
data "terraform_remote_state" "vpc" {
  backend = "s3"

  config = {
    bucket = "my-app-terraform-state"
    key    = "dir_a/terraform.tfstate"
    region = "ap-northeast-1"
  }
}

resource "aws_subnet" "main" {
  vpc_id     = data.terraform_remote_state.vpc.outputs.vpc_id
  cidr_block = "10.0.1.0/24"

  tags = {
    "Name" = "my-subnet-dir2"
  }
}

In dir_b, we use the terraform_remote_state data source to access the state file of dir_a from the S3 bucket. The VPC ID is then used in creating the subnet.

While the remote state function can be very handy, it's important to understand that overuse or misuse can lead to complexities. Also, any changes in the remote state data source will necessitate re-running terraform init to update the configuration.

References

https://developer.hashicorp.com/terraform/language/state/remote-state-data

Ryusei Kakujo

researchgatelinkedingithub

Focusing on data science for mobility

Bench Press 100kg!