Skip to content

Commit 34b800d

Browse files
nitrocodecloudpossebotNuru
authored
Support bottlerocket ami types (#93)
* Support bottlerocket ami types Co-authored-by: cloudpossebot <[email protected]> Co-authored-by: Nuru <[email protected]>
1 parent 37d23d3 commit 34b800d

File tree

8 files changed

+92
-34
lines changed

8 files changed

+92
-34
lines changed

README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -249,9 +249,7 @@ Available targets:
249249
|------|---------|
250250
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.14.11 |
251251
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 3.56 |
252-
| <a name="requirement_local"></a> [local](#requirement\_local) | >= 1.3 |
253252
| <a name="requirement_random"></a> [random](#requirement\_random) | >= 2.0 |
254-
| <a name="requirement_template"></a> [template](#requirement\_template) | >= 2.0 |
255253

256254
## Providers
257255

@@ -294,8 +292,8 @@ Available targets:
294292
| <a name="input_additional_tag_map"></a> [additional\_tag\_map](#input\_additional\_tag\_map) | Additional key-value pairs to add to each map in `tags_as_list_of_maps`. Not added to `tags` or `id`.<br>This is for some rare cases where resources want additional configuration of tags<br>and therefore take a list of maps with tag key, value, and additional configuration. | `map(string)` | `{}` | no |
295293
| <a name="input_after_cluster_joining_userdata"></a> [after\_cluster\_joining\_userdata](#input\_after\_cluster\_joining\_userdata) | Additional `bash` commands to execute on each worker node after joining the EKS cluster (after executing the `bootstrap.sh` script). For more info, see https://kubedex.com/90-days-of-aws-eks-in-production | `list(string)` | `[]` | no |
296294
| <a name="input_ami_image_id"></a> [ami\_image\_id](#input\_ami\_image\_id) | AMI to use. Ignored if `launch_template_id` is supplied. | `list(string)` | `[]` | no |
297-
| <a name="input_ami_release_version"></a> [ami\_release\_version](#input\_ami\_release\_version) | EKS AMI version to use, e.g. "1.16.13-20200821" (no "v"). Defaults to latest version for Kubernetes version. | `list(string)` | `[]` | no |
298-
| <a name="input_ami_type"></a> [ami\_type](#input\_ami\_type) | Type of Amazon Machine Image (AMI) associated with the EKS Node Group.<br>Defaults to `AL2_x86_64`. Valid values: `AL2_x86_64`, `AL2_x86_64_GPU`, and `AL2_ARM_64`. | `string` | `"AL2_x86_64"` | no |
295+
| <a name="input_ami_release_version"></a> [ami\_release\_version](#input\_ami\_release\_version) | EKS AMI version to use, e.g. For AL2 "1.16.13-20200821" or for bottlerocket "1.2.0-ccf1b754" (no "v"). For AL2 and bottlerocket, it defaults to latest version for Kubernetes version. | `list(string)` | `[]` | no |
296+
| <a name="input_ami_type"></a> [ami\_type](#input\_ami\_type) | Type of Amazon Machine Image (AMI) associated with the EKS Node Group.<br>Defaults to `AL2_x86_64`. Valid values: `AL2_x86_64`, `AL2_x86_64_GPU`, `AL2_ARM_64`, `BOTTLEROCKET_x86_64`, and `BOTTLEROCKET_ARM_64`. | `string` | `"AL2_x86_64"` | no |
299297
| <a name="input_associated_security_group_ids"></a> [associated\_security\_group\_ids](#input\_associated\_security\_group\_ids) | A list of IDs of Security Groups to associate the node group with, in addition to the EKS' created security group.<br>These security groups will not be modified. | `list(string)` | `[]` | no |
300298
| <a name="input_attributes"></a> [attributes](#input\_attributes) | ID element. Additional attributes (e.g. `workers` or `cluster`) to add to `id`,<br>in the order they appear in the list. New attributes are appended to the<br>end of the list. The elements of the list are joined by the `delimiter`<br>and treated as a single ID element. | `list(string)` | `[]` | no |
301299
| <a name="input_before_cluster_joining_userdata"></a> [before\_cluster\_joining\_userdata](#input\_before\_cluster\_joining\_userdata) | Additional `bash` commands to execute on each worker node before joining the EKS cluster (before executing the `bootstrap.sh` script). For more info, see https://kubedex.com/90-days-of-aws-eks-in-production | `list(string)` | `[]` | no |

ami.tf

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,59 @@ locals {
55
"AL2_x86_64" : "",
66
"AL2_x86_64_GPU" : "-gpu",
77
"AL2_ARM_64" : "-arm64",
8+
"BOTTLEROCKET_x86_64" : "x86_64",
9+
"BOTTLEROCKET_ARM_64" : "aarch64"
10+
}
11+
12+
ami_kind = split("_", var.ami_type)[0]
13+
14+
ami_format = {
15+
# amazon-eks{arch_label}-node-{ami_kubernetes_version}-v{ami_version}
16+
# e.g. amazon-eks-arm64-node-1.21-v20211013
17+
"AL2" : "amazon-eks%s-node-%s"
18+
# bottlerocket-aws-k8s-{ami_kubernetes_version}-{arch_label}-v{ami_version}
19+
# e.g. bottlerocket-aws-k8s-1.21-x86_64-v1.2.0-ccf1b754
20+
"BOTTLEROCKET" : "bottlerocket-aws-k8s-%s-%s-%s"
821
}
922

1023
# Kubernetes version priority (first one to be set wins)
1124
# 1. prefix of var.ami_release_version
1225
# 2. var.kubernetes_version
1326
# 3. data.eks_cluster.this.kubernetes_version
14-
need_cluster_kubernetes_version = local.enabled ? local.need_ami_id && length(concat(var.ami_release_version, var.kubernetes_version)) == 0 : false
27+
need_cluster_kubernetes_version = local.enabled ? local.need_ami_id && length(var.kubernetes_version) == 0 : false
1528

16-
ami_kubernetes_version = local.need_ami_id ? (local.need_cluster_kubernetes_version ? data.aws_eks_cluster.this[0].version :
17-
regex("^(\\d+\\.\\d+)", coalesce(try(var.ami_release_version[0], null), try(var.kubernetes_version[0], null)))[0]
18-
) : ""
29+
use_cluster_kubernetes_version = local.need_cluster_kubernetes_version && (local.ami_kind == "BOTTLEROCKET" || length(var.ami_release_version) == 0)
1930

20-
ami_version_regex = local.need_ami_id ? (length(var.ami_release_version) == 1 ?
21-
replace(var.ami_release_version[0], "/^(\\d+\\.\\d+)\\.\\d+-(\\d+)$/", "$1-v$2") :
22-
"${local.ami_kubernetes_version}-*"
31+
ami_kubernetes_version = local.need_ami_id ? (local.use_cluster_kubernetes_version ? data.aws_eks_cluster.this[0].version :
32+
regex("^(\\d+\\.\\d+)", coalesce(local.ami_kind == "AL2" ? try(var.ami_release_version[0], null) : null, try(var.kubernetes_version[0], null)))[0]
2333
) : ""
2434

25-
ami_regex = local.need_ami_id ? format("amazon-eks%s-node-%s", local.arch_label_map[var.ami_type], local.ami_version_regex) : ""
35+
# if ami_release_version is provided
36+
ami_version_regex = local.need_ami_id ? {
37+
# if ami_release_version = "1.21-20211013"
38+
# insert the letter v prior to the ami_version so it becomes 1.21-v20211013
39+
# if not, use the kubernetes version
40+
"AL2" : (length(var.ami_release_version) == 1 ?
41+
replace(var.ami_release_version[0], "/^(\\d+\\.\\d+)\\.\\d+-(\\d+)$/", "$1-v$2") :
42+
"${local.ami_kubernetes_version}-*"),
43+
# if ami_release_version = "1.2.0-ccf1b754"
44+
# prefex the ami release version with the letter v
45+
# if not, use an asterisk
46+
"BOTTLEROCKET" : (length(var.ami_release_version) == 1 ?
47+
format("v%s", var.ami_release_version[0]) : "*"),
48+
} : {}
49+
50+
ami_regex = local.need_ami_id ? {
51+
"AL2" : format(local.ami_format["AL2"], local.arch_label_map[var.ami_type], local.ami_version_regex[local.ami_kind]),
52+
"BOTTLEROCKET" : format(local.ami_format["BOTTLEROCKET"], local.ami_kubernetes_version, local.arch_label_map[var.ami_type], local.ami_version_regex[local.ami_kind]),
53+
} : {}
2654
}
2755

2856
data "aws_ami" "selected" {
2957
count = local.enabled && local.need_ami_id ? 1 : 0
3058

3159
most_recent = true
32-
name_regex = local.ami_regex
60+
name_regex = local.ami_regex[local.ami_kind]
3361

3462
owners = ["amazon"]
3563
}
36-

docs/terraform.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
|------|---------|
66
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.14.11 |
77
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 3.56 |
8-
| <a name="requirement_local"></a> [local](#requirement\_local) | >= 1.3 |
98
| <a name="requirement_random"></a> [random](#requirement\_random) | >= 2.0 |
10-
| <a name="requirement_template"></a> [template](#requirement\_template) | >= 2.0 |
119

1210
## Providers
1311

@@ -50,8 +48,8 @@
5048
| <a name="input_additional_tag_map"></a> [additional\_tag\_map](#input\_additional\_tag\_map) | Additional key-value pairs to add to each map in `tags_as_list_of_maps`. Not added to `tags` or `id`.<br>This is for some rare cases where resources want additional configuration of tags<br>and therefore take a list of maps with tag key, value, and additional configuration. | `map(string)` | `{}` | no |
5149
| <a name="input_after_cluster_joining_userdata"></a> [after\_cluster\_joining\_userdata](#input\_after\_cluster\_joining\_userdata) | Additional `bash` commands to execute on each worker node after joining the EKS cluster (after executing the `bootstrap.sh` script). For more info, see https://kubedex.com/90-days-of-aws-eks-in-production | `list(string)` | `[]` | no |
5250
| <a name="input_ami_image_id"></a> [ami\_image\_id](#input\_ami\_image\_id) | AMI to use. Ignored if `launch_template_id` is supplied. | `list(string)` | `[]` | no |
53-
| <a name="input_ami_release_version"></a> [ami\_release\_version](#input\_ami\_release\_version) | EKS AMI version to use, e.g. "1.16.13-20200821" (no "v"). Defaults to latest version for Kubernetes version. | `list(string)` | `[]` | no |
54-
| <a name="input_ami_type"></a> [ami\_type](#input\_ami\_type) | Type of Amazon Machine Image (AMI) associated with the EKS Node Group.<br>Defaults to `AL2_x86_64`. Valid values: `AL2_x86_64`, `AL2_x86_64_GPU`, and `AL2_ARM_64`. | `string` | `"AL2_x86_64"` | no |
51+
| <a name="input_ami_release_version"></a> [ami\_release\_version](#input\_ami\_release\_version) | EKS AMI version to use, e.g. For AL2 "1.16.13-20200821" or for bottlerocket "1.2.0-ccf1b754" (no "v"). For AL2 and bottlerocket, it defaults to latest version for Kubernetes version. | `list(string)` | `[]` | no |
52+
| <a name="input_ami_type"></a> [ami\_type](#input\_ami\_type) | Type of Amazon Machine Image (AMI) associated with the EKS Node Group.<br>Defaults to `AL2_x86_64`. Valid values: `AL2_x86_64`, `AL2_x86_64_GPU`, `AL2_ARM_64`, `BOTTLEROCKET_x86_64`, and `BOTTLEROCKET_ARM_64`. | `string` | `"AL2_x86_64"` | no |
5553
| <a name="input_associated_security_group_ids"></a> [associated\_security\_group\_ids](#input\_associated\_security\_group\_ids) | A list of IDs of Security Groups to associate the node group with, in addition to the EKS' created security group.<br>These security groups will not be modified. | `list(string)` | `[]` | no |
5654
| <a name="input_attributes"></a> [attributes](#input\_attributes) | ID element. Additional attributes (e.g. `workers` or `cluster`) to add to `id`,<br>in the order they appear in the list. New attributes are appended to the<br>end of the list. The elements of the list are joined by the `delimiter`<br>and treated as a single ID element. | `list(string)` | `[]` | no |
5755
| <a name="input_before_cluster_joining_userdata"></a> [before\_cluster\_joining\_userdata](#input\_before\_cluster\_joining\_userdata) | Additional `bash` commands to execute on each worker node before joining the EKS cluster (before executing the `bootstrap.sh` script). For more info, see https://kubedex.com/90-days-of-aws-eks-in-production | `list(string)` | `[]` | no |

examples/complete/fixtures.us-east-2.tfvars

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ stage = "test"
1010

1111
name = "eks-node-group"
1212

13-
kubernetes_version = "1.19"
13+
kubernetes_version = "1.21"
1414

1515
oidc_provider_enabled = true
1616

examples/complete/main.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,11 @@ module "eks_node_group" {
151151
node_role_policy_arns = [local.extra_policy_arn]
152152
update_config = var.update_config
153153

154+
after_cluster_joining_userdata = var.after_cluster_joining_userdata
155+
156+
ami_type = var.ami_type
157+
ami_release_version = var.ami_release_version
158+
154159
before_cluster_joining_userdata = [var.before_cluster_joining_userdata]
155160

156161
context = module.this.context

examples/complete/variables.tf

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,42 @@ variable "ec2_ssh_key_name" {
159159
error_message = "You may not specify more than one `ec2_ssh_key_name`."
160160
}
161161
}
162+
163+
variable "ami_type" {
164+
type = string
165+
description = <<-EOT
166+
Type of Amazon Machine Image (AMI) associated with the EKS Node Group.
167+
Defaults to `AL2_x86_64`. Valid values: `AL2_x86_64`, `AL2_x86_64_GPU`, `AL2_ARM_64`, `BOTTLEROCKET_x86_64`, and `BOTTLEROCKET_ARM_64`.
168+
EOT
169+
default = "AL2_x86_64"
170+
validation {
171+
condition = (
172+
contains(["AL2_x86_64", "AL2_x86_64_GPU", "AL2_ARM_64", "BOTTLEROCKET_x86_64", "BOTTLEROCKET_ARM_64"], var.ami_type)
173+
)
174+
error_message = "Var ami_type must be one of \"AL2_x86_64\", \"AL2_x86_64_GPU\", \"AL2_ARM_64\", \"BOTTLEROCKET_x86_64\", and \"BOTTLEROCKET_ARM_64\"."
175+
}
176+
}
177+
178+
variable "ami_release_version" {
179+
type = list(string)
180+
default = []
181+
description = "EKS AMI version to use, e.g. \"1.16.13-20200821\" (no \"v\"). Defaults to latest version for Kubernetes version."
182+
validation {
183+
condition = (
184+
length(var.ami_release_version) == 0 ? true : length(regexall("^\\d+\\.\\d+\\.\\d+-[\\da-z]+$", var.ami_release_version[0])) == 1
185+
)
186+
error_message = "Var ami_release_version, if supplied, must be like \"1.16.13-20200821\" (no \"v\")."
187+
}
188+
}
189+
190+
variable "after_cluster_joining_userdata" {
191+
type = list(string)
192+
default = []
193+
description = "Additional `bash` commands to execute on each worker node after joining the EKS cluster (after executing the `bootstrap.sh` script). For more info, see https://kubedex.com/90-days-of-aws-eks-in-production"
194+
validation {
195+
condition = (
196+
length(var.after_cluster_joining_userdata) < 2
197+
)
198+
error_message = "You may not specify more than one `after_cluster_joining_userdata`."
199+
}
200+
}

variables.tf

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,19 +107,18 @@ variable "node_role_permissions_boundary" {
107107
default = null
108108
}
109109

110-
111110
variable "ami_type" {
112111
type = string
113112
description = <<-EOT
114113
Type of Amazon Machine Image (AMI) associated with the EKS Node Group.
115-
Defaults to `AL2_x86_64`. Valid values: `AL2_x86_64`, `AL2_x86_64_GPU`, and `AL2_ARM_64`.
114+
Defaults to `AL2_x86_64`. Valid values: `AL2_x86_64`, `AL2_x86_64_GPU`, `AL2_ARM_64`, `BOTTLEROCKET_x86_64`, and `BOTTLEROCKET_ARM_64`.
116115
EOT
117116
default = "AL2_x86_64"
118117
validation {
119118
condition = (
120-
contains(["AL2_x86_64", "AL2_x86_64_GPU", "AL2_ARM_64"], var.ami_type)
119+
contains(["AL2_x86_64", "AL2_x86_64_GPU", "AL2_ARM_64", "BOTTLEROCKET_x86_64", "BOTTLEROCKET_ARM_64"], var.ami_type)
121120
)
122-
error_message = "Var ami_type must be one of \"AL2_x86_64\", \"AL2_x86_64_GPU\", and \"AL2_ARM_64\"."
121+
error_message = "Var ami_type must be one of \"AL2_x86_64\", \"AL2_x86_64_GPU\", \"AL2_ARM_64\", \"BOTTLEROCKET_x86_64\", and \"BOTTLEROCKET_ARM_64\"."
123122
}
124123
}
125124

@@ -232,12 +231,12 @@ variable "ami_image_id" {
232231
variable "ami_release_version" {
233232
type = list(string)
234233
default = []
235-
description = "EKS AMI version to use, e.g. \"1.16.13-20200821\" (no \"v\"). Defaults to latest version for Kubernetes version."
234+
description = "EKS AMI version to use, e.g. For AL2 \"1.16.13-20200821\" or for bottlerocket \"1.2.0-ccf1b754\" (no \"v\"). For AL2 and bottlerocket, it defaults to latest version for Kubernetes version."
236235
validation {
237236
condition = (
238-
length(var.ami_release_version) == 0 ? true : length(regexall("^\\d+\\.\\d+\\.\\d+-\\d+$", var.ami_release_version[0])) == 1
237+
length(var.ami_release_version) == 0 ? true : length(regexall("^\\d+\\.\\d+\\.\\d+-[\\da-z]+$", var.ami_release_version[0])) == 1
239238
)
240-
error_message = "Var ami_release_version, if supplied, must be like \"1.16.13-20200821\" (no \"v\")."
239+
error_message = "Var ami_release_version, if supplied, must be like for AL2 \"1.16.13-20200821\" or for bottlerocket \"1.2.0-ccf1b754\" (no \"v\")."
241240
}
242241
}
243242

versions.tf

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,6 @@ terraform {
88
# update_config starts at 3.56
99
version = ">= 3.56"
1010
}
11-
template = {
12-
source = "hashicorp/template"
13-
version = ">= 2.0"
14-
}
15-
local = {
16-
source = "hashicorp/local"
17-
version = ">= 1.3"
18-
}
1911
random = {
2012
source = "hashicorp/random"
2113
version = ">= 2.0"

0 commit comments

Comments
 (0)