Error: cannot decode dynamic from flatmap in terraform

1k views Asked by At

While I was trying to run terraform plan using terraform0.13 on an old module which had previously run terraform apply using terraform0.11, I got the error:

Error: cannot decode dynamic from flatmap

The error does not indicate any specific line and was difficult to troubleshoot.

Part of my main.tf file:

provider "aws" {
   region  = var.region
   version = "~> 3.57.0"
}

data "terraform_remote_state" "database" {
     backend = "s3"

     config = {
         bucket = "my-s3-bucket-40370278403408"
         region = var.region
         key    = "app/database/terraform.tfstate"
     }
 }

 resource "aws_security_group_rule" "allow_mysql_from_server" {
     type                     = "ingress"
     protocol                 = -1
     from_port                = 3306
     to_port                  = 3306
     source_security_group_id = aws_security_group.ecs_fargate.id
     security_group_id = 
            data.terraform_remote_state.database.outputs.rds["security_group"]
  }

 .....
1

There are 1 answers

0
harshainfo On

Root Cause:

I was using remote states from dependent modules(database) in the above module as shown in the main.tf file. Though my old module had the remote states of dependent modules(database) in terraform0.11, I had actually run terraform apply on those dependent modules and their remote states are now actually in terraform0.13. Therefore, terraform cannot compare those remote states.

Fix:

I found the fix here

I needed to remove remote states using

terraform state rm data.terraform_remote_state.database

Then I could run terraform plan without errors!