Skip to content
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

Adding support for launch templates & userdata parameters #27

Merged
merged 17 commits into from
Aug 29, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ For automated tests of the complete example using [bats](https://github.com/bats
attributes = var.attributes
tags = var.tags
subnet_ids = module.subnets.public_subnet_ids
instance_types = var.instance_types
instance_type = var.instance_type
desired_size = var.desired_size
min_size = var.min_size
max_size = var.max_size
Expand Down Expand Up @@ -178,15 +178,15 @@ Available targets:
| Name | Version |
|------|---------|
| terraform | >= 0.12.0, < 0.14.0 |
| aws | >= 2.0, < 4.0 |
| aws | ~> 3.0 |
| local | ~> 1.3 |
| template | ~> 2.0 |

## Providers

| Name | Version |
|------|---------|
| aws | >= 2.0, < 4.0 |
| aws | ~> 3.0 |

## Inputs

Expand All @@ -208,10 +208,13 @@ Available targets:
| existing\_workers\_role\_policy\_arns | List of existing policy ARNs that will be attached to the workers default role on creation | `list(string)` | `[]` | no |
| existing\_workers\_role\_policy\_arns\_count | Count of existing policy ARNs that will be attached to the workers default role on creation. Needed to prevent Terraform error `count can't be computed` | `number` | `0` | no |
| id\_length\_limit | Limit `id` to this many characters.<br>Set to `0` for unlimited length.<br>Set to `null` for default, which is `0`.<br>Does not affect `id_full`. | `number` | `null` | no |
| instance\_types | Set of instance types associated with the EKS Node Group. Defaults to ["t3.medium"]. Terraform will only perform drift detection if a configuration value is provided | `list(string)` | n/a | yes |
| instance\_type | Instance type associated with the EKS Node Group. Defaults to "t3.medium". Terraform will only perform drift detection if a configuration value is provided | `string` | n/a | yes |
| kubernetes\_labels | Key-value mapping of Kubernetes labels. Only labels that are applied with the EKS API are managed by this argument. Other Kubernetes labels applied to the EKS Node Group will not be managed | `map(string)` | `{}` | no |
| kubernetes\_version | Kubernetes version. Defaults to EKS Cluster Kubernetes version. Terraform will only perform drift detection if a configuration value is provided | `string` | `null` | no |
| label\_order | The naming order of the id output and Name tag.<br>Defaults to ["namespace", "environment", "stage", "name", "attributes"].<br>You can omit any of the 5 elements, but at least one must be present. | `list(string)` | `null` | no |
| launch\_template\_id | The ID of a custom launch template to use for the EKS node group. | `string` | `null` | no |
| launch\_template\_user\_data | Use this to override just the user\_data script if you're not passing a full launch template. | `string` | `null` | no |
| launch\_template\_version | A specific version of the above specific launch template | `string` | `null` | no |
| max\_size | Maximum number of worker nodes | `number` | n/a | yes |
| min\_size | Minimum number of worker nodes | `number` | n/a | yes |
| module\_depends\_on | Can be any value desired. Module will wait for this value to be computed before creating node group. | `any` | `null` | no |
Expand Down
2 changes: 1 addition & 1 deletion README.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ usage: |2-
attributes = var.attributes
tags = var.tags
subnet_ids = module.subnets.public_subnet_ids
instance_types = var.instance_types
instance_type = var.instance_type
desired_size = var.desired_size
min_size = var.min_size
max_size = var.max_size
Expand Down
9 changes: 6 additions & 3 deletions docs/terraform.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
| Name | Version |
|------|---------|
| terraform | >= 0.12.0, < 0.14.0 |
| aws | >= 2.0, < 4.0 |
| aws | ~> 3.0 |
| local | ~> 1.3 |
| template | ~> 2.0 |

## Providers

| Name | Version |
|------|---------|
| aws | >= 2.0, < 4.0 |
| aws | ~> 3.0 |

## Inputs

Expand All @@ -34,10 +34,13 @@
| existing\_workers\_role\_policy\_arns | List of existing policy ARNs that will be attached to the workers default role on creation | `list(string)` | `[]` | no |
| existing\_workers\_role\_policy\_arns\_count | Count of existing policy ARNs that will be attached to the workers default role on creation. Needed to prevent Terraform error `count can't be computed` | `number` | `0` | no |
| id\_length\_limit | Limit `id` to this many characters.<br>Set to `0` for unlimited length.<br>Set to `null` for default, which is `0`.<br>Does not affect `id_full`. | `number` | `null` | no |
| instance\_types | Set of instance types associated with the EKS Node Group. Defaults to ["t3.medium"]. Terraform will only perform drift detection if a configuration value is provided | `list(string)` | n/a | yes |
| instance\_type | Instance type associated with the EKS Node Group. Defaults to "t3.medium". Terraform will only perform drift detection if a configuration value is provided | `string` | n/a | yes |
| kubernetes\_labels | Key-value mapping of Kubernetes labels. Only labels that are applied with the EKS API are managed by this argument. Other Kubernetes labels applied to the EKS Node Group will not be managed | `map(string)` | `{}` | no |
| kubernetes\_version | Kubernetes version. Defaults to EKS Cluster Kubernetes version. Terraform will only perform drift detection if a configuration value is provided | `string` | `null` | no |
| label\_order | The naming order of the id output and Name tag.<br>Defaults to ["namespace", "environment", "stage", "name", "attributes"].<br>You can omit any of the 5 elements, but at least one must be present. | `list(string)` | `null` | no |
| launch\_template\_id | The ID of a custom launch template to use for the EKS node group. | `string` | `null` | no |
| launch\_template\_user\_data | Use this to override just the user\_data script if you're not passing a full launch template. | `string` | `null` | no |
| launch\_template\_version | A specific version of the above specific launch template | `string` | `null` | no |
| max\_size | Maximum number of worker nodes | `number` | n/a | yes |
| min\_size | Minimum number of worker nodes | `number` | n/a | yes |
| module\_depends\_on | Can be any value desired. Module will wait for this value to be computed before creating node group. | `any` | `null` | no |
Expand Down
2 changes: 1 addition & 1 deletion examples/complete/fixtures.us-east-2.tfvars
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ enabled_cluster_log_types = ["audit"]

cluster_log_retention_period = 7

instance_types = ["t3.small"]
instance_type = "t3.small"

desired_size = 2

Expand Down
14 changes: 5 additions & 9 deletions examples/complete/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,8 @@ module "subnets" {
}

module "eks_cluster" {
source = "git::https://github.com/cloudposse/terraform-aws-eks-cluster.git?ref=tags/0.27.0"

# Temporarily retain old styel, due to circular reference
namespace = var.namespace
stage = var.stage
name = var.name
attributes = var.attributes
tags = var.tags
source = "git::https://github.com/cloudposse/terraform-aws-eks-cluster.git?ref=tags/0.28.0"

region = var.region
vpc_id = module.vpc.vpc_id
subnet_ids = module.subnets.public_subnet_ids
Expand All @@ -68,6 +62,8 @@ module "eks_cluster" {
oidc_provider_enabled = var.oidc_provider_enabled
enabled_cluster_log_types = var.enabled_cluster_log_types
cluster_log_retention_period = var.cluster_log_retention_period

context = module.this.context
}

# Ensure ordering of resource creation to eliminate the race conditions when applying the Kubernetes Auth ConfigMap.
Expand All @@ -87,7 +83,7 @@ module "eks_node_group" {

subnet_ids = module.subnets.public_subnet_ids
cluster_name = data.null_data_source.wait_for_cluster_and_kubernetes_configmap.outputs["cluster_name"]
instance_types = var.instance_types
instance_type = var.instance_type
desired_size = var.desired_size
min_size = var.min_size
max_size = var.max_size
Expand Down
24 changes: 21 additions & 3 deletions examples/complete/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ variable "disk_size" {
description = "Disk size in GiB for worker nodes. Defaults to 20. Terraform will only perform drift detection if a configuration value is provided"
}

variable "instance_types" {
type = list(string)
description = "Set of instance types associated with the EKS Node Group. Defaults to [\"t3.medium\"]. Terraform will only perform drift detection if a configuration value is provided"
variable "instance_type" {
type = string
description = "Instance type associated with the EKS Node Group. Defaults to \"t3.medium\". Terraform will only perform drift detection if a configuration value is provided"
}

variable "kubernetes_labels" {
Expand All @@ -98,3 +98,21 @@ variable "min_size" {
type = number
description = "The minimum size of the AutoScaling Group"
}

variable "launch_template_id" {
type = string
description = "The ID of a custom launch template to use for the EKS node group."
default = null
}

variable "launch_template_version" {
type = string
description = "A specific version of the above specific launch template"
default = null
}

variable "launch_template_user_data" {
type = string
description = "Use this to override just the user_data script if you're not passing a full launch template."
default = null
}
49 changes: 42 additions & 7 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
locals {
enabled = module.this.enabled

tags = merge(
var.tags,
node_group_tags = merge(
module.label.tags,
{
"kubernetes.io/cluster/${var.cluster_name}" = "owned"
},
Expand All @@ -14,6 +14,13 @@ locals {
}
)
aws_policy_prefix = format("arn:%s:iam::aws:policy", join("", data.aws_partition.current.*.partition))

# Use a custom launch_template if one was passed as an input
# Otherwise, use the default in this project
launch_template = {
id = coalesce(var.launch_template_id, aws_launch_template.default[0].id)
latest_version = coalesce(var.launch_template_version, aws_launch_template.default[0].latest_version)
}
}

module "label" {
Expand All @@ -24,12 +31,11 @@ module "label" {
# ...name-workers-blue instead of ...name-blue-workers), historically we forced "workers"
# to the end of the attribute list, so we do it again here to maintain compatibility.
attributes = compact(concat(module.this.attributes, ["workers"]))
tags = local.tags
tags = var.tags

context = module.this.context
}


data "aws_partition" "current" {
count = local.enabled ? 1 : 0
}
Expand Down Expand Up @@ -113,27 +119,56 @@ resource "aws_iam_role_policy_attachment" "existing_policies_for_eks_workers_rol
role = join("", aws_iam_role.default.*.name)
}

resource "aws_launch_template" "default" {
# We'll use this default if we aren't provided with a launch template during invocation
count = (local.enabled && (var.launch_template_id == null)) ? 1 : 0
block_device_mappings {
device_name = "/dev/xvda"

ebs {
volume_size = var.disk_size
}
}

instance_type = var.instance_type

dynamic "tag_specifications" {
for_each = ["instance", "volume", "elastic-gpu"]
content {
resource_type = tag_specifications.value
tags = local.node_group_tags
}
}

# Override the default userdata if input is provided
# If none is provided, this value defaults to 'null', which defaults to the typical EKS node group userdata
user_data = var.launch_template_user_data
}

resource "aws_eks_node_group" "default" {
count = local.enabled ? 1 : 0
cluster_name = var.cluster_name
node_group_name = module.label.id
node_role_arn = join("", aws_iam_role.default.*.arn)
subnet_ids = var.subnet_ids
ami_type = var.ami_type
disk_size = var.disk_size
instance_types = var.instance_types
labels = var.kubernetes_labels
release_version = var.ami_release_version
version = var.kubernetes_version

tags = module.label.tags
tags = local.node_group_tags

scaling_config {
desired_size = var.desired_size
max_size = var.max_size
min_size = var.min_size
}

launch_template {
id = local.launch_template.id
version = local.launch_template.latest_version
}

dynamic "remote_access" {
for_each = var.ec2_ssh_key != null && var.ec2_ssh_key != "" ? ["true"] : []
content {
Expand Down
24 changes: 21 additions & 3 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ variable "disk_size" {
default = 20
}

variable "instance_types" {
type = list(string)
description = "Set of instance types associated with the EKS Node Group. Defaults to [\"t3.medium\"]. Terraform will only perform drift detection if a configuration value is provided"
variable "instance_type" {
type = string
description = "Instance type associated with the EKS Node Group. Defaults to \"t3.medium\". Terraform will only perform drift detection if a configuration value is provided"
}

variable "kubernetes_labels" {
Expand Down Expand Up @@ -93,3 +93,21 @@ variable "module_depends_on" {
default = null
description = "Can be any value desired. Module will wait for this value to be computed before creating node group."
}

variable "launch_template_id" {
type = string
description = "The ID of a custom launch template to use for the EKS node group."
default = null
}

variable "launch_template_version" {
type = string
description = "A specific version of the above specific launch template"
default = null
}

variable "launch_template_user_data" {
type = string
description = "Use this to override just the user_data script if you're not passing a full launch template."
default = null
}
2 changes: 1 addition & 1 deletion versions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ terraform {
required_version = ">= 0.12.0, < 0.14.0"

required_providers {
aws = ">= 2.0, < 4.0"
aws = "~> 3.0"
template = "~> 2.0"
local = "~> 1.3"
}
Expand Down