Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion local.tf
Original file line number Diff line number Diff line change
Expand Up @@ -134,5 +134,6 @@ locals {
"t2.xlarge"
]

node_groups = { for node_group in var.node_groups : obj.name => obj }
node_groups = { for node_group in var.node_groups : node_group["name"] => node_group }

}
20 changes: 8 additions & 12 deletions node_groups.tf
Original file line number Diff line number Diff line change
Expand Up @@ -48,30 +48,26 @@ resource "aws_iam_policy" "node_groups_autoscaling" {
}

resource "random_pet" "node_groups" {
count = local.worker_group_managed_node_group_count
for_each = local.node_groups

separator = "-"
length = 2

keepers = {
ec2_ssh_key = lookup(
var.node_groups[count.index],
"key_name",
local.workers_group_defaults["key_name"],
)
instance_type = lookup(each.value, "instance_type", local.workers_group_defaults["instance_type"])

source_security_group_ids = join("-", compact(lookup(
var.node_groups[count.index],
"source_security_group_ids",
local.workers_group_defaults["source_security_group_id"],
ec2_ssh_key = lookup(each.value, "key_name", local.workers_group_defaults["key_name"])

source_security_group_ids = join("-", compact(
lookup(each.value, "source_security_group_ids", local.workers_group_defaults["source_security_group_id"]
)))

node_group_name = join(
"-",
compact(
[
aws_eks_cluster.this.name,
lookup(var.node_groups[count.index], "name", count.index),
each.value["name"],
]
)
)
Expand All @@ -92,7 +88,7 @@ resource "aws_eks_node_group" "workers" {
[
aws_eks_cluster.this.name,
lookup(var.node_groups[count.index], "name", count.index),
random_pet.node_groups[count.index].id
random_pet.node_groups[lookup(var.node_groups[count.index], "name", count.index)]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello, I'm getting this error:

Error: Invalid function argument

  on .terraform/modules/eks/node_groups.tf line 88, in resource "aws_eks_node_group" "workers":
  87: 
  88:       [
  89: 
  90: 
  91: 
  92: 
  93: 
    |----------------
    | aws_eks_cluster.this.name is "toro-dev2"
    | count.index is 0
    | random_pet.node_groups is object with 1 attribute "standard"
    | var.node_groups is tuple with 1 element

Invalid value for "list" parameter: element 2: string required.

looks like .id is missing here:

random_pet.node_groups[lookup(var.node_groups[count.index], "name", count.index)].id

tks!

Copy link
Contributor

@eytanhanig eytanhanig Nov 29, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try this:

resource "random_pet" "node_groups" {
  for_each = local.node_groups

  separator = "-"
  length    = 2

  keepers = {
    instance_type = lookup(each.value, "instance_type", local.workers_group_defaults["instance_type"])

    ec2_ssh_key = lookup(each.value, "key_name", local.workers_group_defaults["key_name"])

    source_security_group_ids = join("-", compact(
      lookup(each.value, "source_security_group_ids", local.workers_group_defaults["source_security_group_id"]
    )))

    node_group_name = join("-", [var.cluster_name, each.value["name"]])
  }
}

resource "aws_eks_node_group" "workers" {
  for_each = local.node_groups

  node_group_name = join("-", [var.cluster_name, each.key, random_pet.node_groups[each.key].id])

  cluster_name    = var.cluster_name
  node_role_arn   = lookup(each.value, "iam_role_arn", aws_iam_role.node_groups[0].arn)
  subnet_ids      = lookup(each.value, "subnets", local.workers_group_defaults["subnets"])

  scaling_config {
    desired_size = lookup(each.value, "node_group_desired_capacity", local.workers_group_defaults["asg_desired_capacity"])
    max_size     = lookup(each.value, "node_group_max_capacity", local.workers_group_defaults["asg_max_size"])
    min_size     = lookup(each.value, "node_group_min_capacity", local.workers_group_defaults["asg_min_size"])
  }

  ami_type        = lookup(each.value, "ami_type", null)
  disk_size       = lookup(each.value, "root_volume_size", null)
  instance_types  = [lookup(each.value, "instance_type", null)]
  labels          = lookup(each.value, "node_group_k8s_labels", null)
  release_version = lookup(each.value, "ami_release_version", null)

  # This sometimes breaks idempotency as described in https://github.com/terraform-providers/terraform-provider-aws/issues/11063
  remote_access {
    ec2_ssh_key               = lookup(each.value, "key_name", "") != "" ? each.value["key_name"] : null
    source_security_group_ids = lookup(each.value, "key_name", "") != "" ? lookup(each.value, "source_security_group_ids", []) : null
  }

  version = aws_eks_cluster.this.version

  tags = lookup(each.value, "node_group_additional_tags", null)

  lifecycle {
    create_before_destroy = true
  }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eytanhanig I added the changes you suggested and the terraform plan seems to work.

]
)
)
Expand Down