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:
- Variable Definition: You’ve defined a variable
extra_node_group
with a default value offalse
.
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 }
- 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 theextra
node group within theself_managed_node_groups
block without any conditional logic.
self_managed_node_groups = { extra = { count = var.extra_node_group ? 1 : 0 platform = "linux" name = "extra" ... } ... }
- 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.