Skip to content

Commit 6b4a129

Browse files
authored
Add support for create-before-destroy plus upgrades (#1)
1 parent d6517be commit 6b4a129

File tree

8 files changed

+136
-12
lines changed

8 files changed

+136
-12
lines changed

.pre-commit-config.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
repos:
22
- repo: https://github.com/pre-commit/pre-commit-hooks
3-
rev: v4.1.0
3+
rev: v4.4.0
44
hooks:
55
- id: check-added-large-files
66
args: ['--maxkb=500']
@@ -17,8 +17,8 @@ repos:
1717
- id: detect-aws-credentials
1818
args: ['--allow-missing-credentials']
1919
- id: trailing-whitespace
20-
- repo: git://github.com/antonbabenko/pre-commit-terraform
21-
rev: 48bc03ca3f0f2f782d2f430069868019a6892062
20+
- repo: https://github.com/antonbabenko/pre-commit-terraform
21+
rev: v1.79.1
2222
hooks:
2323
- id: terraform_fmt
2424
- id: terraform_docs

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Terraform module to provision EKS Managed Node Group
88
```hcl
99
module "eks-node-group" {
1010
source = "native-cube/eks-node-group/aws"
11-
version = "~> 1.0.0"
11+
version = "~> 1.1.0"
1212
1313
cluster_name = aws_eks_cluster.cluster.id
1414
@@ -61,13 +61,13 @@ module "eks-node-group" {
6161
| Name | Version |
6262
|------|---------|
6363
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0.0 |
64-
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 4.0.0 |
64+
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 4.64.0 |
6565

6666
## Providers
6767

6868
| Name | Version |
6969
|------|---------|
70-
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 4.0.0 |
70+
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 4.64.0 |
7171

7272
## Modules
7373

@@ -78,6 +78,7 @@ No modules.
7878
| Name | Type |
7979
|------|------|
8080
| [aws_eks_node_group.main](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/eks_node_group) | resource |
81+
| [aws_eks_node_group.main_create_before_destroy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/eks_node_group) | resource |
8182
| [aws_iam_role.main](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
8283

8384
## Inputs
@@ -88,6 +89,7 @@ No modules.
8889
| <a name="input_ami_type"></a> [ami\_type](#input\_ami\_type) | Type of Amazon Machine Image (AMI) associated with the EKS Node Group. Valid values: AL2\_x86\_64 \| AL2\_x86\_64\_GPU \| AL2\_ARM\_64 \| CUSTOM \| BOTTLEROCKET\_ARM\_64 \| BOTTLEROCKET\_x86\_64. Terraform will only perform drift detection if a configuration value is provided. | `string` | `null` | no |
8990
| <a name="input_capacity_type"></a> [capacity\_type](#input\_capacity\_type) | Type of capacity associated with the EKS Node Group. Defaults to ON\_DEMAND. Valid values: ON\_DEMAND, SPOT. | `string` | `"ON_DEMAND"` | no |
9091
| <a name="input_cluster_name"></a> [cluster\_name](#input\_cluster\_name) | The name of the EKS cluster. | `string` | n/a | yes |
92+
| <a name="input_create_before_destroy"></a> [create\_before\_destroy](#input\_create\_before\_destroy) | Create new node group before destroying an old one. To be used with node\_group\_name\_prefix argument. | `bool` | `false` | no |
9193
| <a name="input_create_iam_role"></a> [create\_iam\_role](#input\_create\_iam\_role) | Create IAM role for node group. Set to false if pass `node_role_arn` as an argument | `bool` | `true` | no |
9294
| <a name="input_desired_size"></a> [desired\_size](#input\_desired\_size) | Desired number of worker nodes. | `number` | n/a | yes |
9395
| <a name="input_disk_size"></a> [disk\_size](#input\_disk\_size) | Disk size in GiB for worker nodes. Defaults to 20. Terraform will only perform drift detection if a configuration value is provided. | `number` | `null` | no |
@@ -108,6 +110,7 @@ No modules.
108110
| <a name="input_subnet_ids"></a> [subnet\_ids](#input\_subnet\_ids) | A list of subnet IDs to launch resources in. | `list(string)` | n/a | yes |
109111
| <a name="input_tags"></a> [tags](#input\_tags) | A map of tags (key-value pairs) passed to resources. | `map(string)` | `{}` | no |
110112
| <a name="input_taints"></a> [taints](#input\_taints) | List of objects containing Kubernetes taints which will be applied to the nodes in the node group. Maximum of 50 taints per node group. | `list(object({ key = string, value = any, effect = string }))` | `[]` | no |
113+
| <a name="input_timeouts"></a> [timeouts](#input\_timeouts) | Create, update, and delete timeout configurations for the node group | `map(string)` | `{}` | no |
111114
| <a name="input_update_config"></a> [update\_config](#input\_update\_config) | Update config configuration block which is a key-value map. Accepted argmuents are `max_unavailable` and `max_unavailable_percentage`. | `map(any)` | `{}` | no |
112115

113116
## Outputs
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
terraform {
2+
required_version = ">= 1.0"
3+
4+
required_providers {
5+
aws = {
6+
source = "hashicorp/aws"
7+
version = ">= 4.64.0"
8+
}
9+
}
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
terraform {
2+
required_version = ">= 1.0"
3+
4+
required_providers {
5+
aws = {
6+
source = "hashicorp/aws"
7+
version = ">= 4.64.0"
8+
}
9+
}
10+
}

main.tf

Lines changed: 91 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
resource "aws_eks_node_group" "main" {
2+
count = var.create_before_destroy ? 0 : 1
3+
24
cluster_name = var.cluster_name
35

46
node_group_name_prefix = var.node_group_name_prefix
57
node_group_name = var.node_group_name
6-
node_role_arn = var.node_role_arn == null ? join("", aws_iam_role.main.*.arn) : var.node_role_arn
8+
node_role_arn = var.node_role_arn == null ? aws_iam_role.main[0].arn : var.node_role_arn
79

810
subnet_ids = var.subnet_ids
911

@@ -61,7 +63,94 @@ resource "aws_eks_node_group" "main" {
6163
}
6264
}
6365

66+
timeouts {
67+
create = lookup(var.timeouts, "create", null)
68+
update = lookup(var.timeouts, "update", null)
69+
delete = lookup(var.timeouts, "delete", null)
70+
}
71+
72+
lifecycle {
73+
ignore_changes = [
74+
scaling_config[0].desired_size
75+
]
76+
}
77+
}
78+
79+
resource "aws_eks_node_group" "main_create_before_destroy" {
80+
count = var.create_before_destroy ? 1 : 0
81+
82+
cluster_name = var.cluster_name
83+
84+
node_group_name_prefix = var.node_group_name_prefix
85+
node_group_name = var.node_group_name
86+
node_role_arn = var.node_role_arn == null ? aws_iam_role.main[0].arn : var.node_role_arn
87+
88+
subnet_ids = var.subnet_ids
89+
90+
ami_type = var.ami_type
91+
disk_size = var.disk_size
92+
instance_types = var.instance_types
93+
capacity_type = var.capacity_type
94+
95+
labels = var.labels
96+
97+
release_version = var.ami_release_version
98+
version = var.kubernetes_version
99+
100+
force_update_version = var.force_update_version
101+
102+
tags = var.tags
103+
104+
scaling_config {
105+
desired_size = var.desired_size
106+
max_size = var.max_size
107+
min_size = var.min_size
108+
}
109+
110+
dynamic "taint" {
111+
for_each = var.taints
112+
content {
113+
key = lookup(taint.value, "key")
114+
value = lookup(taint.value, "value")
115+
effect = lookup(taint.value, "effect")
116+
}
117+
}
118+
119+
dynamic "remote_access" {
120+
for_each = var.ec2_ssh_key != null || var.source_security_group_ids != null ? ["true"] : []
121+
content {
122+
ec2_ssh_key = var.ec2_ssh_key
123+
source_security_group_ids = var.source_security_group_ids
124+
}
125+
}
126+
127+
dynamic "update_config" {
128+
for_each = length(var.update_config) == 0 ? [] : [var.update_config]
129+
content {
130+
max_unavailable = lookup(update_config.value, "max_unavailable", null)
131+
max_unavailable_percentage = lookup(update_config.value, "max_unavailable_percentage", null)
132+
}
133+
}
134+
135+
dynamic "launch_template" {
136+
for_each = length(var.launch_template) == 0 ? [] : [var.launch_template]
137+
content {
138+
id = lookup(launch_template.value, "id", null)
139+
name = lookup(launch_template.value, "name", null)
140+
version = lookup(launch_template.value, "version")
141+
}
142+
}
143+
144+
timeouts {
145+
create = lookup(var.timeouts, "create", null)
146+
update = lookup(var.timeouts, "update", null)
147+
delete = lookup(var.timeouts, "delete", null)
148+
}
149+
64150
lifecycle {
65-
ignore_changes = [scaling_config.0.desired_size]
151+
create_before_destroy = true
152+
ignore_changes = [
153+
scaling_config[0].desired_size
154+
]
66155
}
67156
}

outputs.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
output "iam_role_arn" {
22
description = "IAM role ARN used by node group."
3-
value = join("", aws_iam_role.main.*.arn)
3+
value = try(aws_iam_role.main[0].arn, null)
44
}
55

66
output "iam_role_id" {
77
description = "IAM role ID used by node group."
8-
value = join("", aws_iam_role.main.*.id)
8+
value = try(aws_iam_role.main[0].id, null)
99
}
1010

1111
output "node_group" {
1212
description = "Outputs from EKS node group. See `aws_eks_node_group` Terraform documentation for values"
13-
value = aws_eks_node_group.main
13+
value = var.create_before_destroy ? try(aws_eks_node_group.main_create_before_destroy[0], null) : try(aws_eks_node_group.main[0], null)
1414
}

variables.tf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,15 @@ variable "taints" {
142142
description = "List of objects containing Kubernetes taints which will be applied to the nodes in the node group. Maximum of 50 taints per node group."
143143
default = []
144144
}
145+
146+
variable "create_before_destroy" {
147+
type = bool
148+
description = "Create new node group before destroying an old one. To be used with node_group_name_prefix argument."
149+
default = false
150+
}
151+
152+
variable "timeouts" {
153+
description = "Create, update, and delete timeout configurations for the node group"
154+
type = map(string)
155+
default = {}
156+
}

versions.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ terraform {
44
required_providers {
55
aws = {
66
source = "hashicorp/aws"
7-
version = ">= 4.0.0"
7+
version = ">= 4.64.0"
88
}
99
}
1010
}

0 commit comments

Comments
 (0)