Skip to content

Commit 6f43768

Browse files
authored
Merge pull request #441 from alphagov/sengi/lb-controller-iam
Use iam-assumable-role module in aws_lb_controller_iam.tf
2 parents 6709e3f + dcd34c7 commit 6f43768

File tree

4 files changed

+25
-40
lines changed

4 files changed

+25
-40
lines changed

terraform/deployments/cluster-infrastructure/aws_lb_controller_iam.tf

+15-38
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,21 @@
55
# ../cluster-services/aws_lb_controller.tf. See
66
# https://github.com/alphagov/govuk-infrastructure/blob/main/docs/architecture/decisions/0003-split-terraform-state-into-separate-aws-cluster-and-kubernetes-resource-phases.md#decision for rationale.
77

8+
locals {
9+
aws_lb_controller_service_account_name = "aws-load-balancer-controller"
10+
}
11+
12+
module "aws_lb_controller_iam_role" {
13+
source = "terraform-aws-modules/iam/aws//modules/iam-assumable-role-with-oidc"
14+
version = "4.3.0"
15+
create_role = true
16+
role_name = "${local.aws_lb_controller_service_account_name}-${var.cluster_name}"
17+
role_description = "Role for the AWS Load Balancer Controller. Corresponds to ${local.aws_lb_controller_service_account_name} k8s ServiceAccount."
18+
provider_url = local.cluster_oidc_issuer
19+
role_policy_arns = [aws_iam_policy.aws_lb_controller.arn]
20+
oidc_fully_qualified_subjects = ["system:serviceaccount:${local.cluster_services_namespace}:${local.aws_lb_controller_service_account_name}"]
21+
}
22+
823
resource "aws_iam_policy" "aws_lb_controller" {
924
name = "AWSLoadBalancerController-${var.cluster_name}"
1025
description = "Allow AWS Load Balancer Controller to manage ALBs/NLBs etc."
@@ -220,41 +235,3 @@ resource "aws_iam_policy" "aws_lb_controller" {
220235
]
221236
})
222237
}
223-
224-
locals {
225-
aws_lb_controller_service_account_name = "aws-load-balancer-controller"
226-
227-
# module.eks.cluster_oidc_issuer_url is a full URL, e.g.
228-
# "https://oidc.eks.eu-west-1.amazonaws.com/id/B4378A8EBD334FEEFDF3BCB6D0E612C6"
229-
# but the string to which IAM compares this lacks the protocol part, so we
230-
# have to strip the "https://" when we construct the trust policy
231-
# (assume-role policy).
232-
cluster_oidc_issuer = replace(module.eks.cluster_oidc_issuer_url, "https://", "")
233-
}
234-
235-
resource "aws_iam_role" "aws_lb_controller" {
236-
name = "AWSLoadBalancerController-${var.cluster_name}"
237-
description = "Role for the AWS Load Balancer Controller. Corresponds to ${local.aws_lb_controller_service_account_name} k8s ServiceAccount."
238-
assume_role_policy = jsonencode({
239-
"Version" : "2012-10-17",
240-
"Statement" : [
241-
{
242-
"Effect" : "Allow",
243-
"Principal" : {
244-
"Federated" : module.eks.oidc_provider_arn
245-
},
246-
"Action" : "sts:AssumeRoleWithWebIdentity",
247-
"Condition" : {
248-
"StringEquals" : {
249-
"${local.cluster_oidc_issuer}:sub" : "system:serviceaccount:kube-system:${local.aws_lb_controller_service_account_name}"
250-
}
251-
}
252-
}
253-
]
254-
})
255-
}
256-
257-
resource "aws_iam_role_policy_attachment" "aws_lb_controller" {
258-
role = aws_iam_role.aws_lb_controller.name
259-
policy_arn = aws_iam_policy.aws_lb_controller.arn
260-
}

terraform/deployments/cluster-infrastructure/main.tf

+8
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ terraform {
2121
locals {
2222
cluster_services_namespace = "cluster-services"
2323
secrets_prefix = "govuk"
24+
25+
# module.eks.cluster_oidc_issuer_url is a full URL, e.g.
26+
# "https://oidc.eks.eu-west-1.amazonaws.com/id/B4378A8EBD334FEEFDF3BCB6D0E612C6"
27+
# but the string to which IAM compares this lacks the protocol part, so we
28+
# have to strip the "https://" when we construct the trust policy
29+
# (assume-role policy).
30+
cluster_oidc_issuer = replace(module.eks.cluster_oidc_issuer_url, "https://", "")
31+
2432
default_tags = {
2533
cluster = var.cluster_name
2634
project = "replatforming"

terraform/deployments/cluster-infrastructure/outputs.tf

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ output "external_secrets_role_arn" {
6565

6666
output "aws_lb_controller_role_arn" {
6767
description = "IAM role ARN corresponding to the k8s service account for the AWS Load Balancer Controller."
68-
value = aws_iam_role.aws_lb_controller.arn
68+
value = module.aws_lb_controller_iam_role.iam_role_arn
6969
}
7070

7171
output "aws_lb_controller_service_account_name" {

terraform/deployments/cluster-services/aws_lb_controller.tf

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ resource "helm_release" "aws_lb_controller" {
1010
repository = "https://aws.github.io/eks-charts"
1111
chart = "aws-load-balancer-controller"
1212
version = "1.2.6" # TODO: Dependabot or equivalent so this doesn't get neglected.
13-
namespace = "kube-system"
13+
namespace = local.services_ns
1414
values = [yamlencode({
1515
clusterName = data.terraform_remote_state.cluster_infrastructure.outputs.cluster_id
1616
serviceAccount = {

0 commit comments

Comments
 (0)