Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terraform s3 backend vs terraform_remote_state

Tags:

terraform

According to the documentation, to use s3 and not a local terraform.tfstate file for state storage, one should configure a backend more or less as follows:

terraform {

  backend "s3" {
      bucket = "my-bucket-name"
      key = "my-key-name"
      region = "my-region"
  }
}

I was

  • using a local (terraform.tfstate) file
  • added the above snippet in my provided.tf file
  • run (again) terraform init
  • was asked by terraform to migrate my state to the above bucket

...so far so good...

But then comes this confusing part about terraform_remote_state ...

Why do I need this?

Isn't my state now saved remotely (on the aforemenetioned s3 bucket) already?

like image 797
pkaramol Avatar asked Jun 12 '18 15:06

pkaramol


People also ask

What is backend S3 in Terraform?

terraform-aws-remote-state-s3-backend. Terraform Module Registry. A terraform module to set up remote state management with S3 backend for your account. It creates an encrypted S3 bucket to store state files and a DynamoDB table for state locking and consistency checking.

How is the Terraform remote backend different than other state backends such as S3 consul etc?

How is the Terraform remote backend different than other state backends such asS3, Consul, etc.? A. It can execute Terraform runs on dedicated infrastructure on premises or inTerraform CloudB. It doesn't show the output of a terraform apply locallyC.

How is the Terraform remote backend different?

A Terraform backend determines how Terraform loads and stores state. The default backend, which you've been using this entire time, is the local backend, which stores the state file on your local disk. Remote backends allow you to store the state file in a remote, shared store.

What is the benefit of using a Terraform remote backend?

The cloud option includes an improved user experience and more features. The remote backend is unique among all other Terraform backends because it can both store state snapshots and execute operations for Terraform Cloud's CLI-driven run workflow. It used to be called an "enhanced" backend.


1 Answers

terraform_remote_state isn't for storage of your state its for retrieval in another terraform plan if you have outputs. It is a data source. For example if you output your Elastic IP Address in one state:

resource "aws_eip" "default" {
  vpc      = true
}

output "eip_id" {
  value = "${aws_eip.default.id}"
}

Then wanted to retrieve that in another state:

data "terraform_remote_state" "remote" {
  backend = "s3"
  config {
    bucket = "my-bucket-name"
    key = "my-key-name"
    region = "my-region"
  }
}

resource "aws_instance" "foo" {
  ...
}

resource "aws_eip_association" "eip_assoc" {
  instance_id   = "${aws_instance.foo.id}"
  allocation_id = "${data.terraform_remote_state.remote.eip_id}"
}

edit: If you are retrieving outputs in Terraform > 0.12 you need to include outputs

data "terraform_remote_state" "remote" {
  backend = "s3"
  config {
    bucket = "my-bucket-name"
    key = "my-key-name"
    region = "my-region"
  }
}

resource "aws_instance" "foo" {
  ...
}

resource "aws_eip_association" "eip_assoc" {
  instance_id   = "${aws_instance.foo.id}"
  allocation_id = "${data.terraform_remote_state.remote.outputs.eip_id}"
}
like image 95
Brandon Miller Avatar answered Dec 21 '22 20:12

Brandon Miller