Terraform: Failure on resolving output and data source due to missing map entry prevents state destruction

Created on 10 Aug 2020  路  2Comments  路  Source: hashicorp/terraform

Terraform Version

Terraform v0.12.29
+ provider.aws v3.1.0

Terraform Configuration Files

provider "aws" {
  # ...
}

resource "aws_cloudformation_stack" "failing_resource" {
  name = "FailingResource"

  template_body = jsonencode({
    AWSTemplateFormatVersion = "2010-09-09"

    Resources = {
      FailingResource = {
        Type = "Custom::FailingResource"
        Properties = {
          ServiceToken = "112233445566778899"
        }

      }
    }

    Outputs = {
      MissingOutput = {
        Value = "MissingValue"
      }
    }
  })
}

data "aws_cloudformation_stack" "missing_stack" {
  name = "TheStack${aws_cloudformation_stack.failing_resource.outputs["MissingOutput"]}"
}

output "missing_output" {
    value = "The the result is: ${aws_cloudformation_stack.failing_resource.outputs["MissingOutput"]}"
}

Debug Output

Crash Output

$ terraform destroy
aws_cloudformation_stack.failing_resource: Refreshing state... [id=arn:aws:cloudformation:us-east-1:************:stack/FailingResource/aeb06230-daf1-11ea-b81c-0a34514375d1]

Error: Invalid index

  on main.tf line 70, in data "aws_cloudformation_stack" "missing_stack":
  70:   name = "TheStack${aws_cloudformation_stack.failing_resource.outputs["MissingOutput"]}"
    |----------------
    | aws_cloudformation_stack.failing_resource.outputs is empty map of string

The given key does not identify an element in this collection value.


Error: Invalid index

  on main.tf line 74, in output "missing_output":
  74:     value = "The the result is: ${aws_cloudformation_stack.failing_resource.outputs["MissingOutput"]}"
    |----------------
    | aws_cloudformation_stack.failing_resource.outputs is empty map of string

The given key does not identify an element in this collection value.

Expected Behavior


Outputs and data sources should not be resolved as its dependencies are not in a valid state.

I should be able to destroy the my state and underlying resources.

Actual Behavior


The failure on executing the cloud formation stack cause its state to contains an empty map of outputs instead of the expected map.

It is causing the output and data source evaluation to fail, preventing it from destroying the invalid state.

Steps to Reproduce

  1. terraform init
  2. terraform apply
  3. terraform destroy

Additional Context

References

bug confirmed v0.12 v0.13

Most helpful comment

@mikhasd as a short-term workaround in case you're blocked ont his, you can use a terraform config with just your aws provider block to set the region and run a destroy just with that and your state.

All 2 comments

@mikhasd as a short-term workaround in case you're blocked ont his, you can use a terraform config with just your aws provider block to set the region and run a destroy just with that and your state.

I have "this code" wrapped into a module, and just noticed that the output is being refreshed before the cloudfomation stack is createad.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

zeninfinity picture zeninfinity  路  3Comments

rjinski picture rjinski  路  3Comments

ketzacoatl picture ketzacoatl  路  3Comments

larstobi picture larstobi  路  3Comments

rnowosielski picture rnowosielski  路  3Comments