From b47723b31419ec916876709500d054ffc6050003 Mon Sep 17 00:00:00 2001 From: Samuel CHNIBER Date: Sat, 23 Jul 2022 14:31:02 +0200 Subject: [PATCH 1/2] fix of the assignment of the capacity reservation id --- examples/complete/README.md | 2 +- examples/complete/main.tf | 14 +++++++------- main.tf | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/examples/complete/README.md b/examples/complete/README.md index 008f8307..40bc6e30 100644 --- a/examples/complete/README.md +++ b/examples/complete/README.md @@ -48,7 +48,7 @@ Note that this example may create resources which can cost money. Run `terraform | Name | Type | |------|------| -| [aws_ec2_capacity_reservation.targeted](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ec2_capacity_reservation) | resource | +| [aws_ec2_capacity_reservation.open](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ec2_capacity_reservation) | resource | | [aws_kms_key.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kms_key) | resource | | [aws_network_interface.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/network_interface) | resource | | [aws_placement_group.web](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/placement_group) | resource | diff --git a/examples/complete/main.tf b/examples/complete/main.tf index c2cd951a..d37d98c4 100644 --- a/examples/complete/main.tf +++ b/examples/complete/main.tf @@ -73,17 +73,17 @@ resource "aws_network_interface" "this" { subnet_id = element(module.vpc.private_subnets, 0) } -resource "aws_ec2_capacity_reservation" "targeted" { +resource "aws_ec2_capacity_reservation" "open" { instance_type = "t3.micro" instance_platform = "Linux/UNIX" availability_zone = "${local.region}a" instance_count = 1 - instance_match_criteria = "targeted" + instance_match_criteria = "open" } -# ################################################################################ -# # EC2 Module -# ################################################################################ +# # ################################################################################ +# # # EC2 Module +# # ################################################################################ module "ec2_disabled" { source = "../../" @@ -350,11 +350,11 @@ module "ec2_capacity_reservation" { instance_type = "t3.micro" subnet_id = element(module.vpc.private_subnets, 0) vpc_security_group_ids = [module.security_group.security_group_id] - associate_public_ip_address = true + associate_public_ip_address = false capacity_reservation_specification = { capacity_reservation_target = { - capacity_reservation_id = aws_ec2_capacity_reservation.targeted.id + capacity_reservation_id = aws_ec2_capacity_reservation.open.id } } diff --git a/main.tf b/main.tf index 5d0ec624..864912cd 100644 --- a/main.tf +++ b/main.tf @@ -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) + capacity_reservation_id = lookup(capacity_reservation_specification.value.capacity_reservation_target, "capacity_reservation_id", null) } } } From e0187434be3f06147294dde7c770d470c56dfeaa Mon Sep 17 00:00:00 2001 From: Samuel CHNIBER Date: Thu, 28 Jul 2022 09:45:17 +0200 Subject: [PATCH 2/2] addition of targeted capacity reservation in the examples and simplification of the capacity reservation attribute assignment --- examples/complete/README.md | 4 +++- examples/complete/main.tf | 32 ++++++++++++++++++++++++++++++-- main.tf | 9 +++++---- 3 files changed, 38 insertions(+), 7 deletions(-) diff --git a/examples/complete/README.md b/examples/complete/README.md index 40bc6e30..327f765f 100644 --- a/examples/complete/README.md +++ b/examples/complete/README.md @@ -32,15 +32,16 @@ Note that this example may create resources which can cost money. Run `terraform | Name | Source | Version | |------|--------|---------| -| [ec2\_capacity\_reservation](#module\_ec2\_capacity\_reservation) | ../../ | n/a | | [ec2\_complete](#module\_ec2\_complete) | ../../ | n/a | | [ec2\_disabled](#module\_ec2\_disabled) | ../../ | n/a | | [ec2\_metadata\_options](#module\_ec2\_metadata\_options) | ../../ | n/a | | [ec2\_multiple](#module\_ec2\_multiple) | ../../ | n/a | | [ec2\_network\_interface](#module\_ec2\_network\_interface) | ../../ | n/a | +| [ec2\_open\_capacity\_reservation](#module\_ec2\_open\_capacity\_reservation) | ../../ | n/a | | [ec2\_spot\_instance](#module\_ec2\_spot\_instance) | ../../ | n/a | | [ec2\_t2\_unlimited](#module\_ec2\_t2\_unlimited) | ../../ | n/a | | [ec2\_t3\_unlimited](#module\_ec2\_t3\_unlimited) | ../../ | n/a | +| [ec2\_targeted\_capacity\_reservation](#module\_ec2\_targeted\_capacity\_reservation) | ../../ | n/a | | [security\_group](#module\_security\_group) | terraform-aws-modules/security-group/aws | ~> 4.0 | | [vpc](#module\_vpc) | terraform-aws-modules/vpc/aws | ~> 3.0 | @@ -49,6 +50,7 @@ Note that this example may create resources which can cost money. Run `terraform | Name | Type | |------|------| | [aws_ec2_capacity_reservation.open](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ec2_capacity_reservation) | resource | +| [aws_ec2_capacity_reservation.targeted](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ec2_capacity_reservation) | resource | | [aws_kms_key.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kms_key) | resource | | [aws_network_interface.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/network_interface) | resource | | [aws_placement_group.web](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/placement_group) | resource | diff --git a/examples/complete/main.tf b/examples/complete/main.tf index d37d98c4..338b45c1 100644 --- a/examples/complete/main.tf +++ b/examples/complete/main.tf @@ -81,6 +81,14 @@ resource "aws_ec2_capacity_reservation" "open" { instance_match_criteria = "open" } +resource "aws_ec2_capacity_reservation" "targeted" { + instance_type = "t3.micro" + instance_platform = "Linux/UNIX" + availability_zone = "${local.region}a" + instance_count = 1 + instance_match_criteria = "targeted" +} + # # ################################################################################ # # # EC2 Module # # ################################################################################ @@ -341,10 +349,10 @@ module "ec2_spot_instance" { # EC2 Module - Capacity Reservation ################################################################################ -module "ec2_capacity_reservation" { +module "ec2_open_capacity_reservation" { source = "../../" - name = "${local.name}-capacity-reservation" + name = "${local.name}-open-capacity-reservation" ami = data.aws_ami.amazon_linux.id instance_type = "t3.micro" @@ -360,3 +368,23 @@ module "ec2_capacity_reservation" { tags = local.tags } + +module "ec2_targeted_capacity_reservation" { + source = "../../" + + name = "${local.name}-targeted-capacity-reservation" + + ami = data.aws_ami.amazon_linux.id + instance_type = "t3.micro" + subnet_id = element(module.vpc.private_subnets, 0) + vpc_security_group_ids = [module.security_group.security_group_id] + associate_public_ip_address = false + + capacity_reservation_specification = { + capacity_reservation_target = { + capacity_reservation_id = aws_ec2_capacity_reservation.targeted.id + } + } + + tags = local.tags +} diff --git a/main.tf b/main.tf index 864912cd..5f244b8e 100644 --- a/main.tf +++ b/main.tf @@ -182,14 +182,15 @@ resource "aws_spot_instance_request" "this" { # End spot request specific attributes dynamic "capacity_reservation_specification" { - for_each = var.capacity_reservation_specification != null ? [var.capacity_reservation_specification] : [] + for_each = length(var.capacity_reservation_specification) > 0 ? [var.capacity_reservation_specification] : [] content { - capacity_reservation_preference = lookup(capacity_reservation_specification.value, "capacity_reservation_preference", null) + capacity_reservation_preference = try(capacity_reservation_specification.value.capacity_reservation_preference, null) dynamic "capacity_reservation_target" { - for_each = lookup(capacity_reservation_specification.value, "capacity_reservation_target", []) + for_each = try([capacity_reservation_specification.value.capacity_reservation_target], []) content { - capacity_reservation_id = lookup(capacity_reservation_target.value, "capacity_reservation_id", null) + capacity_reservation_id = try(capacity_reservation_target.value.capacity_reservation_id, null) + capacity_reservation_resource_group_arn = try(capacity_reservation_target.value.capacity_reservation_resource_group_arn, null) } } }