Jiew Meng
Jiew Meng

Reputation: 88187

Configuring remote state in terrform seems duplicated?

I am configuring remote state in terraform like:

provider "aws" {
  region = "ap-southeast-1"
}

terraform {
  backend "s3" {
    bucket = "xxx-artifacts"
    key = "terraform_state.tfstate"
    region = "ap-southeast-1"
  }
}

data "terraform_remote_state" "s3_state" {
  backend = "s3"
  config {
    bucket = "xxx-artifacts"
    key = "terraform_state.tfstate"
    region = "ap-southeast-1"
  }
}

It seems very duplicated tho, why is it like that? I have the same variables in terraform block and the terraform_remote_state data source block. Is this actually required?

Upvotes: 0

Views: 446

Answers (1)

ydaetskcoR
ydaetskcoR

Reputation: 56849

The terraform.backend configuration is for configuring where to store remote state for the Terraform context/directory where Terraform is being ran from.

This allows you to share state between different machines, backup your state and also co-ordinate between usages of a Terraform context via state locking.

The terraform_remote_state data source is, like other data sources, for retrieving data from an external source, in this case a Terraform state file.

This allows you to retrieve information stored in a state file from another Terraform context and use that elsewhere.

For example in one location you might create an aws_elasticsearch_domain but then need to lookup the endpoint of the domain in another context (such as for configuring where to ship logs to). Currently there isn't a data source for ES domains so you would need to either hardcode the endpoint elsewhere or you could look it up with the terraform_remote_state data source like this:

elasticsearch/main.tf

resource "aws_elasticsearch_domain" "example" {
  domain_name           = "example"
  elasticsearch_version = "1.5"

  cluster_config {
    instance_type = "r4.large.elasticsearch"
  }

  snapshot_options {
    automated_snapshot_start_hour = 23
  }

  tags = {
    Domain = "TestDomain"
  }
}

output "es_endpoint" {
  value = "$aws_elasticsearch_domain.example.endpoint}"
}

logstash/userdata.sh.tpl

#!/bin/bash
sed -i 's/|ES_DOMAIN|/${es_domain}/' >> /etc/logstash.conf

logstash/main.tf

data "terraform_remote_state" "elasticsearch" {
  backend = "s3"
  config {
    bucket = "xxx-artifacts"
    key = "elasticsearch.tfstate"
    region = "ap-southeast-1"
  }
}

data "template_file" "logstash_config" {
  template = "${file("${path.module}/userdata.sh.tpl")}"

  vars {
    es_domain = "${data.terraform_remote_state.elasticsearch.es_endpoint}"
  }
}

resource "aws_instance" "foo" {
  # ...
  user_data = "${data.template_file.logstash_config.rendered}"
}

Upvotes: 1

Related Questions