Skip to content

Commit 7f0a0ae

Browse files
authored
fix: Assignment of the Capacity Reservation id to an instance (#282)
Co-authored-by: Samuel CHNIBER <schniber@amazon.fr>
1 parent b3d6f51 commit 7f0a0ae

File tree

3 files changed

+43
-12
lines changed

3 files changed

+43
-12
lines changed

examples/complete/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,24 @@ Note that this example may create resources which can cost money. Run `terraform
3232

3333
| Name | Source | Version |
3434
|------|--------|---------|
35-
| <a name="module_ec2_capacity_reservation"></a> [ec2\_capacity\_reservation](#module\_ec2\_capacity\_reservation) | ../../ | n/a |
3635
| <a name="module_ec2_complete"></a> [ec2\_complete](#module\_ec2\_complete) | ../../ | n/a |
3736
| <a name="module_ec2_disabled"></a> [ec2\_disabled](#module\_ec2\_disabled) | ../../ | n/a |
3837
| <a name="module_ec2_metadata_options"></a> [ec2\_metadata\_options](#module\_ec2\_metadata\_options) | ../../ | n/a |
3938
| <a name="module_ec2_multiple"></a> [ec2\_multiple](#module\_ec2\_multiple) | ../../ | n/a |
4039
| <a name="module_ec2_network_interface"></a> [ec2\_network\_interface](#module\_ec2\_network\_interface) | ../../ | n/a |
40+
| <a name="module_ec2_open_capacity_reservation"></a> [ec2\_open\_capacity\_reservation](#module\_ec2\_open\_capacity\_reservation) | ../../ | n/a |
4141
| <a name="module_ec2_spot_instance"></a> [ec2\_spot\_instance](#module\_ec2\_spot\_instance) | ../../ | n/a |
4242
| <a name="module_ec2_t2_unlimited"></a> [ec2\_t2\_unlimited](#module\_ec2\_t2\_unlimited) | ../../ | n/a |
4343
| <a name="module_ec2_t3_unlimited"></a> [ec2\_t3\_unlimited](#module\_ec2\_t3\_unlimited) | ../../ | n/a |
44+
| <a name="module_ec2_targeted_capacity_reservation"></a> [ec2\_targeted\_capacity\_reservation](#module\_ec2\_targeted\_capacity\_reservation) | ../../ | n/a |
4445
| <a name="module_security_group"></a> [security\_group](#module\_security\_group) | terraform-aws-modules/security-group/aws | ~> 4.0 |
4546
| <a name="module_vpc"></a> [vpc](#module\_vpc) | terraform-aws-modules/vpc/aws | ~> 3.0 |
4647

4748
## Resources
4849

4950
| Name | Type |
5051
|------|------|
52+
| [aws_ec2_capacity_reservation.open](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ec2_capacity_reservation) | resource |
5153
| [aws_ec2_capacity_reservation.targeted](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ec2_capacity_reservation) | resource |
5254
| [aws_kms_key.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kms_key) | resource |
5355
| [aws_network_interface.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/network_interface) | resource |

examples/complete/main.tf

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ resource "aws_network_interface" "this" {
7373
subnet_id = element(module.vpc.private_subnets, 0)
7474
}
7575

76+
resource "aws_ec2_capacity_reservation" "open" {
77+
instance_type = "t3.micro"
78+
instance_platform = "Linux/UNIX"
79+
availability_zone = "${local.region}a"
80+
instance_count = 1
81+
instance_match_criteria = "open"
82+
}
83+
7684
resource "aws_ec2_capacity_reservation" "targeted" {
7785
instance_type = "t3.micro"
7886
instance_platform = "Linux/UNIX"
@@ -81,9 +89,9 @@ resource "aws_ec2_capacity_reservation" "targeted" {
8189
instance_match_criteria = "targeted"
8290
}
8391

84-
# ################################################################################
85-
# # EC2 Module
86-
# ################################################################################
92+
# # ################################################################################
93+
# # # EC2 Module
94+
# # ################################################################################
8795

8896
module "ec2_disabled" {
8997
source = "../../"
@@ -341,16 +349,36 @@ module "ec2_spot_instance" {
341349
# EC2 Module - Capacity Reservation
342350
################################################################################
343351

344-
module "ec2_capacity_reservation" {
352+
module "ec2_open_capacity_reservation" {
345353
source = "../../"
346354

347-
name = "${local.name}-capacity-reservation"
355+
name = "${local.name}-open-capacity-reservation"
348356

349357
ami = data.aws_ami.amazon_linux.id
350358
instance_type = "t3.micro"
351359
subnet_id = element(module.vpc.private_subnets, 0)
352360
vpc_security_group_ids = [module.security_group.security_group_id]
353-
associate_public_ip_address = true
361+
associate_public_ip_address = false
362+
363+
capacity_reservation_specification = {
364+
capacity_reservation_target = {
365+
capacity_reservation_id = aws_ec2_capacity_reservation.open.id
366+
}
367+
}
368+
369+
tags = local.tags
370+
}
371+
372+
module "ec2_targeted_capacity_reservation" {
373+
source = "../../"
374+
375+
name = "${local.name}-targeted-capacity-reservation"
376+
377+
ami = data.aws_ami.amazon_linux.id
378+
instance_type = "t3.micro"
379+
subnet_id = element(module.vpc.private_subnets, 0)
380+
vpc_security_group_ids = [module.security_group.security_group_id]
381+
associate_public_ip_address = false
354382

355383
capacity_reservation_specification = {
356384
capacity_reservation_target = {

main.tf

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ resource "aws_instance" "this" {
4242
dynamic "capacity_reservation_target" {
4343
for_each = lookup(capacity_reservation_specification.value, "capacity_reservation_target", [])
4444
content {
45-
capacity_reservation_id = lookup(capacity_reservation_target, "capacity_reservation_id", null)
45+
capacity_reservation_id = lookup(capacity_reservation_specification.value.capacity_reservation_target, "capacity_reservation_id", null)
4646
}
4747
}
4848
}
@@ -182,14 +182,15 @@ resource "aws_spot_instance_request" "this" {
182182
# End spot request specific attributes
183183

184184
dynamic "capacity_reservation_specification" {
185-
for_each = var.capacity_reservation_specification != null ? [var.capacity_reservation_specification] : []
185+
for_each = length(var.capacity_reservation_specification) > 0 ? [var.capacity_reservation_specification] : []
186186
content {
187-
capacity_reservation_preference = lookup(capacity_reservation_specification.value, "capacity_reservation_preference", null)
187+
capacity_reservation_preference = try(capacity_reservation_specification.value.capacity_reservation_preference, null)
188188

189189
dynamic "capacity_reservation_target" {
190-
for_each = lookup(capacity_reservation_specification.value, "capacity_reservation_target", [])
190+
for_each = try([capacity_reservation_specification.value.capacity_reservation_target], [])
191191
content {
192-
capacity_reservation_id = lookup(capacity_reservation_target.value, "capacity_reservation_id", null)
192+
capacity_reservation_id = try(capacity_reservation_target.value.capacity_reservation_id, null)
193+
capacity_reservation_resource_group_arn = try(capacity_reservation_target.value.capacity_reservation_resource_group_arn, null)
193194
}
194195
}
195196
}

0 commit comments

Comments
 (0)