Skip to content

Commit

Permalink
feat: Search for Windows or Linux AMIs only if they are needed (terra…
Browse files Browse the repository at this point in the history
  • Loading branch information
barryib authored and ArchiFleKs committed Jun 1, 2021
1 parent ec92645 commit e6cda0e
Show file tree
Hide file tree
Showing 13 changed files with 35 additions and 22 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ MIT Licensed. See [LICENSE](https://github.com/terraform-aws-modules/terraform-a
| <a name="output_workers_asg_arns"></a> [workers\_asg\_arns](#output\_workers\_asg\_arns) | IDs of the autoscaling groups containing workers. |
| <a name="output_workers_asg_names"></a> [workers\_asg\_names](#output\_workers\_asg\_names) | Names of the autoscaling groups containing workers. |
| <a name="output_workers_default_ami_id"></a> [workers\_default\_ami\_id](#output\_workers\_default\_ami\_id) | ID of the default worker group AMI |
| <a name="output_workers_default_ami_id_windows"></a> [workers\_default\_ami\_id\_windows](#output\_workers\_default\_ami\_id\_windows) | ID of the default Windows worker group AMI |
| <a name="output_workers_launch_template_arns"></a> [workers\_launch\_template\_arns](#output\_workers\_launch\_template\_arns) | ARNs of the worker launch templates. |
| <a name="output_workers_launch_template_ids"></a> [workers\_launch\_template\_ids](#output\_workers\_launch\_template\_ids) | IDs of the worker launch templates. |
| <a name="output_workers_launch_template_latest_versions"></a> [workers\_launch\_template\_latest\_versions](#output\_workers\_launch\_template\_latest\_versions) | Latest versions of the worker launch templates. |
Expand Down
4 changes: 4 additions & 0 deletions data.tf
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ data "aws_iam_policy_document" "workers_assume_role_policy" {
}

data "aws_ami" "eks_worker" {
count = local.worker_has_linux_ami ? 1 : 0

filter {
name = "name"
values = [local.worker_ami_name_filter]
Expand All @@ -25,6 +27,8 @@ data "aws_ami" "eks_worker" {
}

data "aws_ami" "eks_worker_windows" {
count = local.worker_has_windows_ami ? 1 : 0

filter {
name = "name"
values = [local.worker_ami_name_filter_windows]
Expand Down
2 changes: 1 addition & 1 deletion examples/basic/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ module "vpc" {
module "eks" {
source = "../.."
cluster_name = local.cluster_name
cluster_version = "1.17"
cluster_version = "1.20"
subnets = module.vpc.private_subnets

tags = {
Expand Down
2 changes: 1 addition & 1 deletion examples/fargate/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ module "vpc" {
module "eks" {
source = "../.."
cluster_name = local.cluster_name
cluster_version = "1.17"
cluster_version = "1.20"
subnets = module.vpc.private_subnets

tags = {
Expand Down
2 changes: 1 addition & 1 deletion examples/instance_refresh/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ resource "aws_autoscaling_lifecycle_hook" "aws_node_termination_handler" {
module "eks" {
source = "../.."
cluster_name = local.cluster_name
cluster_version = "1.19"
cluster_version = "1.20"
subnets = module.vpc.public_subnets
vpc_id = module.vpc.vpc_id
enable_irsa = true
Expand Down
2 changes: 1 addition & 1 deletion examples/irsa/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ module "vpc" {
module "eks" {
source = "../.."
cluster_name = local.cluster_name
cluster_version = "1.17"
cluster_version = "1.20"
subnets = module.vpc.public_subnets
vpc_id = module.vpc.vpc_id
enable_irsa = true
Expand Down
2 changes: 1 addition & 1 deletion examples/launch_templates/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ module "vpc" {
module "eks" {
source = "../.."
cluster_name = local.cluster_name
cluster_version = "1.17"
cluster_version = "1.20"
subnets = module.vpc.public_subnets
vpc_id = module.vpc.vpc_id

Expand Down
2 changes: 1 addition & 1 deletion examples/launch_templates_with_managed_node_groups/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ module "vpc" {
module "eks" {
source = "../.."
cluster_name = local.cluster_name
cluster_version = "1.17"
cluster_version = "1.20"
subnets = module.vpc.private_subnets
vpc_id = module.vpc.vpc_id

Expand Down
2 changes: 1 addition & 1 deletion examples/managed_node_groups/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ module "vpc" {
module "eks" {
source = "../.."
cluster_name = local.cluster_name
cluster_version = "1.17"
cluster_version = "1.20"
subnets = module.vpc.private_subnets

tags = {
Expand Down
2 changes: 1 addition & 1 deletion examples/secrets_encryption/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ module "vpc" {
module "eks" {
source = "../.."
cluster_name = local.cluster_name
cluster_version = "1.17"
cluster_version = "1.20"
subnets = module.vpc.private_subnets

cluster_encryption_config = [
Expand Down
2 changes: 1 addition & 1 deletion examples/spot_instances/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ module "vpc" {
module "eks" {
source = "../.."
cluster_name = local.cluster_name
cluster_version = "1.17"
cluster_version = "1.20"
subnets = module.vpc.public_subnets
vpc_id = module.vpc.vpc_id

Expand Down
27 changes: 15 additions & 12 deletions local.tf
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ locals {
worker_security_group_id = var.worker_create_security_group ? join("", aws_security_group.workers.*.id) : var.worker_security_group_id

default_iam_role_id = concat(aws_iam_role.workers.*.id, [""])[0]
default_ami_id_linux = coalesce(local.workers_group_defaults.ami_id, data.aws_ami.eks_worker.id)
default_ami_id_windows = coalesce(local.workers_group_defaults.ami_id_windows, data.aws_ami.eks_worker_windows.id)
default_ami_id_linux = local.workers_group_defaults.ami_id != "" ? local.workers_group_defaults.ami_id : concat(data.aws_ami.eks_worker.*.id, [""])[0]
default_ami_id_windows = local.workers_group_defaults.ami_id_windows != "" ? local.workers_group_defaults.ami_id_windows : concat(data.aws_ami.eks_worker_windows.*.id, [""])[0]

kubeconfig_name = var.kubeconfig_name == "" ? "eks_${var.cluster_name}" : var.kubeconfig_name

worker_group_count = length(var.worker_groups)
worker_group_launch_template_count = length(var.worker_groups_launch_template)

worker_has_linux_ami = length([for x in concat(var.worker_groups, var.worker_groups_launch_template) : x if lookup(x, "platform", "linux") == "linux"]) > 0
worker_has_windows_ami = length([for x in concat(var.worker_groups, var.worker_groups_launch_template) : x if lookup(x, "platform", "linux") == "windows"]) > 0

worker_ami_name_filter = var.worker_ami_name_filter != "" ? var.worker_ami_name_filter : "amazon-eks-node-${var.cluster_version}-v*"
# Windows nodes are available from k8s 1.14. If cluster version is less than 1.14, fix ami filter to some constant to not fail on 'terraform plan'.
worker_ami_name_filter_windows = (var.worker_ami_name_filter_windows != "" ?
Expand Down Expand Up @@ -72,16 +75,16 @@ locals {
additional_ebs_volumes = [] # A list of additional volumes to be attached to the instances on this Auto Scaling group. Each volume should be an object with the following: block_device_name (required), volume_size, volume_type, iops, encrypted, kms_key_id (only on launch-template), delete_on_termination. Optional values are grabbed from root volume or from defaults
warm_pool = null # If this block is configured, add a Warm Pool to the specified Auto Scaling group.
# Settings for launch templates
root_block_device_name = data.aws_ami.eks_worker.root_device_name # Root device name for workers. If non is provided, will assume default AMI was used.
root_kms_key_id = "" # The KMS key to use when encrypting the root storage device
launch_template_id = null # The id of the launch template used for managed node_groups
launch_template_version = "$Latest" # The latest version of the launch template to use in the autoscaling group
update_default_version = false # Update the autoscaling group launch template's default version upon each update
launch_template_placement_tenancy = "default" # The placement tenancy for instances
launch_template_placement_group = null # The name of the placement group into which to launch the instances, if any.
root_encrypted = false # Whether the volume should be encrypted or not
eni_delete = true # Delete the Elastic Network Interface (ENI) on termination (if set to false you will have to manually delete before destroying)
cpu_credits = "standard" # T2/T3 unlimited mode, can be 'standard' or 'unlimited'. Used 'standard' mode as default to avoid paying higher costs
root_block_device_name = concat(data.aws_ami.eks_worker.*.root_device_name, [""])[0] # Root device name for workers. If non is provided, will assume default AMI was used.
root_kms_key_id = "" # The KMS key to use when encrypting the root storage device
launch_template_id = null # The id of the launch template used for managed node_groups
launch_template_version = "$Latest" # The latest version of the launch template to use in the autoscaling group
update_default_version = false # Update the autoscaling group launch template's default version upon each update
launch_template_placement_tenancy = "default" # The placement tenancy for instances
launch_template_placement_group = null # The name of the placement group into which to launch the instances, if any.
root_encrypted = false # Whether the volume should be encrypted or not
eni_delete = true # Delete the Elastic Network Interface (ENI) on termination (if set to false you will have to manually delete before destroying)
cpu_credits = "standard" # T2/T3 unlimited mode, can be 'standard' or 'unlimited'. Used 'standard' mode as default to avoid paying higher costs
market_type = null
metadata_http_endpoint = "enabled" # The state of the metadata service: enabled, disabled.
metadata_http_tokens = "optional" # If session tokens are required: optional, required.
Expand Down
7 changes: 6 additions & 1 deletion outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,12 @@ output "workers_user_data" {

output "workers_default_ami_id" {
description = "ID of the default worker group AMI"
value = data.aws_ami.eks_worker.id
value = local.default_ami_id_linux
}

output "workers_default_ami_id_windows" {
description = "ID of the default Windows worker group AMI"
value = local.default_ami_id_windows
}

output "workers_launch_template_ids" {
Expand Down

0 comments on commit e6cda0e

Please sign in to comment.