-
Notifications
You must be signed in to change notification settings - Fork 95
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add aws_launch_template & config vars to enable options to specify pre-bootstrap commands and/or custom AMI for EKS nodes #2621
Changes from 2 commits
70d2020
1e8745e
d0619d3
e60a6fe
612533a
9ed0ceb
daaa146
ed56038
bd72818
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,46 @@ resource "aws_eks_cluster" "main" { | |
tags = merge({ Name = var.name }, var.tags) | ||
} | ||
|
||
## aws_launch_template user_data invocation | ||
## If using a Custom AMI, then the /etc/eks/bootstrap cmds and args must be included/modified, | ||
## otherwise, on default AWS EKS Node AMI, the bootstrap cmd is appended automatically | ||
resource "aws_launch_template" "main" { | ||
# Invoke launch_template only if var.node_prebootstrap_command is not null or custom_ami is not null | ||
count = var.node_prebootstrap_command != null ? length(var.node_groups) : length(local.cust_ami_node_index) | ||
name = var.node_prebootstrap_command != null ? var.node_groups[count.index].name : var.node_groups[local.cust_ami_node_index[count.index]].name | ||
image_id = var.node_prebootstrap_command != null ? var.node_groups[count.index].custom_ami : var.node_groups[local.cust_ami_node_index[count.index]].custom_ami | ||
|
||
vpc_security_group_ids = var.cluster_security_groups | ||
|
||
block_device_mappings { | ||
device_name = "/dev/xvda" | ||
|
||
ebs { | ||
volume_size = 50 | ||
volume_type = "gp2" | ||
} | ||
} | ||
# https://docs.aws.amazon.com/eks/latest/userguide/launch-templates.html#launch-template-basics | ||
user_data = base64encode(<<-EOF | ||
MIME-Version: 1.0 | ||
Content-Type: multipart/mixed; boundary="==MYBOUNDARY==" | ||
|
||
--==MYBOUNDARY== | ||
Content-Type: text/x-shellscript; charset="us-ascii" | ||
%{ if var.node_prebootstrap_command != null }${var.node_prebootstrap_command}%{ endif } | ||
|
||
%{ if var.node_prebootstrap_command != null && var.node_groups[count.index].custom_ami != null }--==MYBOUNDARY==%{ endif } | ||
%{ if var.node_prebootstrap_command != null && var.node_groups[count.index].custom_ami != null }Content-Type: text/x-shellscript; charset="us-ascii"%{ endif } | ||
%{ if var.node_prebootstrap_command != null && var.node_groups[count.index].custom_ami == null }%{ else }#!/bin/bash%{ endif } | ||
%{ if var.node_prebootstrap_command != null && var.node_groups[count.index].custom_ami == null }%{ else }set -ex%{ endif } | ||
%{ if var.node_prebootstrap_command != null && var.node_groups[count.index].custom_ami == null }%{ else }B64_CLUSTER_CA=${aws_eks_cluster.main.certificate_authority[0].data}%{ endif } | ||
%{ if var.node_prebootstrap_command != null && var.node_groups[count.index].custom_ami == null }%{ else }API_SERVER_URL=${aws_eks_cluster.main.endpoint}%{ endif } | ||
%{ if var.node_prebootstrap_command != null && var.node_groups[count.index].custom_ami == null }%{ else }/etc/eks/bootstrap.sh ${aws_eks_cluster.main.name} --b64-cluster-ca $B64_CLUSTER_CA --apiserver-endpoint $API_SERVER_URL%{ endif } | ||
|
||
--==MYBOUNDARY==-- | ||
EOF | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would suggest moving this into base64encode(templatefile("${path.module}/files/user_data.tpl", {
node_prebootstrap_command = var.node_prebootstrap_command
node_groups = var.node_groups
aws_eks_cluster = aws_eks_cluster.main
count_index = count.index
cust_ami_node_index = local.cust_ami_node_index
})) as this will become easier to maintain There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've implemented the suggested modification with updated main.tf and added files/user_data.tftpl |
||
) | ||
} | ||
|
||
resource "aws_eks_node_group" "main" { | ||
count = length(var.node_groups) | ||
|
@@ -30,8 +70,8 @@ resource "aws_eks_node_group" "main" { | |
subnet_ids = var.node_groups[count.index].single_subnet ? [element(var.cluster_subnets, 0)] : var.cluster_subnets | ||
|
||
instance_types = [var.node_groups[count.index].instance_type] | ||
ami_type = var.node_groups[count.index].gpu == true ? "AL2_x86_64_GPU" : "AL2_x86_64" | ||
disk_size = 50 | ||
ami_type = var.node_groups[count.index].custom_ami != null ? "CUSTOM" : (var.node_groups[count.index].gpu == true ? "AL2_x86_64_GPU" : "AL2_x86_64") | ||
disk_size = var.node_prebootstrap_command == null && var.node_groups[count.index].custom_ami == null ? 50 : null | ||
Comment on lines
+73
to
+74
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is nothing wrong with the way it is right now, but it would be better if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @viniciusdc |
||
|
||
scaling_config { | ||
min_size = var.node_groups[count.index].min_size | ||
|
@@ -49,6 +89,15 @@ resource "aws_eks_node_group" "main" { | |
] | ||
} | ||
|
||
# Invoke launch_template only if var.node_prebootstrap_command is not null or node group custom_ami is not null | ||
dynamic "launch_template" { | ||
for_each = var.node_prebootstrap_command == null && var.node_groups[count.index].custom_ami == null ? [] : [1] | ||
content { | ||
id = var.node_prebootstrap_command != null ? aws_launch_template.main[count.index].id : aws_launch_template.main[index(local.cust_ami_node_index, count.index)].id | ||
version = var.node_prebootstrap_command != null ? aws_launch_template.main[count.index].latest_version : aws_launch_template.main[index(local.cust_ami_node_index, count.index)].latest_version | ||
} | ||
} | ||
|
||
# Ensure that IAM Role permissions are created before and deleted | ||
# after EKS Node Group handling. Otherwise, EKS will not be able to | ||
# properly delete EC2 Instances and Elastic Network Interfaces. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
to follow the aws standard naming, let's use
ami_type
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hello @viniciusdc,
in this case, the
custom_ami
prompts the user to input an AMI Id, as opposed to the ami_type (e.g. "CUSTOM"). The corresponding aws var here isimage_id
. My impression ofimage_id
is that it seems a bit ambiguous for nebari config purposes since it does not make it obvious that an ami is expected.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@viniciusdc how about using
ami_image_id
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@joneszc I see; I was under the impression
ami_type
would support the ID of a custom AMI as well, but after your comment and re-reding the docs, that does not seem to be true (unfortunately). I agree with you that image_id would also be ambiguous, considering the overall shape of the nebari-config.