Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

for_each is failing not yet created resources #22629

Closed
Menur opened this issue Aug 28, 2019 · 7 comments
Closed

for_each is failing not yet created resources #22629

Menur opened this issue Aug 28, 2019 · 7 comments
Labels
bug config crash v0.12 Issues (primarily bugs) reported against v0.12 releases

Comments

@Menur
Copy link

Menur commented Aug 28, 2019

Terraform Version

Terraform v0.12.7

Terraform Configuration Files

terraform {
  required_version = ">= 0.12"
}


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


locals {
  topics = [
      "topicA",
      "topicB",
  ]
  prefix = "prefix"
}


resource "aws_sns_topic" "topics" {
  for_each = toset(local.topics)
  name     = "${local.prefix}-${each.value}"
}

resource "aws_sqs_queue" "queue" {
  name = "${local.prefix}-Queue"
}


resource "aws_sns_topic_subscription" "subscription" {
  for_each  = toset(values(aws_sns_topic.topics)[*].arn)
  protocol  = "sqs"
  endpoint  = aws_sqs_queue.queue.arn
  topic_arn = each.value
}

Crash Output

https://gist.github.com/Menur/228b400095d0f972226608123a12fde7

Expected Behavior

SQS and two SNS topics should be created and subscribed to SNS

Actual Behavior

!!!!!!!!!!!!!!!!!!!!!!!!!!! TERRAFORM CRASH !!!!!!!!!!!!!!!!!!!!!!!!!!!!

Terraform crashed! This is always indicative of a bug within Terraform.
A crash log has been placed at "crash.log" relative to your current
working directory. It would be immensely helpful if you could please
report the crash with Terraform[1] so that we can fix this.

When reporting bugs, please include your terraform version. That
information is available on the first line of crash.log. You can also
get it by running 'terraform --version' on the command line.

[1]: https://github.com/hashicorp/terraform/issues

!!!!!!!!!!!!!!!!!!!!!!!!!!! TERRAFORM CRASH !!!!!!!!!!!!!!!!!!!!!!!!!!!!

Steps to Reproduce

  1. terraform init
  2. terraform plan

Additional Context

When SNS topics aren't created terraform will crash.
When SNS topics are created terraform will proceed.

@pselle
Copy link
Contributor

pselle commented Aug 28, 2019

@Menur Currently working on a fix for this panic, but the case you're writing will error/won't work, because the values passed to for_each must be known. Once this is fixed, you'll get the following error, but the described work-around will help you in the present:

Error: Invalid for_each argument

The "for_each" value depends on resource attributes that cannot be determined
until apply, so Terraform cannot predict how many instances will be created.
To work around this, use the -target argument to first apply only the
resources that the for_each depends on.

Thank you for the report!

@hashibot hashibot added config v0.12 Issues (primarily bugs) reported against v0.12 releases waiting-response An issue/pull request is waiting for a response from the community labels Aug 28, 2019
@Menur
Copy link
Author

Menur commented Aug 29, 2019

Know means constant, right?
Passing values to foreach from data or variable results in crash too.

data "aws_caller_identity" "current" {}
data "aws_region" "current" {}

locals {
  topicARNs = [
    for messageName in local.topics : "arn:aws:sns:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:${local.prefix}-${messageName}"    
  ]
}

resource "aws_sns_topic_subscription" "subscription" {
  for_each  = toset(local.topicARNs) ##toset(values(aws_sns_topic.topics)[*].arn)
  protocol  = "sqs"
  endpoint  = aws_sqs_queue.queue.arn
  topic_arn = each.value
  depends_on = [aws_sns_topic.topics]
}

When region name and account_id are specified as constant strings in locals it works.

@ghost ghost removed the waiting-response An issue/pull request is waiting for a response from the community label Aug 29, 2019
@pselle
Copy link
Contributor

pselle commented Sep 3, 2019

@Menur Known doesn't mean constant, it's a bit different.

Sorry to confirm that the crash will still happen with known values on the current release of Terraform -- my instructions for the work around (-target) was hoping to help you in the meantime. Meanwhile, the fix is merged upstream, so things will improve once 0.12.8 is released.

@Menur
Copy link
Author

Menur commented Sep 4, 2019

-target is helpful and it works.

@pselle
Copy link
Contributor

pselle commented Sep 4, 2019

@Menur Thanks so much for the report back! I'll close this issue when the upstream fix is released :)

@pselle
Copy link
Contributor

pselle commented Sep 4, 2019

0.12.8 is out! https://www.terraform.io/downloads.html

@pselle pselle closed this as completed Sep 4, 2019
@ghost
Copy link

ghost commented Oct 5, 2019

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@ghost ghost locked and limited conversation to collaborators Oct 5, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug config crash v0.12 Issues (primarily bugs) reported against v0.12 releases
Projects
None yet
Development

No branches or pull requests

3 participants