What’s up Guys! Welcome to automationcalling.com


This post is mainly focus on how to get remote state in the terraform when you have script developed in different folders under “modules”. For eg., The below listed snapshot shows the AWS resources were created in different hierarchy which is independent or no relation to other folder under Modules.

The purpose of above structure helps you to plan, apply and destroy for specific resources without interrupting others in terms of infrastructure as code, in fact it helps you to do cost reduction for eg., resources like elastic cache, SNS, SQS you can turn down after 6 PM and turn on After 10 AM. The challenge is, the ARN name or endpoint or host name gets changed when you destroy and create again. In this case, how do to handle this situation without breaking your application when resources are back in AWS? Well, the answer is getting remote state.


  1. Create S3 bucket in AWS
  2. Create a unique key for all resources related under environment in S3

3. Make sure to create tfstate file in s3 for each resource whenever you create a module and input for dev, QA, preprod or prod environment. For eg., refer below snapshot and make sure to have dynamo db table for state lock.

How to get Remote State for Different Resources

Here is few real time scenarios:

  1. ElasticCache ARN/Configuration endpoints get changed often when you destroy and create again
  2. Everyday I destroy my environment in the evening and up in the morning
  3. Storing all SSM Param in one folder so that whenever changes, I can get them in runtime for other dependencies
  4. Instead of hard coding VPC, subnet or any resources related name, ARN or host configuration, how about getting those values in Remote State file.

To address this challenge, “data.tf” plays major role here.

In this example, we are going to get VPC Information from Remote State file exist in S3 folder which is described in point no.4 listed above. After get the Remote State and get output variable of vpc id or VPC CIDR details to apply in ElasticCache dynamically.

For Module input for eg., QA, Dev, Preprod or Prod under each resource (for eg., I created elasticcache) folder, create a file name called “data.tf” and enter details like below.

If you compare with above 2 images, both were pointed to the same S3 bucket, but the 1st image is creating remote state and the 2nd image is to retrieve the remote state file using data.

Now, it’s time to apply VPC details in ElasticCache terraform.tf file using Remote State.

In above example, ${data.terraform_remote_state.vpc_information}=> indicates the state present in the data.tf file

outputs.vpc_id => indicates the variable name of runtime values that were exported in the output.tf file.

That’s it. This approach helps to hold the state in runtime variable and avoid hard coding details.

Hope! this post may be helpful to you guys.

Thanks for your time, please do subscribe for more updates! 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s