From a61db1558fb203fc318d77ee96c750b6b1f43444 Mon Sep 17 00:00:00 2001 From: Joakim Olsson Date: Fri, 1 Dec 2023 10:41:24 +0100 Subject: [PATCH] feat: fetch latest focal image if not specified --- ami.tf | 25 +++++++++++++++++++++++++ k8s.tf | 6 +++--- 2 files changed, 28 insertions(+), 3 deletions(-) create mode 100644 ami.tf diff --git a/ami.tf b/ami.tf new file mode 100644 index 0000000..17c75e4 --- /dev/null +++ b/ami.tf @@ -0,0 +1,25 @@ +data "aws_ami" "default_image" { + most_recent = true + name_regex = "^ubuntu/.*focal-20.04-amd64-server-\\d+(\\.\\d+)?$" + owners = ["099720109477"] + + filter { + name = "name" + values = ["ubuntu/*focal*"] + } + + filter { + name = "architecture" + values = ["x86_64"] + } + + filter { + name = "root-device-type" + values = ["ebs"] + } + + filter { + name = "virtualization-type" + values = ["hvm"] + } +} diff --git a/k8s.tf b/k8s.tf index feeb8a0..7693f49 100644 --- a/k8s.tf +++ b/k8s.tf @@ -237,7 +237,7 @@ resource "kops_instance_group" "masters" { cluster_name = kops_cluster.k8s.id name = "${var.control_plane_prefix}-${var.region}${each.key}" role = "ControlPlane" - image = var.master_image != null ? var.master_image : var.image + image = coalesce(var.master_image, var.image, "${data.aws_ami.default_image.owner_id}/${data.aws_ami.default_image.name}") min_size = 1 max_size = 1 machine_type = var.master_types[0] @@ -272,7 +272,7 @@ resource "kops_instance_group" "nodes" { cluster_name = kops_cluster.k8s.id name = "nodes-${each.key}" role = "Node" - image = var.node_image != null ? var.node_image : var.image + image = coalesce(var.node_image, var.image, "${data.aws_ami.default_image.owner_id}/${data.aws_ami.default_image.name}") min_size = lookup(local.min_nodes, each.key) max_size = lookup(local.max_nodes, each.key) machine_type = var.node_types[0] @@ -311,7 +311,7 @@ resource "kops_instance_group" "additional_nodes" { cluster_name = kops_cluster.k8s.id name = "nodes-${each.key}" role = "Node" - image = each.value.image != null ? each.value.image : var.image + image = coalesce(each.value.image, var.image, "${data.aws_ami.default_image.owner_id}/${data.aws_ami.default_image.name}") min_size = each.value.min_size max_size = each.value.max_size machine_type = each.value.types[0]