From 42c2eb59a0466ce6d137f8d386dff87c4ee9a792 Mon Sep 17 00:00:00 2001 From: Bryant Biggs Date: Sat, 2 Apr 2022 11:49:20 -0400 Subject: [PATCH] fix: Correct `capacity_reservation_target` within launch templates of both EKS and self managed node groups --- .pre-commit-config.yaml | 2 +- examples/self_managed_node_group/README.md | 1 + examples/self_managed_node_group/main.tf | 14 ++++++++++++++ modules/eks-managed-node-group/main.tf | 2 +- modules/self-managed-node-group/main.tf | 2 +- 5 files changed, 18 insertions(+), 3 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 8a010fdd41..f00fbfe8aa 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,6 +1,6 @@ repos: - repo: https://github.com/antonbabenko/pre-commit-terraform - rev: v1.64.0 + rev: v1.64.1 hooks: - id: terraform_fmt - id: terraform_validate diff --git a/examples/self_managed_node_group/README.md b/examples/self_managed_node_group/README.md index f91bbf6b4c..3798d8e17e 100644 --- a/examples/self_managed_node_group/README.md +++ b/examples/self_managed_node_group/README.md @@ -49,6 +49,7 @@ Note that this example may create resources which cost money. Run `terraform des | Name | Type | |------|------| +| [aws_ec2_capacity_reservation.targeted](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ec2_capacity_reservation) | resource | | [aws_key_pair.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/key_pair) | resource | | [aws_kms_key.ebs](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kms_key) | resource | | [aws_kms_key.eks](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kms_key) | resource | diff --git a/examples/self_managed_node_group/main.tf b/examples/self_managed_node_group/main.tf index 43da796503..5ba1f7f0ad 100644 --- a/examples/self_managed_node_group/main.tf +++ b/examples/self_managed_node_group/main.tf @@ -202,6 +202,12 @@ module "eks" { instance_metadata_tags = "disabled" } + capacity_reservation_specification = { + capacity_reservation_target = { + capacity_reservation_id = aws_ec2_capacity_reservation.targeted.id + } + } + create_iam_role = true iam_role_name = "self-managed-node-group-complete-example" iam_role_use_name_prefix = false @@ -407,6 +413,14 @@ resource "aws_kms_key" "ebs" { policy = data.aws_iam_policy_document.ebs.json } +resource "aws_ec2_capacity_reservation" "targeted" { + instance_type = "m6i.large" + instance_platform = "Linux/UNIX" + availability_zone = "${local.region}a" + instance_count = 1 + instance_match_criteria = "targeted" +} + # This policy is required for the KMS key used for EKS root volumes, so the cluster is allowed to enc/dec/attach encrypted EBS volumes data "aws_iam_policy_document" "ebs" { # Copy of default KMS policy that lets you manage it diff --git a/modules/eks-managed-node-group/main.tf b/modules/eks-managed-node-group/main.tf index 662b2aa258..90b300b906 100644 --- a/modules/eks-managed-node-group/main.tf +++ b/modules/eks-managed-node-group/main.tf @@ -94,7 +94,7 @@ resource "aws_launch_template" "this" { capacity_reservation_preference = lookup(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) } diff --git a/modules/self-managed-node-group/main.tf b/modules/self-managed-node-group/main.tf index b09b601818..e625582cd7 100644 --- a/modules/self-managed-node-group/main.tf +++ b/modules/self-managed-node-group/main.tf @@ -97,7 +97,7 @@ resource "aws_launch_template" "this" { capacity_reservation_preference = lookup(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) }