Description
Description
When trying to call the module with the specification of a Capacity Reservation ID, the module ends up with the following error:
╷
│ Error: Invalid function argument
│
│ on .terraform/modules/terraform-aws-ec2-instance/main.tf line 45, in resource "aws_instance" "this":
│ 45: capacity_reservation_id = lookup(capacity_reservation_target.value, "capacity_reservation_id", null)
│ ├────────────────
│ │ capacity_reservation_target.value is "cr-xxxxxxxx"
│
│ Invalid value for "inputMap" parameter: lookup() requires a map as the
│ first argument.
╵
As per line 45 analysis, it looks like the bug is in the following statement:
capacity_reservation_id = lookup(capacity_reservation_target, "capacity_reservation_id", null)
it should rather be as follows:
capacity_reservation_id = lookup(capacity_reservation_specification.value.capacity_reservation_target, "capacity_reservation_id", null)
In this case, terraform should lookup in the capacity reservation target map (since it is the iterator in the for_each) rather than in its value for the mapping of the attribute capacity reservation id
- ✋ I have searched the open/closed issues and my issue is not listed.
⚠️ Note
Before you submit an issue, please perform the following first:
- Remove the local
.terraform
directory (! ONLY if state is stored remotely, which hopefully you are following that best practice!):rm -rf .terraform/
- Re-initialize the project root to pull down modules:
terraform init
- Re-attempt your terraform plan or apply and check if the issue still persists
Versions
-
Module version [Required]: 4.1.1
-
Terraform version: v1.2.5
- Provider version(s): v4.22.0
Reproduction Code [Required]
Steps to reproduce the behavior:
Call the module with following inputs:
module "ec2_capacity_reservation" {
source = "../../"
name = "${local.name}-capacity-reservation"
ami = data.aws_ami.amazon_linux.id
instance_type = "u-6tb1.56xlarge"
subnet_id = element(module.vpc.private_subnets, 0)
vpc_security_group_ids = [module.security_group.security_group_id]
associate_public_ip_address = true
capacity_reservation_specification = {
capacity_reservation_target = {
capacity_reservation_id = "cr-00000000000000000"
}
}
tags = local.tags
}
Expected behavior
TF plan should go through and the module should create an EC2 Instance which consumes the capacity reservation.
Actual behavior
TF plan fails in assigning the right reservation id