Skip to content

Commit

Permalink
Pass in custom metadata options to the eks launch template
Browse files Browse the repository at this point in the history
  • Loading branch information
OlamideOl1 committed Sep 25, 2024
1 parent a094ef5 commit d35aca0
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 16 deletions.
27 changes: 14 additions & 13 deletions aws/cluster/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,20 @@ module "node_groups" {
for_each = var.node_groups
source = "./modules/eks-node-group"

capacity_type = each.value.capacity_type
cluster = module.eks_cluster.instance
instance_types = each.value.instance_types
labels = var.labels
max_size = each.value.max_size
max_unavailable = each.value.max_unavailable
min_size = each.value.min_size
name = each.key
namespace = [module.cluster_name.full]
role = module.node_role.instance
subnets = values(data.aws_subnet.private)
tags = var.tags
user_data = var.user_data
capacity_type = each.value.capacity_type
cluster = module.eks_cluster.instance
instance_types = each.value.instance_types
labels = var.labels
max_size = each.value.max_size
max_unavailable = each.value.max_unavailable
metadata_options = var.metadata_options
min_size = each.value.min_size
name = each.key
namespace = [module.cluster_name.full]
role = module.node_role.instance
subnets = values(data.aws_subnet.private)
tags = var.tags
user_data = lookup(var.user_data, each.key, null)

depends_on = [module.node_role]
}
Expand Down
20 changes: 17 additions & 3 deletions aws/cluster/modules/eks-node-group/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ resource "aws_eks_node_group" "this" {
subnet_ids = [each.value.id]

dynamic "launch_template" {
for_each = var.user_data != null ? [aws_launch_template.this[0]] : []
for_each = var.metadata_options != {} ? [aws_launch_template.this[0]] : []

content {
id = launch_template.value.id
Expand Down Expand Up @@ -40,10 +40,22 @@ resource "aws_eks_node_group" "this" {
}
}

# resource "aws_launch_template" "this" {
# count = var.user_data != null ? 1 : 0

# user_data = base64encode(var.user_data)
# }

resource "aws_launch_template" "this" {
count = var.user_data != null ? 1 : 0
count = var.metadata_options != {} ? 1 : 0

user_data = base64encode(var.user_data)
metadata_options {
http_endpoint = lookup(var.metadata_options, "http_endpoint", "enabled")
http_tokens = lookup(var.metadata_options, "http_tokens", "optional")
http_put_response_hop_limit = lookup(var.metadata_options, "htthttp_put_response_hop_limit", "1")
http_protocol_ipv6 = lookup(var.metadata_options, "http_protocol_ipv6", "disabled")
instance_metadata_tags = lookup(var.metadata_options, "instance_metadata_tags", "disabled")
}
}

locals {
Expand All @@ -54,4 +66,6 @@ locals {
var.subnets[*].availability_zone,
var.subnets
)


}
6 changes: 6 additions & 0 deletions aws/cluster/modules/eks-node-group/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,9 @@ variable "user_data" {
description = "Optional user data script for the launch template"
default = null # Default to an empty string if no user data is provided
}

variable "metadata_options" {
type = map(string)
description = "Customize the metadata options for the cluster nodes"
default = {}
}
6 changes: 6 additions & 0 deletions aws/cluster/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,9 @@ variable "user_data" {
description = "Optional user data script for the launch template"
default = {} # Default to an empty string if no user data is provided
}

variable "metadata_options" {
type = map(string)
description = "Customize the metadata options for the cluster nodes"
default = {}
}

0 comments on commit d35aca0

Please sign in to comment.