Using Remote State for Modular Code

Normally when using “Terraform Apply” command, Terraform automatically places records into your local folder as a “Terraform State” file. This file contains a custom JSON format that records a mapping from the Terraform resources in your configuration files to the representation of those resources in the real world.

As long as anyone else or any other module does not need to know about the resources created by Terraform, we are happy that Terraform keeps track of what it deployed by creating a “Terraform State” file LOCALLY in the same folder.

However, what if another folder (module) needs to know about a VPC that has already been created? “Terraform Remote State” to the rescue!

Terraform Remote State

Using AWS S3 bucket

The diagram above represents sending resource data to a Terraform remote state. First, we create a shared location, like an AWS S3 bucket. Then we OUTPUT resource data into “Terraform’s remote state.” A module (another folder) then gets the resource data about what has been created from our remote state by using a “Data statement,” which will pull the resource data from Terraform’s Remote State.

Remote State Outputs

First, we need to output the data to “Terraform Remote State.” For example, using an AWS S3 bucket as file storage for Terraform remote state files.

Set S3 as our terraform remote state
Two requirements for creating and using S3 as a remote state

Beyond the obvious requirement that you must have permission to create & manage the contents of the shared location like an AWS S3 bucket, there are crucial caveats to remember.

  • Before trying to use an S3 bucket in Terraform, the BUCKET must exist before you can declare the bucket as a resource.
  • The “KEY” component of the configuration will not exist until you create a resource in that bucket.
  • The resource statement for Terraform Remote State will NOT accept any variables.
Output resources to terraform remote state

Once we’ve put some code into our folder about our AWS S3 bucket Terraform Remote State, we can then start outputting data about our resources into the bucket. The example below is an “OUTPUT” statement that will output our new VPC ID into Terraform Remote State.

The format is:

Output “Name” { value = module.<remote state name>.<output name> “

Obtaining the remote state Output data

Getting resource information

Another module needs information about resources already deployed (like a VPC that has already been deployed, for example). The other module get the information from Terraform remote state.

In the format:

Where the “Data” statement name can be any name you want to give to this function. It can be any name you want to assign; I like to give it a name like the “Test-VPC” or “Dev-Environment” to remind me what data is being obtained from Remote State.

Once we have declared terraform remote state resources and the configuration of our shared location, we can pull the data.

We use the following format to get the data:

Value = data.terraform_remote_state.<name>.outputs<output name>

Author: Joseph OMara

Solution Architect for Information Technology. Hobbies include amateur photography, backpacking, camping, hiking, and travel

One thought on “Using Remote State for Modular Code”

Leave a ReplyCancel reply

Discover more from Joseph O'Mara

Subscribe now to keep reading and get access to the full archive.

Continue reading

Exit mobile version
%%footer%%