Referencing an element while using count in terraform resource and handling the case when count is 0

0 votes

I have an AWS WAFv2 Web ACL Logging configuration declared like this

resource "aws_wafv2_web_acl_logging_configuration" "logging_configuration" {
  count = var.enable_logging && var.waf_s3_log_bucket_id != "" ? 1 : 0

  log_destination_configs = [aws_kinesis_firehose_delivery_stream.waf_log_stream.*.arn[0]]
  resource_arn            = aws_wafv2_web_acl.waf.arn

  depends_on = [aws_kinesis_firehose_delivery_stream.waf_log_stream]
}

This code works, but the linter gave me a warning to not use the deprecated * notation when referencing an item that is in a list and to use square bracket notation instead. so I changed it

log_destination_configs = [aws_kinesis_firehose_delivery_stream.waf_log_stream[count.index].arn[0]]

When I do terraform plan it now runs, but then gives me this error

Error: Invalid index

aws_kinesis_firehose_delivery_stream.waf_log_stream is tuple with 1 element

count.index is 0

This value does not have any indices.

I understand the error, but there are 2 things that confuse me

  1. If the count is 0, this resource should not even be created. Why would it be flagged by Terraform if that's the case?

  2. If count is not 0, then the error that I'm getting should not exist. Why is Terraform CLI giving me this error?

Can someone tell me what the issue is here and how I can resolve this?
 

Dec 20, 2022 in AWS by Tejashwini
• 3,820 points
1,260 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In AWS

+1 vote
1 answer

What is the difference between an Instance, AMI and Snaphots in AWS?

AMI is the Amazon Machine Image which provides ...READ MORE

answered May 24, 2019 in AWS by ArchanaNagur
• 2,360 points
4,225 views
0 votes
0 answers
0 votes
1 answer

Create Snapshot of EBS and attach to EC2 using Terraform

You can also create a snapshot using ...READ MORE

answered Oct 29, 2018 in AWS by Priyaj
• 58,020 points
2,241 views
0 votes
1 answer

How to attach an EBS volume to EC2 instance using terraform?

Hi@akhtar, You can use aws_volume_attachment resource to attach ...READ MORE

answered Jun 12, 2020 in Terraform by MD
• 95,460 points
8,869 views
0 votes
1 answer

How to attach Elastic IP in EC2 instance using Terraform code?

Hi@akhtar, You can use aws_eip resource in your ...READ MORE

answered Jul 22, 2020 in Terraform by MD
• 95,460 points
8,581 views
0 votes
1 answer

How to read the volume ID of the EC2 instance using Terraform?

Hi@akhtar, You won't be able to extract EBS ...READ MORE

answered Dec 4, 2020 in Terraform by MD
• 95,460 points
2,304 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP