From 03b0c87be791538d1ec2f355c8ae2902c3f8523a Mon Sep 17 00:00:00 2001 From: Victor Jimenez Date: Wed, 11 Oct 2023 14:38:18 +0200 Subject: [PATCH] Add support to provide an external AMI id (#13) Co-authored-by: Victor Jimenez --- .gitignore | 2 + examples/basic/.terraform.lock.hcl | 80 ------------------------------ examples/basic/cluster.tf | 30 ++++++++++- launch-template.tf | 8 ++- test/basic_test.go | 2 +- variables.tf | 7 +++ 6 files changed, 44 insertions(+), 85 deletions(-) delete mode 100644 examples/basic/.terraform.lock.hcl diff --git a/.gitignore b/.gitignore index 6bb45e3..2b68aff 100644 --- a/.gitignore +++ b/.gitignore @@ -129,6 +129,8 @@ override.tf.json .terraformrc terraform.rc +.terraform.lock.hcl + ### Vim ### # Swap [._]*.s[a-v][a-z] diff --git a/examples/basic/.terraform.lock.hcl b/examples/basic/.terraform.lock.hcl deleted file mode 100644 index 8aa0a01..0000000 --- a/examples/basic/.terraform.lock.hcl +++ /dev/null @@ -1,80 +0,0 @@ -# This file is maintained automatically by "terraform init". -# Manual edits may be lost in future updates. - -provider "registry.terraform.io/hashicorp/aws" { - version = "4.34.0" - hashes = [ - "h1:JRqeU/5qR61U+z86mC68C5hp0XHZXxmRK9dupTIAhGg=", - "zh:2bdc9b908008c1e874d8ba7e2cfabd856cafb63c52fef51a1fdeef2f5584bffd", - "zh:43c5364e3161be3856e56494cbb8b21d513fc05875f1b40e66e583602154dd0a", - "zh:44e763adae92489f223f65866c1f8b5342e7e85b95daa8d1f483a2afb47f7db3", - "zh:62bfabb3a1a31814cb3fadc356539d8253b95abacfffd90984affb54c9a53a86", - "zh:6495ce67897d2d5648d466c09e8588e837c2878322988738a95c06926044b05d", - "zh:9b12af85486a96aedd8d7984b0ff811a4b42e3d88dad1a3fb4c0b580d04fa425", - "zh:b1546b2ac61d7cc27a8eba160ae1b6ac581d2c4af824a400d6511e4998da398a", - "zh:c8c362c5527f0533bde581e41cdb2bdf42aea557762f326dbfa122fdf001fb10", - "zh:c8cc28fb41f73ca09f590bace2204ea325f6116be0bbce6abfebd393d028f12c", - "zh:db0601c9bd12ca028d60ac87e85320285ebc64857715f7908dd6a283e5f44d45", - "zh:e64d2193236d05348ba2e8b99650d9274e5af80be39b3ee28bbe442b0684d8a3", - "zh:ff6228f3751e1f0ee7dc086d09e32d39ca6556f0b5267f36aae67881d29ace94", - ] -} - -provider "registry.terraform.io/hashicorp/helm" { - version = "2.7.1" - constraints = ">= 2.0.0" - hashes = [ - "h1:OGZRkgiLBWmoA8/a9xZnEs5gsC5JhW+75++MkCPQbqw=", - "zh:13e2467092deeff01c4cfa2b54ba4510aa7a9b06c58f22c4215b0f4333858364", - "zh:4549843db4fdf5d8150e8c0734e67b54b5c3bcfc914e3221e6952f428fb984d2", - "zh:55b5f83ed52f93dd00a73c33c948326052efd700350c19e63bb1679b12bfcda6", - "zh:749397e41393289eb0ef6efd0a75911d29b8aa7f48e5d6813b4b350dad91acbd", - "zh:7a4a2c95b055f6c8e70d1fc7a4cc4fd6e4f04845be36e40d42d31dfc13db37b8", - "zh:8143e5b8218857052505c805b570889b862c618ce6cbfbddb98938ff7a5901d3", - "zh:856d94b3b34d6204d66c6de4feab4737c74dba037ad64e4c613e8eec61d17f1a", - "zh:b9b037f1edda209022df1c7fc906786970524873e27b061f3355cb9bbed2cf08", - "zh:c433b27f52a0600490af07f8b217ab0b1048ba347d68e6fe478aba18634e78d9", - "zh:da133748368c6e27b433cd7faeb7b800536c8651e7af0415452901dfc7577dbf", - "zh:eecc63c2dec8aafa2ffd7426800c3e1a5e31e848be01ea9511ad0184dce15945", - "zh:f569b65999264a9416862bca5cd2a6177d94ccb0424f3a4ef424428912b9cb3c", - ] -} - -provider "registry.terraform.io/hashicorp/kubernetes" { - version = "2.14.0" - constraints = ">= 2.0.0" - hashes = [ - "h1:4zSUEWLVFn2Sji7mWT64XQGWwBQVDqTGXGfW4ZBB16U=", - "zh:1363fcd6eb3c63113eaa6947a4e7a9f78a6974ea344e89b662d97a78e2ccb70c", - "zh:166352455666b7d584705ceeb00f24fb9b884ab84e3a1a6019dc45d6539c9174", - "zh:4615249ce5311f6fbea9738b25b6e6159e7dcf4693b0a24bc6a5720d1bfd38d0", - "zh:5205343f8e6cfa89d2f9a312edddcf263755bc294a5216555c390244df826f17", - "zh:60b7d9b5da2d1a13bc9cdfe5be75da2e3d1034617dff51ef3f0beb72fe801879", - "zh:61b73d78ef03f0b38ff567b78f2984089eb17724fd8d0f92943b7e522cf31e39", - "zh:69dfe1278eecc6049736d74c3fa2d1f384035621ec5d72f8b180e3b25b45b592", - "zh:7746656be1b437e43f7324898cd4548d7e8cad5308042ba38cb45c4fecbf38fe", - "zh:7e573462091aaf2e6a37edeee33ee4d8f4c37f9a35c331e0f3a60caf078c88c1", - "zh:a05e1f02b2385679087a7059944cac7fb1d71dd042601ee4d0d26e9808d14dd5", - "zh:d8d5d52af1aa55160fec601a1006552d9b6fe21e97313850a1e79bc026e99cfe", - "zh:f569b65999264a9416862bca5cd2a6177d94ccb0424f3a4ef424428912b9cb3c", - ] -} - -provider "registry.terraform.io/hashicorp/null" { - version = "3.1.1" - hashes = [ - "h1:Pctug/s/2Hg5FJqjYcTM0kPyx3AoYK1MpRWO0T9V2ns=", - "zh:063466f41f1d9fd0dd93722840c1314f046d8760b1812fa67c34de0afcba5597", - "zh:08c058e367de6debdad35fc24d97131c7cf75103baec8279aba3506a08b53faf", - "zh:73ce6dff935150d6ddc6ac4a10071e02647d10175c173cfe5dca81f3d13d8afe", - "zh:78d5eefdd9e494defcb3c68d282b8f96630502cac21d1ea161f53cfe9bb483b3", - "zh:8fdd792a626413502e68c195f2097352bdc6a0df694f7df350ed784741eb587e", - "zh:976bbaf268cb497400fd5b3c774d218f3933271864345f18deebe4dcbfcd6afa", - "zh:b21b78ca581f98f4cdb7a366b03ae9db23a73dfa7df12c533d7c19b68e9e72e5", - "zh:b7fc0c1615dbdb1d6fd4abb9c7dc7da286631f7ca2299fb9cd4664258ccfbff4", - "zh:d1efc942b2c44345e0c29bc976594cb7278c38cfb8897b344669eafbc3cddf46", - "zh:e356c245b3cd9d4789bab010893566acace682d7db877e52d40fc4ca34a50924", - "zh:ea98802ba92fcfa8cf12cbce2e9e7ebe999afbf8ed47fa45fc847a098d89468b", - "zh:eff8872458806499889f6927b5d954560f3d74bf20b6043409edf94d26cd906f", - ] -} diff --git a/examples/basic/cluster.tf b/examples/basic/cluster.tf index 3c5970f..3747724 100644 --- a/examples/basic/cluster.tf +++ b/examples/basic/cluster.tf @@ -1,11 +1,11 @@ locals { cluster_name = "simple-eks-integration-test-for-eks-node-group" - cluster_version = "1.23" + cluster_version = "1.28" } module "cluster" { source = "vistaprint/simple-eks/aws" - version = "0.4.0-rc1" + version = "0.4.0" cluster_name = local.cluster_name cluster_version = local.cluster_version @@ -64,3 +64,29 @@ module "ebs_encrypted_node_group" { depends_on = [module.cluster] } + +module "custom_ami_node_group" { + source = "../.." + + cluster_name = local.cluster_name + node_group_version = local.cluster_version + node_group_name = "custom-ami" + + instance_types = ["t3a.small"] + + image_id = "ami-0d028bd9a57562372" + + scaling_config = { + desired_size = 1 + max_size = 1 + min_size = 1 + } + + worker_role_arn = module.cluster.worker_role_arn + subnet_ids = module.cluster.private_subnet_ids + + region = var.aws_region + profile = var.aws_profile + + depends_on = [module.cluster] +} diff --git a/launch-template.tf b/launch-template.tf index 86e982f..715ad92 100644 --- a/launch-template.tf +++ b/launch-template.tf @@ -4,8 +4,10 @@ locals { x86_64_ami = "amazon-eks-node-${var.node_group_version}-v*" arm64_ami = "amazon-eks-arm64-node-${var.node_group_version}-v*" + ami_id = var.image_id != null ? var.image_id : data.aws_ami.ami.0.id + node_labels = "--node-labels=${join(",", [ - "eks.amazonaws.com/nodegroup-image=${data.aws_ami.ami.id}", + "eks.amazonaws.com/nodegroup-image=${local.ami_id}", "eks.amazonaws.com/capacityType=${local.capacity_type}", "eks.amazonaws.com/nodegroup=${var.node_group_name}" ])}" @@ -29,6 +31,8 @@ locals { } data "aws_ami" "ami" { + count = var.image_id == null ? 1 : 0 + most_recent = true name_regex = var.architecture == "x86_64" ? local.x86_64_ami : local.arm64_ami owners = ["amazon"] @@ -39,7 +43,7 @@ resource "aws_launch_template" "worker_nodes" { name = "${var.cluster_name}-${var.node_group_name}" - image_id = data.aws_ami.ami.id + image_id = local.ami_id block_device_mappings { device_name = "/dev/xvda" diff --git a/test/basic_test.go b/test/basic_test.go index c0622bd..0c04af2 100644 --- a/test/basic_test.go +++ b/test/basic_test.go @@ -43,8 +43,8 @@ func TestTerraformBasicExample(t *testing.T) { terraform.InitAndApply(t, terraformOptions) checkNodeGroupExists(t, "basic") - checkNodeGroupExists(t, "calico") checkNodeGroupExists(t, "encrypt-ebs") + checkNodeGroupExists(t, "custom-ami") } func checkNodeGroupExists(t *testing.T, nodeGroupName string) { diff --git a/variables.tf b/variables.tf index 6b12d5b..79eeb66 100644 --- a/variables.tf +++ b/variables.tf @@ -51,6 +51,13 @@ variable "architecture" { } } +variable "image_id" { + type = string + default = null + + description = "AMI id to use for the node group" +} + variable "worker_role_arn" { type = string }