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

fix: Assignment of the Capacity Reservation id to an instance #282

Merged
merged 2 commits into from
Aug 10, 2022
Merged

Conversation

schniber
Copy link
Contributor

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

Motivation and Context

Fixes #277 and #281

Breaking Changes

No breaking changes, just fixing a typo in line 45 in the main.tf

How Has This Been Tested?

  • I have updated at least one of the examples/* to demonstrate and validate my change(s)
  • I have tested and validated these changes using one or more of the provided examples/* projects
  • I have executed pre-commit run -a on my pull request

@schniber schniber changed the title fix of the assignment of the capacity reservation id fix: Assignment of the Capacity Reservation id to an instance Jul 23, 2022
@schniber
Copy link
Contributor Author

@bryantbiggs : Hello Bryant, I noticed that my logic on the previous PR on the same topic was wrong.

In this PR I have revisited this statement in the main.tf:

capacity_reservation_id = lookup(capacity_reservation_specification.value.capacity_reservation_target, "capacity_reservation_id", null)

I also tested thoroughly using the complete example and this is now satisfactory.

Sorry again for the double review work.

Enjoy your weekend !

@schniber
Copy link
Contributor Author

Hello @bryantbiggs,

kind reminder for this PR which is ready for review.

Thanks again for your feedback.

Bests.

@@ -42,7 +42,7 @@ resource "aws_instance" "this" {
dynamic "capacity_reservation_target" {
for_each = lookup(capacity_reservation_specification.value, "capacity_reservation_target", [])
content {
capacity_reservation_id = lookup(capacity_reservation_target, "capacity_reservation_id", null)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


capacity_reservation_specification = {
capacity_reservation_target = {
capacity_reservation_id = aws_ec2_capacity_reservation.targeted.id
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

…ication of the capacity reservation attribute assignment
@schniber
Copy link
Contributor Author

Hello @bryantbiggs

Thanks for your valuable feedback that I have taken into account.

I have added the example for the targeted capacity reservation together with the simplification of the capacity reservation assignment. it's more readable with try function.

I have also deployed to test and well went through.

Looking forward for the merge if all is okay for you.

Thanks a ton !

Bests.

@schniber
Copy link
Contributor Author

schniber commented Aug 1, 2022

Hello @bryantbiggs

Would it be possible to review the changes I made upon your feedback ?

Thanks again.

Bests.

Copy link
Member

@bryantbiggs bryantbiggs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@bryantbiggs bryantbiggs merged commit 7f0a0ae into terraform-aws-modules:master Aug 10, 2022
antonbabenko pushed a commit that referenced this pull request Aug 10, 2022
### [4.1.2](v4.1.1...v4.1.2) (2022-08-10)

### Bug Fixes

* Assignment of the Capacity Reservation id to an instance ([#282](#282)) ([7f0a0ae](7f0a0ae))
@antonbabenko
Copy link
Member

This PR is included in version 4.1.2 🎉

@yewton
Copy link

yewton commented Aug 11, 2022

Should the default value of capacity_reservation_specification be updated from null to {} ?

@bryantbiggs
Copy link
Member

@yewton good spot - yes, looks like we need to update the variable as well. PRs are welcomed!

@schniber
Copy link
Contributor Author

I will create a new PR to fix it.
Thanks for spotting it !

@yewton
Copy link

yewton commented Aug 12, 2022

Thank you!

@github-actions
Copy link

github-actions bot commented Nov 8, 2022

I'm going to lock this pull request 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 related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 8, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Creation of an Instance with a Capacity Reservation ID
4 participants