|
| 1 | +provider "aws" { |
| 2 | + region = var.region |
| 3 | +} |
| 4 | + |
| 5 | +data "aws_caller_identity" "current" {} |
| 6 | + |
| 7 | +data "aws_eks_cluster" "cluster" { |
| 8 | + name = module.eks.cluster_id |
| 9 | +} |
| 10 | + |
| 11 | +data "aws_eks_cluster_auth" "cluster" { |
| 12 | + name = module.eks.cluster_id |
| 13 | +} |
| 14 | + |
| 15 | +provider "kubernetes" { |
| 16 | + host = data.aws_eks_cluster.cluster.endpoint |
| 17 | + cluster_ca_certificate = base64decode(data.aws_eks_cluster.cluster.certificate_authority.0.data) |
| 18 | + token = data.aws_eks_cluster_auth.cluster.token |
| 19 | + load_config_file = false |
| 20 | +} |
| 21 | + |
| 22 | +provider "helm" { |
| 23 | + kubernetes { |
| 24 | + config_path = module.eks.kubeconfig_filename |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +data "aws_availability_zones" "available" { |
| 29 | +} |
| 30 | + |
| 31 | +locals { |
| 32 | + cluster_name = "test-refresh-${random_string.suffix.result}" |
| 33 | +} |
| 34 | + |
| 35 | +resource "random_string" "suffix" { |
| 36 | + length = 8 |
| 37 | + special = false |
| 38 | +} |
| 39 | + |
| 40 | +module "vpc" { |
| 41 | + source = "terraform-aws-modules/vpc/aws" |
| 42 | + version = "~> 3.0.0" |
| 43 | + |
| 44 | + name = local.cluster_name |
| 45 | + cidr = "10.0.0.0/16" |
| 46 | + azs = data.aws_availability_zones.available.names |
| 47 | + public_subnets = ["10.0.4.0/24", "10.0.5.0/24", "10.0.6.0/24"] |
| 48 | + enable_dns_hostnames = true |
| 49 | +} |
| 50 | + |
| 51 | +data "aws_iam_policy_document" "node_term" { |
| 52 | + statement { |
| 53 | + effect = "Allow" |
| 54 | + actions = [ |
| 55 | + "ec2:DescribeInstances", |
| 56 | + "autoscaling:DescribeAutoScalingInstances", |
| 57 | + "autoscaling:DescribeTags", |
| 58 | + ] |
| 59 | + resources = [ |
| 60 | + "*", |
| 61 | + ] |
| 62 | + } |
| 63 | + statement { |
| 64 | + effect = "Allow" |
| 65 | + actions = [ |
| 66 | + "autoscaling:CompleteLifecycleAction", |
| 67 | + ] |
| 68 | + resources = module.eks.workers_asg_arns |
| 69 | + } |
| 70 | + statement { |
| 71 | + effect = "Allow" |
| 72 | + actions = [ |
| 73 | + "sqs:DeleteMessage", |
| 74 | + "sqs:ReceiveMessage" |
| 75 | + ] |
| 76 | + resources = [ |
| 77 | + module.node_term_sqs.sqs_queue_arn |
| 78 | + ] |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +resource "aws_iam_policy" "node_term" { |
| 83 | + name = "node-term-${local.cluster_name}" |
| 84 | + policy = data.aws_iam_policy_document.node_term.json |
| 85 | +} |
| 86 | + |
| 87 | +resource "aws_iam_role_policy_attachment" "node_term_policy" { |
| 88 | + policy_arn = aws_iam_policy.node_term.arn |
| 89 | + role = module.eks.worker_iam_role_name |
| 90 | +} |
| 91 | + |
| 92 | +data "aws_iam_policy_document" "node_term_events" { |
| 93 | + statement { |
| 94 | + effect = "Allow" |
| 95 | + principals { |
| 96 | + type = "Service" |
| 97 | + identifiers = [ |
| 98 | + "events.amazonaws.com", |
| 99 | + "sqs.amazonaws.com", |
| 100 | + ] |
| 101 | + } |
| 102 | + actions = [ |
| 103 | + "sqs:SendMessage", |
| 104 | + ] |
| 105 | + resources = [ |
| 106 | + "arn:aws:sqs:${var.region}:${data.aws_caller_identity.current.account_id}:${local.cluster_name}", |
| 107 | + ] |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +module "node_term_sqs" { |
| 112 | + source = "terraform-aws-modules/sqs/aws" |
| 113 | + version = "~> 3.0.0" |
| 114 | + name = local.cluster_name |
| 115 | + message_retention_seconds = 300 |
| 116 | + policy = data.aws_iam_policy_document.node_term_events.json |
| 117 | +} |
| 118 | + |
| 119 | +resource "aws_cloudwatch_event_rule" "node_term_event_rule" { |
| 120 | + name = "${local.cluster_name}-nth-rule" |
| 121 | + description = "Node termination event rule" |
| 122 | + event_pattern = jsonencode( |
| 123 | + { |
| 124 | + "source" : [ |
| 125 | + "aws.autoscaling" |
| 126 | + ], |
| 127 | + "detail-type" : [ |
| 128 | + "EC2 Instance-terminate Lifecycle Action" |
| 129 | + ] |
| 130 | + "resources" : module.eks.workers_asg_arns |
| 131 | + } |
| 132 | + ) |
| 133 | +} |
| 134 | + |
| 135 | +resource "aws_cloudwatch_event_target" "node_term_event_target" { |
| 136 | + rule = aws_cloudwatch_event_rule.node_term_event_rule.name |
| 137 | + target_id = "ANTHandler" |
| 138 | + arn = module.node_term_sqs.sqs_queue_arn |
| 139 | +} |
| 140 | + |
| 141 | +module "node_term_role" { |
| 142 | + source = "terraform-aws-modules/iam/aws//modules/iam-assumable-role-with-oidc" |
| 143 | + version = "4.1.0" |
| 144 | + create_role = true |
| 145 | + role_description = "IRSA role for ANTH, cluster ${local.cluster_name}" |
| 146 | + role_name_prefix = local.cluster_name |
| 147 | + provider_url = replace(module.eks.cluster_oidc_issuer_url, "https://", "") |
| 148 | + role_policy_arns = [aws_iam_policy.node_term.arn] |
| 149 | + oidc_fully_qualified_subjects = ["system:serviceaccount:${var.namespace}:${var.serviceaccount}"] |
| 150 | +} |
| 151 | + |
| 152 | +resource "helm_release" "anth" { |
| 153 | + depends_on = [ |
| 154 | + module.eks |
| 155 | + ] |
| 156 | + |
| 157 | + name = "aws-node-termination-handler" |
| 158 | + namespace = var.namespace |
| 159 | + repository = "https://aws.github.io/eks-charts" |
| 160 | + chart = "aws-node-termination-handler" |
| 161 | + version = var.aws_node_termination_handler_chart_version |
| 162 | + create_namespace = true |
| 163 | + |
| 164 | + set { |
| 165 | + name = "awsRegion" |
| 166 | + value = var.region |
| 167 | + } |
| 168 | + set { |
| 169 | + name = "serviceAccount.name" |
| 170 | + value = var.serviceaccount |
| 171 | + } |
| 172 | + set { |
| 173 | + name = "serviceAccount.annotations.eks\\.amazonaws\\.com/role-arn" |
| 174 | + value = module.node_term_role.iam_role_arn |
| 175 | + type = "string" |
| 176 | + } |
| 177 | + set { |
| 178 | + name = "enableSqsTerminationDraining" |
| 179 | + value = "true" |
| 180 | + } |
| 181 | + set { |
| 182 | + name = "queueURL" |
| 183 | + value = module.node_term_sqs.sqs_queue_id |
| 184 | + } |
| 185 | + set { |
| 186 | + name = "logLevel" |
| 187 | + value = "DEBUG" |
| 188 | + } |
| 189 | +} |
| 190 | + |
| 191 | +// Creating the lifecycle-hook outside of the ASG resource's |
| 192 | +// `initial_lifecycle_hook` ensures that node termination does not require the |
| 193 | +// lifecycle action to be completed, and thus allows the ASG to be destroyed cleanly. |
| 194 | +resource "aws_autoscaling_lifecycle_hook" "node_term" { |
| 195 | + name = "node_term-${local.cluster_name}" |
| 196 | + autoscaling_group_name = module.eks.workers_asg_names[0] |
| 197 | + lifecycle_transition = "autoscaling:EC2_INSTANCE_TERMINATING" |
| 198 | + heartbeat_timeout = 300 |
| 199 | + default_result = "CONTINUE" |
| 200 | +} |
| 201 | + |
| 202 | +module "eks" { |
| 203 | + source = "../.." |
| 204 | + cluster_name = local.cluster_name |
| 205 | + cluster_version = "1.19" |
| 206 | + subnets = module.vpc.public_subnets |
| 207 | + vpc_id = module.vpc.vpc_id |
| 208 | + enable_irsa = true |
| 209 | + worker_groups_launch_template = [ |
| 210 | + { |
| 211 | + name = "refresh" |
| 212 | + asg_max_size = 2 |
| 213 | + asg_desired_capacity = 2 |
| 214 | + instance_refresh_enabled = true |
| 215 | + instance_refresh_triggers = ["tag"] |
| 216 | + public_ip = true |
| 217 | + metadata_http_put_response_hop_limit = 3 |
| 218 | + tags = [ |
| 219 | + { |
| 220 | + key = "aws-node-termination-handler/managed" |
| 221 | + value = "" |
| 222 | + propagate_at_launch = true |
| 223 | + }, |
| 224 | + { |
| 225 | + key = "foo" |
| 226 | + value = "buzz" |
| 227 | + propagate_at_launch = true |
| 228 | + }, |
| 229 | + ] |
| 230 | + }, |
| 231 | + ] |
| 232 | +} |
| 233 | + |
0 commit comments