AWS remote state with terraform 0.12.12

Hi all,

I have an error when i want to do remote_state.
I read this documentation : The terraform_remote_state Data Source | Terraform | HashiCorp Developer

My first terraform state build VPC, with resource to AWS.
Hosting statement to S3 with DynamoDB lock.

/*

  • Set AWS provider

  • Set region
  • Set version
    */
    provider “aws” {
    region = var.region
    version = “~> 2.0”
    }

/*

  • Set backend

  • Set bucket S3 for hosting state file
    */
    terraform {
    required_version = “>= 0.12.1”

backend “s3” {
encrypt = true
bucket = “vpc-platform”
region = “eu-west-1”
key = “terraform/layer-base”
dynamodb_table = “dynamodb-vpc-platform-S3-lock”
}
}

After build network infrastructure, i want to create new statement :

/*

  • Set AWS provider

  • Set region
  • Set version
    */
    provider “aws” {
    region = var.region
    }

terraform {
required_version = “>= 0.12.1”

backend “s3” {
encrypt = true
bucket = “vpc-platform”
region = “eu-west-1”
key = “terraform/layer-app”
dynamodb_table = “dynamodb-vpc-platform-S3-lock”
}
}

data “terraform_remote_state” “stack-base” {
backend = “s3”

config = {
bucket = “vpc-platform”
key = “terraform/layer-base”
region = “eu-west-1”
}
}

At this time all config is OK ! But when i want to create a simple “security_group”, i have an error :

resource “aws_security_group” “allow_bastion” {
name = “security_group_bastion_ssh”
description = “Allow SSH traffic”
vpc_id = data.terraform_remote_state.stack-base.outputs.vpc_id

ingress {
from_port = 22
to_port = 22
protocol = “tcp”
cidr_blocks = [“0.0.0.0/0”]
}
}

Issue :

Refreshing Terraform state in-memory prior to plan…
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.

data.terraform_remote_state.stack-base: Refreshing state…

Error: Unsupported attribute

on EC2.tf line 5, in resource “aws_security_group” “allow_bastion_ssh”:
5: vpc_id = data.terraform_remote_state.stack-base.outputs.vpc_id
|----------------
| data.terraform_remote_state.stack-base.outputs is object with no attributes

This object does not have an attribute named “vpc_id”

Regards,

Same problem here, did you find a solution or get any feedback ?

Hi and sorry for this funny question but are you sure to have an output definition in your first state ?
Example :
output “vpc_id” {
description = “Reference to my VPC’s Identifier”
value = “your_vpc_id_ref”
}

Regards

Yes I’m sure, when I investigate the state file I see:

“outputs”: {
“dns_zone_id”: {
“value”: “***********”,
“type”: “string”
}
},

Same problem. In my own case, the output value wasn’t writing in tfstate file. You should check to write the output value in tfstate. Execute terraform apply if empty. The output value is written in tfstate and this problem will be solve.

Regards