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