Search code examples
terraformdevopsinfrastructure-as-codeterraform0.12+

terraform_remote_state data block syntax


I'm working on an AWS multi-account setup with Terraform. I've got a master account that creates several sub-accounts, and in the sub-accounts I'm referencing the master's remote state to retrieve output values.

The terraform plan command is failing for this configuration in a test main.tf:

terraform {
  required_version = ">= 0.12.0"

  backend "s3" {
    bucket = "bucketname"
    key    = "statekey.tfstate"
    region = "us-east-1"
  }
}

provider "aws" {
  region  = "us-east-1"
  version = "~> 2.7"
}

data "aws_region" "current" {}

data "terraform_remote_state" "common" {
  backend = "s3"
  config {
        bucket = "anotherbucket"
        key    = "master.tfstate"
  }
}

With the following error:

➜  test terraform plan

Error: Unsupported block type

  on main.tf line 20, in data "terraform_remote_state" "common":
  20:   config {

Blocks of type "config" are not expected here. Did you mean to define argument
"config"? If so, use the equals sign to assign it a value.

From what I can tell from the documentation, this should be working… what am I doing wrong?

➜  test terraform -v  
Terraform v0.12.2
+ provider.aws v2.14.0

Solution

  • Seems the related document isn't updated after upgrade to 0.12.x

    As the error prompt, add = after config

    
    data "terraform_remote_state" "common" {
      backend = "s3"
      config = {
            bucket = "anotherbucket"
            key    = "master.tfstate"
      }
    }
    

    If the problem is fixed, recommend to raise a PR to update the document, then others can avoid the same issue again.