3
votes

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
1
Out of curiosity, what things are you pulling from remote state? In 99% of cases where people are using the remote state data source I would recommend replacing it with a more native provider data source instead. - ydaetskcoR
@ydaetskcoR I'm pulling out AWS account numbers. The parent account is creating several sub accounts with AWS Organizations for environments and projects, and each project has it's own Terraform config. - Jon Buys

1 Answers

8
votes

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.