Skip to content

NimTechnology

Trình bày các công nghệ CLOUD một cách dễ hiểu.

  • Kubernetes & Container
    • Docker
    • Kubernetes
      • Ingress
      • Pod
    • Helm Chart
    • OAuth2 Proxy
    • Isito-EnvoyFilter
    • Apache Kafka
      • Kafka
      • Kafka Connect
      • Lenses
    • Vault
    • Longhorn – Storage
    • VictoriaMetrics
    • MetalLB
    • Kong Gateway
  • CI/CD
    • ArgoCD
    • ArgoWorkflows
    • Argo Events
    • Spinnaker
    • Jenkins
    • Harbor
    • TeamCity
    • Git
      • Bitbucket
  • Coding
    • DevSecOps
    • Terraform
      • GCP – Google Cloud
      • AWS – Amazon Web Service
      • Azure Cloud
    • Golang
    • Laravel
    • Python
    • Jquery & JavaScript
    • Selenium
  • Log, Monitor & Tracing
    • DataDog
    • Prometheus
    • Grafana
    • ELK
      • Kibana
      • Logstash
  • BareMetal
    • NextCloud
  • Toggle search form

[Terraform] Use “IF/ELSE” through the Merge function in Terraform

Posted on August 9, 2023March 28, 2024 By nim No Comments on [Terraform] Use “IF/ELSE” through the Merge function in Terraform

Contents

Toggle
  • 1) “IF/ELSE” with the Merge function
    • Solution:
  • 2) “IF/ELSE” to insert values template terraform

1) “IF/ELSE” with the Merge function

From the code snippet you’ve provided, it seems like you’re working with Terraform to create an AWS EKS (Elastic Kubernetes Service) cluster along with various node groups, including an “extra” node group. You’ve mentioned that the extra node group is still created even though you’ve set the variable extra_node_group to false.

Based on the provided code, here’s a detailed analysis:

  1. Variable Definition: You’ve defined a variable extra_node_group with a default value of false.
   variable "extra_node_group" {
     description = "When you want to create an extra node group for the special purpose"
     type    = bool
     default = false  # Set to true to enable the extra_node_group, or false to disable it
   }
  1. Usage in the Code: However, there is no condition in your code that checks the value of var.extra_node_group to determine whether or not to create the extra node group. The code snippet you provided is defining the extra node group within the self_managed_node_groups block without any conditional logic.
   self_managed_node_groups = {
     extra = {
       count = var.extra_node_group ? 1 : 0
       platform = "linux"
       name = "extra"
       ...
     }
     ...
   }
  1. Possible Cause: The issue is that in Terraform, the count argument is used at the resource level to determine how many instances of that resource to create, but you’re using it inside a map where it doesn’t have the desired effect.

Solution:

You can fix this issue by restructuring your code to use a conditional expression that controls the creation of the extra node group. Here’s an example modification to the code:

self_managed_node_groups = merge(
  {
    linux = {
      ...
    }
    windows = {
      ...
    }
  },
  var.extra_node_group ? {
    extra = {
      platform = "linux"
      name = "extra"
      ...
    }
  } : {}
)

In this example, the merge function is used to conditionally include the “extra” node group only if var.extra_node_group is true. If var.extra_node_group is false, an empty map is merged, effectively omitting the “extra” node group from the final configuration.

Make sure to adjust the rest of your code accordingly to match this structure.

This approach leverages Terraform’s conditional expressions and map handling to achieve the desired behavior of conditionally creating the extra node group based on the value of the extra_node_group variable.

Và bạn có thể học thêm ở Video dưới

2) “IF/ELSE” to insert values template terraform

block_device_mappings = [
          {
            device_name = ng.platform == "windows" ? "/dev/sda1" : "/dev/xvda",
            ebs = {
              volume_size           = 100
              volume_type           = "gp3"
              iops                  = 3000
              throughput            = 125
              encrypted             = true
              delete_on_termination = true
            }
          }
        ]

ở đây chỉ đơn giản nếu windows thí đưa value “/dev/sda1” còn lại là /dev/xvda

        tags = {
          "k8s.io/cluster-autoscaler/enabled"                 = "true",
          "k8s.io/cluster-autoscaler/${var.eks_cluster_name}" = "owned"
        }

        taints = ng.taints
        labels = ng.labels

        # Conditional AMI type based on the platform
        ami_type = ng.platform == "windows" ? var.windows_ami_type : null,
        instance_types = [ng.instance_type]
        min_size       = ng.min_size
        max_size       = ng.max_size
        desired_size   = ng.desired_size
        key_name = var.node_host_key_name

Còn 1 case khác là: nếu ng.platform == “windows” thì đưa value của var.windows_ami_type vào.

Terraform

Post navigation

Previous Post: [Kubernetes] Monitor Persistent Volume usage in Kubernetes using Prometheus
Next Post: [WordPress] Fix the “Another Update is Currently in Progress” Error in WordPress

More Related Articles

[Terraform ] Using Terragrunt to provision AWS base on Terraform Module Terraform
[Terraform] Your query returned no results. Please change your search criteria and try again Terraform
[Terraform] Online Lab Terraform (FREE). Terraform
[Terraform/Init] Starting to learn Terraform easily. Terraform
[Terraform] Lesson 3: GCP Networking with Terraform GCP - Google Cloud
[Terraform] Lesson 7: Cloud BigQuery with Terraform GCP - Google Cloud

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Tham Gia Group DevOps nhé!
Để Nim có nhiều động lực ra nhiều bài viết.
Để nhận được những thông báo mới nhất.

Recent Posts

  • [Azure] The subscription is not registered to use namespace ‘Microsoft.ContainerService’ May 8, 2025
  • [Azure] Insufficient regional vcpu quota left May 8, 2025
  • [WordPress] How to add a Dynamic watermark on WordPress. May 6, 2025
  • [vnet/Azure] VNet provisioning via Terraform. April 28, 2025
  • [tracetcp] How to perform a tracert command using a specific port. April 3, 2025

Archives

  • May 2025
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • July 2024
  • June 2024
  • May 2024
  • April 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Categories

  • BareMetal
    • NextCloud
  • CI/CD
    • Argo Events
    • ArgoCD
    • ArgoWorkflows
    • Git
      • Bitbucket
    • Harbor
    • Jenkins
    • Spinnaker
    • TeamCity
  • Coding
    • DevSecOps
    • Golang
    • Jquery & JavaScript
    • Laravel
    • NextJS 14 & ReactJS & Type Script
    • Python
    • Selenium
    • Terraform
      • AWS – Amazon Web Service
      • Azure Cloud
      • GCP – Google Cloud
  • Kubernetes & Container
    • Apache Kafka
      • Kafka
      • Kafka Connect
      • Lenses
    • Docker
    • Helm Chart
    • Isito-EnvoyFilter
    • Kong Gateway
    • Kubernetes
      • Ingress
      • Pod
    • Longhorn – Storage
    • MetalLB
    • OAuth2 Proxy
    • Vault
    • VictoriaMetrics
  • Log, Monitor & Tracing
    • DataDog
    • ELK
      • Kibana
      • Logstash
    • Fluent
    • Grafana
    • Prometheus
  • Uncategorized
  • Admin

Copyright © 2025 NimTechnology.