From ae2f8e58dbe1fc1bec1dbd6861a3fd2bc648f548 Mon Sep 17 00:00:00 2001 From: Max Williams Date: Tue, 7 May 2019 16:50:42 +0200 Subject: [PATCH] Adding new mixed type of worker group with instance overrides and mixed instances policy (#371) * Adding new mixed type of worker group with instance overrides and mixed instances policy * moving all count and lifecycle rule parameters to top/bottom * adding custom IAM parts * updating doc with new options * fixes for spot instances --- CHANGELOG.md | 3 +- README.md | 2 + aws_auth.tf | 5 +- cluster.tf | 12 +-- data.tf | 34 +++++++-- docs/spot-instances.md | 123 +++++++++++++++---------------- kubectl.tf | 2 +- local.tf | 12 +++ outputs.tf | 8 +- variables.tf | 17 +++++ workers.tf | 44 +++++------ workers_launch_template.tf | 18 ++--- workers_launch_template_mixed.tf | 122 ++++++++++++++++++++++++++++++ 13 files changed, 285 insertions(+), 117 deletions(-) create mode 100644 workers_launch_template_mixed.tf diff --git a/CHANGELOG.md b/CHANGELOG.md index e01d73e..374b54a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,9 +13,10 @@ project adheres to [Semantic Versioning](http://semver.org/). - Added support for custom service linked role for Auto Scaling group (by @voanhduy1512) - Added support for custom IAM roles for cluster and workers (by @erks) -- Add cluster arn to outputs (by @alexsn) +- Added cluster ARN to outputs (by @alexsn) - Added outputs for `workers_user_data` and `workers_default_ami_id` (by @max-rocket-internet) - Added doc about spot instances (by @max-rocket-internet) +- Added new worker group option with a mixed instances policy (by @max-rocket-internet) ### Changed diff --git a/README.md b/README.md index b1875a3..61dc4d1 100644 --- a/README.md +++ b/README.md @@ -148,6 +148,8 @@ MIT Licensed. See [LICENSE](https://github.com/terraform-aws-modules/terraform-a | worker\_create\_security\_group | Whether to create a security group for the workers or attach the workers to `worker_security_group_id`. | string | `"true"` | no | | worker\_group\_count | The number of maps contained within the worker_groups list. | string | `"1"` | no | | worker\_group\_launch\_template\_count | The number of maps contained within the worker_groups_launch_template list. | string | `"0"` | no | +| worker\_group\_launch\_template\_mixed | A list of maps defining worker group configurations to be defined using AWS Launch Templates. See workers_group_defaults for valid keys. | list | `[ { "name": "default" } ]` | no | +| worker\_group\_launch\_template\_mixed\_count | The number of maps contained within the worker_group_launch_template_mixed list. | string | `"0"` | no | | worker\_group\_tags | A map defining extra tags to be applied to the worker group ASG. | map | `{ "default": [] }` | no | | worker\_groups | A list of maps defining worker group configurations to be defined using AWS Launch Configurations. See workers_group_defaults for valid keys. | list | `[ { "name": "default" } ]` | no | | worker\_groups\_launch\_template | A list of maps defining worker group configurations to be defined using AWS Launch Templates. See workers_group_defaults for valid keys. | list | `[ { "name": "default" } ]` | no | diff --git a/aws_auth.tf b/aws_auth.tf index a86349c..a9fc899 100644 --- a/aws_auth.tf +++ b/aws_auth.tf @@ -1,10 +1,11 @@ resource "local_file" "config_map_aws_auth" { + count = "${var.write_aws_auth_config ? 1 : 0}" content = "${data.template_file.config_map_aws_auth.rendered}" filename = "${var.config_output_path}config-map-aws-auth_${var.cluster_name}.yaml" - count = "${var.write_aws_auth_config ? 1 : 0}" } resource "null_resource" "update_config_map_aws_auth" { + count = "${var.manage_aws_auth ? 1 : 0}" depends_on = ["aws_eks_cluster.this"] provisioner "local-exec" { @@ -28,8 +29,6 @@ EOS config_map_rendered = "${data.template_file.config_map_aws_auth.rendered}" endpoint = "${aws_eks_cluster.this.endpoint}" } - - count = "${var.manage_aws_auth ? 1 : 0}" } data "aws_caller_identity" "current" {} diff --git a/cluster.tf b/cluster.tf index 5877c62..dc789ee 100644 --- a/cluster.tf +++ b/cluster.tf @@ -23,14 +23,15 @@ resource "aws_eks_cluster" "this" { } resource "aws_security_group" "cluster" { + count = "${var.cluster_create_security_group ? 1 : 0}" name_prefix = "${var.cluster_name}" description = "EKS cluster security group." vpc_id = "${var.vpc_id}" tags = "${merge(var.tags, map("Name", "${var.cluster_name}-eks_cluster_sg"))}" - count = "${var.cluster_create_security_group ? 1 : 0}" } resource "aws_security_group_rule" "cluster_egress_internet" { + count = "${var.cluster_create_security_group ? 1 : 0}" description = "Allow cluster egress access to the Internet." protocol = "-1" security_group_id = "${aws_security_group.cluster.id}" @@ -38,10 +39,10 @@ resource "aws_security_group_rule" "cluster_egress_internet" { from_port = 0 to_port = 0 type = "egress" - count = "${var.cluster_create_security_group ? 1 : 0}" } resource "aws_security_group_rule" "cluster_https_worker_ingress" { + count = "${var.cluster_create_security_group ? 1 : 0}" description = "Allow pods to communicate with the EKS cluster API." protocol = "tcp" security_group_id = "${aws_security_group.cluster.id}" @@ -49,26 +50,25 @@ resource "aws_security_group_rule" "cluster_https_worker_ingress" { from_port = 443 to_port = 443 type = "ingress" - count = "${var.cluster_create_security_group ? 1 : 0}" } resource "aws_iam_role" "cluster" { + count = "${var.manage_cluster_iam_resources ? 1 : 0}" name_prefix = "${var.cluster_name}" assume_role_policy = "${data.aws_iam_policy_document.cluster_assume_role_policy.json}" permissions_boundary = "${var.permissions_boundary}" path = "${var.iam_path}" force_detach_policies = true - count = "${var.manage_cluster_iam_resources ? 1 : 0}" } resource "aws_iam_role_policy_attachment" "cluster_AmazonEKSClusterPolicy" { + count = "${var.manage_cluster_iam_resources ? 1 : 0}" policy_arn = "arn:aws:iam::aws:policy/AmazonEKSClusterPolicy" role = "${aws_iam_role.cluster.name}" - count = "${var.manage_cluster_iam_resources ? 1 : 0}" } resource "aws_iam_role_policy_attachment" "cluster_AmazonEKSServicePolicy" { + count = "${var.manage_cluster_iam_resources ? 1 : 0}" policy_arn = "arn:aws:iam::aws:policy/AmazonEKSServicePolicy" role = "${aws_iam_role.cluster.name}" - count = "${var.manage_cluster_iam_resources ? 1 : 0}" } diff --git a/data.tf b/data.tf index 5ef35bd..91e6570 100644 --- a/data.tf +++ b/data.tf @@ -58,13 +58,13 @@ data "template_file" "kubeconfig" { } data "template_file" "aws_authenticator_env_variables" { + count = "${length(var.kubeconfig_aws_authenticator_env_variables)}" + template = <