-
Notifications
You must be signed in to change notification settings - Fork 25
/
main.tf
210 lines (187 loc) · 6.26 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# The cluster-infrastructure module is responsible for the AWS resources which
# constitute the EKS cluster.
#
# Any Kubernetes objects which need to be managed via Terraform belong in
# ../cluster-services, not in this module.
#
# See https://github.com/alphagov/govuk-infrastructure/blob/main/docs/architecture/decisions/0003-split-terraform-state-into-separate-aws-cluster-and-kubernetes-resource-phases.md
terraform {
cloud {
organization = "govuk"
workspaces {
tags = ["cluster-infrastructure", "eks", "aws"]
}
}
required_version = "~> 1.5"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
locals {
cluster_services_namespace = "cluster-services"
secrets_prefix = "govuk"
monitoring_namespace = "monitoring"
main_managed_node_group = {
main = {
name_prefix = var.cluster_name
# TODO: set iam_role_permissions_boundary
# TODO: apply provider default_tags to instances; might need to set launch_template_tags.
desired_size = var.workers_size_desired
max_size = var.workers_size_max
min_size = var.workers_size_min
instance_types = var.workers_instance_types
update_config = { max_unavailable = 1 }
block_device_mappings = {
xvda = {
device_name = "/dev/xvda"
ebs = {
volume_size = var.node_disk_size
volume_type = "gp3"
encrypted = true
delete_on_termination = true
}
}
}
additional_tags = {
"k8s.io/cluster-autoscaler/enabled" = "true"
"k8s.io/cluster-autoscaler/${var.cluster_name}" = "owned"
}
}
}
arm_managed_node_group = {
arm = {
ami_type = "AL2023_ARM_64_STANDARD"
name_prefix = var.cluster_name
desired_size = var.arm_workers_size_desired
max_size = var.arm_workers_size_max
min_size = var.arm_workers_size_min
instance_types = var.arm_workers_instance_types
update_config = { max_unavailable = 1 }
block_device_mappings = local.main_managed_node_group.main.block_device_mappings
taints = {
arch = {
key = "arch"
value = "arm64"
effect = "NO_SCHEDULE"
}
}
additional_tags = {
"k8s.io/cluster-autoscaler/enabled" = "true"
"k8s.io/cluster-autoscaler/${var.cluster_name}" = "owned"
}
}
}
eks_managed_node_groups = merge(local.main_managed_node_group, var.enable_arm_workers ? local.arm_managed_node_group : {})
}
provider "aws" {
region = "eu-west-1"
default_tags {
tags = {
Product = "GOV.UK"
System = "EKS cluster infrastructure"
Environment = var.govuk_environment
Owner = "[email protected]"
cluster = var.cluster_name
repository = "govuk-infrastructure"
terraform_deployment = basename(abspath(path.root))
}
}
}
data "aws_iam_policy_document" "node_assumerole" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["ec2.amazonaws.com"]
}
}
}
resource "aws_iam_role" "node" {
description = "EKS managed node group IAM role"
assume_role_policy = data.aws_iam_policy_document.node_assumerole.json
force_detach_policies = true
}
data "aws_iam_policy_document" "pull_from_ecr" {
statement {
actions = [
"ecr:GetAuthorizationToken",
"ecr:BatchCheckLayerAvailability",
"ecr:GetDownloadUrlForLayer",
"ecr:GetRepositoryPolicy",
"ecr:DescribeRepositories",
"ecr:ListImages",
"ecr:DescribeImages",
"ecr:BatchGetImage",
"ecr:BatchImportUpstreamImage",
"ecr:GetLifecyclePolicy",
"ecr:GetLifecyclePolicyPreview",
"ecr:ListTagsForResource",
"ecr:DescribeImageScanFindings"
]
effect = "Allow"
resources = ["*"]
}
}
resource "aws_iam_policy" "pull_from_ecr" {
name = "pull-from-ecr"
description = "Policy to allows EKS to pull images from ECR"
policy = data.aws_iam_policy_document.pull_from_ecr.json
}
resource "aws_iam_role_policy_attachment" "pull_from_ecr" {
policy_arn = aws_iam_policy.pull_from_ecr.arn
role = aws_iam_role.node.name
}
resource "aws_iam_role_policy_attachment" "node" {
for_each = toset([
"AmazonEKSWorkerNodePolicy",
"AmazonEKS_CNI_Policy",
"AmazonSSMManagedInstanceCore",
])
policy_arn = "arn:aws:iam::aws:policy/${each.key}"
role = aws_iam_role.node.name
}
module "eks" {
source = "terraform-aws-modules/eks/aws"
version = "~> 20.0"
cluster_name = var.cluster_name
cluster_version = var.cluster_version
subnet_ids = [for s in aws_subnet.eks_control_plane : s.id]
vpc_id = data.terraform_remote_state.infra_vpc.outputs.vpc_id
cluster_addons = {
coredns = { most_recent = true }
kube-proxy = { most_recent = true }
vpc-cni = { most_recent = true }
}
cluster_endpoint_public_access = true
cloudwatch_log_group_retention_in_days = var.cluster_log_retention_in_days
cluster_enabled_log_types = [
"api", "audit", "authenticator", "controllerManager", "scheduler"
]
cluster_encryption_config = {
provider_key_arn = aws_kms_key.eks.arn
resources = ["secrets"]
}
create_kms_key = false
kms_key_enable_default_policy = false
# We're just using the cluster primary SG as created by EKS.
create_cluster_security_group = false
create_node_security_group = false
authentication_mode = "CONFIG_MAP"
eks_managed_node_group_defaults = {
ami_type = "AL2023_x86_64_STANDARD"
capacity_type = var.workers_default_capacity_type
subnet_ids = [for s in aws_subnet.eks_private : s.id]
create_security_group = false
create_iam_role = false
iam_role_arn = aws_iam_role.node.arn
}
eks_managed_node_groups = local.eks_managed_node_groups
}
resource "aws_kms_key" "eks" {
description = "EKS Secret Encryption Key"
deletion_window_in_days = 7
enable_key_rotation = true
}