terraform-aws-modules / terraform-aws-eks

Terraform module to create Amazon Elastic Kubernetes (EKS) resources 🇺🇦
https://registry.terraform.io/modules/terraform-aws-modules/eks/aws
Apache License 2.0
4.45k stars 4.06k forks source link

Create nodegroups conditionally #1668

Closed YakobovLior closed 2 years ago

YakobovLior commented 2 years ago

Versions

Description

I'm looking for the ability to configure multiple nodegroups, but actually create them conditionally based on variables. Here is a snippet from my configuration:

module "eks" {
  source                          = "terraform-aws-modules/eks/aws"
  cluster_name                    = var.cluster_name
  cluster_version                 = var.eks_version
  cluster_endpoint_private_access = true
  subnets                         = var.new_vpc == "true" ? module.vpc.private_subnets : data.aws_subnet_ids.private[0].ids
  ...
  ...
  ...
  node_groups = [
    {
      count                   = var.cluster_size == "small" ? 1 : 0
      name                    = "${var.cluster_name}-main-nodegroup"
      launch_template_id      = aws_launch_template.default.id
      launch_template_version = aws_launch_template.default.default_version
      instance_types          = [var.instance_type]

      additional_tags = {
        "Name" = var.cluster_name
      }
      desired_capacity = var.desired_capacity
      max_capacity     = var.max_capacity
      min_capacity     = var.min_capacity
      subnets          = [var.new_vpc == "true" ? module.vpc.private_subnets[0] : tolist(data.aws_subnet_ids.private[0].ids)[0]]
      key_name         = aws_key_pair.generated_key.key_name
...
...
...

node_groups list includes more than one nodegroup of course, and I want to manage the creation of the different nodegroups based on variables. Seems that count as part of the nodegroup configuration is not supported. Is there any other way that I can achieve my goal?

Thanks, Lior

antonbabenko commented 2 years ago

Hi Lior,

Are you using Terragrunt with this module or plain Terraform?

It seems that your question is about the general usage of Terraform, specifically for expressions should give you some information about how to generate lists or maps.

Please ask general Terraform questions on discuss.hashicorp.com.

YakobovLior commented 2 years ago

Hey @antonbabenko, I am using Terragrunt alongside with Terraform, does it matter in some way?

The question is does this module supports the desired behavior I mentioned above.

Thanks, Lior

antonbabenko commented 2 years ago

Yes, the module supports multiple node_groups. See this example.

jaimehrubiks commented 2 years ago

You can have two variables, the temporary one with a boolean, and the final one as a for if loop on the first one that only copies the element when the special boolean is true

YakobovLior commented 2 years ago

Hey @antonbabenko , @jaimehrubiks I managed to solve my issue with the use of locals, I will describe here as it might help someone in the future.

I have defined 3 locals, whereas every local has different values based on the conditions I have, and then a fourth local which equals to one of the previous 3 based on the conditions. And eventually I use the fourth local as the value for node_groups in the module.

Thanks for the help, Lior

github-actions[bot] commented 1 year ago

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.