terraform-aws-modules / terraform-aws-ec2-instance

Terraform module to create AWS EC2 instance(s) resources 🇺🇦
https://registry.terraform.io/modules/terraform-aws-modules/ec2-instance/aws
Apache License 2.0
758 stars 1.88k forks source link

Creation of an Intance with a Capacity Reservation ID [ref #277] #281

Closed schniber closed 2 years ago

schniber commented 2 years ago

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

⚠️ Note

Before you submit an issue, please perform the following first:

  1. Remove the local .terraform directory (! ONLY if state is stored remotely, which hopefully you are following that best practice!): rm -rf .terraform/
  2. Re-initialize the project root to pull down modules: terraform init
  3. Re-attempt your terraform plan or apply and check if the issue still persists

Versions

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

Terminal Output Screenshot(s)

Additional context

bryantbiggs commented 2 years ago

closed in #282

github-actions[bot] commented 2 years ago

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.