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

The given key does not identify an element in this collection value: a number is required. #25367

Open
kalaibarak opened this issue Jun 15, 2022 · 2 comments
Labels
service/cloudwatch Issues and PRs that pertain to the cloudwatch service. service/ebs Issues and PRs that pertain to the ebs service. service/ec2ebs Issues and PRs that pertain to the ec2ebs service. service/ec2 Issues and PRs that pertain to the ec2 service. stale Old or inactive issues managed by automation, if no further action taken these will get closed. waiting-response Maintainers are waiting on response from community or contributor.

Comments

@kalaibarak
Copy link

kalaibarak commented Jun 15, 2022

Terraform Enterprise edition 0.13.7
provider aws version 3.37.0
data.tf

data "aws_instance" "instance_name" {
  for_each      = toset(data.aws_instances.instance_cloudwatch.ids)
  instance_id   = each.value
 }
data "aws_instances" "instance_cloudwatch" {

  instance_tags = {
    Type        = var.type
  }
}
data "aws_ebs_volumes" "cw_volumes" {
  for_each    = toset(flatten(data.aws_instances.instance_cloudwatch.ids))
}

The resource section

resource "aws_cloudwatch_metric_alarm" "iops" {
  for_each                  = toset(data.aws_instances.instance_cloudwatch.ids)
  not related lines
  metric {
      metric_name = "VolumeWriteOps"
      namespace   = "AWS/EBS"
      period      = "60"
      stat        = "Sum"
      dimensions = {
        InstanceId   = each.value
        VolumeId     = toset(flatten(values(data.aws_ebs_volumes.cw_volumes)[each.key].*))
      }
    }  

error
on modules/notification/iops.tf line 34, in resource "aws_cloudwatch_metric_alarm" "iops":
34: VolumeId = toset(flatten(values(data.aws_ebs_volumes.cw_volumes)[each.key].*))
|----------------
| data.aws_ebs_volumes.cw_volumes is object with 2 attributes
| each.key is "i-078c2050de4851e93"

The given key does not identify an element in this collection value: a number
is required.

@github-actions github-actions bot added needs-triage Waiting for first response or review from a maintainer. service/cloudwatch Issues and PRs that pertain to the cloudwatch service. service/ebs Issues and PRs that pertain to the ebs service. service/ec2 Issues and PRs that pertain to the ec2 service. service/ec2ebs Issues and PRs that pertain to the ec2ebs service. labels Jun 15, 2022
@justinretzolk
Copy link
Member

Hey @kalaibarak 👋 Thank you for taking the time to raise this! There are a couple of things that I noticed when attempting to reproduce this.

The first is the reason that you're receiving the error you mentioned. This is coming from the VolumeId argument that you're passing. Since you're specifying values(data.aws_ebs_volumes.cw_volumes), the keys are removed, as shown in the following example output from my test:

Configuration:

output "example" {
  value = values(data.aws_ebs_volumes.cw_volumes)
}

Output:

  + example = [
      + {
          + filter = null
          + id     = "us-east-1"
          + ids    = [
              + "vol-0e6XXXXXXXXXX",
             ...omitted for brevity
            ]
          + tags   = null
        },
      + {
          + filter = null
          + id     = "us-east-1"
          + ids    = [
              + "vol-0e66ca09c26587569",
             ...omitted for brevity
            ]
          + tags   = null
        },
    ]

This means that when you then try to index with [each.key], there's no key to index off of, which is why the error is thrown. This can be mitigated by updating your configuration as follows:

        VolumeId     = toset(flatten(data.aws_ebs_volumes.cw_volumes[each.key].ids))

Updating my example (I'm passing an instance ID directly, just for the sake of making the reproduction easier to read), yields this change to the output:

Configuration:

output "example" {
  value = toset(flatten(data.aws_ebs_volumes.volumes["i-0b1edXXXXXXX"].ids)) // ID obfuscated for security's sake
}

Output:

  + example = [
      + "vol-0e6XXXXXXXX",
      ...omitted for brevity
    ]

Unfortunately, that won't entirely solve your issue, as there were two additional things that came up when I got past the initial issue that you reported:

  1. The VolumeId argument expects a string rather than a list of strings
  2. As far as I can tell, the AWS/EBS namespace supports the VolumeId dimension, but does not support InstanceId. This could be a misunderstanding on my part, but may be something that you run into, so I figured it was worth noting.

@justinretzolk justinretzolk added waiting-response Maintainers are waiting on response from community or contributor. and removed needs-triage Waiting for first response or review from a maintainer. labels Jun 16, 2022
Copy link

github-actions bot commented Jun 6, 2024

Marking this issue as stale due to inactivity. This helps our maintainers find and focus on the active issues. If this issue receives no comments in the next 30 days it will automatically be closed. Maintainers can also remove the stale label.

If this issue was automatically closed and you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thank you!

@github-actions github-actions bot added the stale Old or inactive issues managed by automation, if no further action taken these will get closed. label Jun 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
service/cloudwatch Issues and PRs that pertain to the cloudwatch service. service/ebs Issues and PRs that pertain to the ebs service. service/ec2ebs Issues and PRs that pertain to the ec2ebs service. service/ec2 Issues and PRs that pertain to the ec2 service. stale Old or inactive issues managed by automation, if no further action taken these will get closed. waiting-response Maintainers are waiting on response from community or contributor.
Projects
None yet
Development

No branches or pull requests

2 participants