Skip to content

Commit e2ce5c9

Browse files
authored
feat: Add new addon policy for AWS load balancer controller to IRSA role (#189)
1 parent e1ce116 commit e2ce5c9

File tree

10 files changed

+312
-15
lines changed

10 files changed

+312
-15
lines changed

examples/iam-eks-role/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ Run `terraform destroy` when you don't need these resources.
2121
|------|---------|
2222
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.12.6 |
2323
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 2.23 |
24-
| <a name="requirement_random"></a> [random](#requirement\_random) | >= 2 |
24+
| <a name="requirement_random"></a> [random](#requirement\_random) | >= 2.0 |
2525

2626
## Providers
2727

2828
| Name | Version |
2929
|------|---------|
3030
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 2.23 |
31-
| <a name="provider_random"></a> [random](#provider\_random) | >= 2 |
31+
| <a name="provider_random"></a> [random](#provider\_random) | >= 2.0 |
3232

3333
## Modules
3434

examples/iam-eks-role/versions.tf

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@ terraform {
22
required_version = ">= 0.12.6"
33

44
required_providers {
5-
aws = ">= 2.23"
6-
random = ">= 2"
5+
aws = {
6+
source = "hashicorp/aws"
7+
version = ">= 2.23"
8+
}
9+
random = {
10+
source = "hashicorp/random"
11+
version = ">= 2.0"
12+
}
713
}
814
}

examples/iam-role-for-service-accounts-eks/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ No providers.
3737
| <a name="module_external_dns_irsa_role"></a> [external\_dns\_irsa\_role](#module\_external\_dns\_irsa\_role) | ../../modules/iam-role-for-service-accounts-eks | n/a |
3838
| <a name="module_irsa_role"></a> [irsa\_role](#module\_irsa\_role) | ../../modules/iam-role-for-service-accounts-eks | n/a |
3939
| <a name="module_karpenter_controller_irsa_role"></a> [karpenter\_controller\_irsa\_role](#module\_karpenter\_controller\_irsa\_role) | ../../modules/iam-role-for-service-accounts-eks | n/a |
40+
| <a name="module_load_balancer_controller_irsa_role"></a> [load\_balancer\_controller\_irsa\_role](#module\_load\_balancer\_controller\_irsa\_role) | ../../modules/iam-role-for-service-accounts-eks | n/a |
4041
| <a name="module_node_termination_handler_irsa_role"></a> [node\_termination\_handler\_irsa\_role](#module\_node\_termination\_handler\_irsa\_role) | ../../modules/iam-role-for-service-accounts-eks | n/a |
4142
| <a name="module_vpc"></a> [vpc](#module\_vpc) | terraform-aws-modules/vpc/aws | ~> 3.0 |
4243
| <a name="module_vpc_cni_ipv4_irsa_role"></a> [vpc\_cni\_ipv4\_irsa\_role](#module\_vpc\_cni\_ipv4\_irsa\_role) | ../../modules/iam-role-for-service-accounts-eks | n/a |

examples/iam-role-for-service-accounts-eks/main.tf

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ module "cluster_autoscaler_irsa_role" {
5757
oidc_providers = {
5858
ex = {
5959
provider_arn = module.eks.oidc_provider_arn
60-
namespace_service_accounts = ["default:my-app", "canary:my-app"]
60+
namespace_service_accounts = ["kube-system:cluster-autoscaler"]
6161
}
6262
}
6363

@@ -90,7 +90,7 @@ module "ebs_csi_irsa_role" {
9090
oidc_providers = {
9191
ex = {
9292
provider_arn = module.eks.oidc_provider_arn
93-
namespace_service_accounts = ["default:my-app", "canary:my-app"]
93+
namespace_service_accounts = ["kube-system:aws-ebs-csi-driver"]
9494
}
9595
}
9696

@@ -107,7 +107,7 @@ module "vpc_cni_ipv4_irsa_role" {
107107
oidc_providers = {
108108
ex = {
109109
provider_arn = module.eks.oidc_provider_arn
110-
namespace_service_accounts = ["default:my-app", "canary:my-app"]
110+
namespace_service_accounts = ["kube-system:aws-vpc-cni"]
111111
}
112112
}
113113

@@ -124,7 +124,7 @@ module "vpc_cni_ipv6_irsa_role" {
124124
oidc_providers = {
125125
ex = {
126126
provider_arn = module.eks.oidc_provider_arn
127-
namespace_service_accounts = ["default:my-app", "canary:my-app"]
127+
namespace_service_accounts = ["kube-system:aws-vpc-cni"]
128128
}
129129
}
130130

@@ -140,7 +140,7 @@ module "node_termination_handler_irsa_role" {
140140
oidc_providers = {
141141
ex = {
142142
provider_arn = module.eks.oidc_provider_arn
143-
namespace_service_accounts = ["default:my-app", "canary:my-app"]
143+
namespace_service_accounts = ["kube-system:aws-node"]
144144
}
145145
}
146146

@@ -159,7 +159,23 @@ module "karpenter_controller_irsa_role" {
159159
oidc_providers = {
160160
ex = {
161161
provider_arn = module.eks.oidc_provider_arn
162-
namespace_service_accounts = ["default:my-app", "canary:my-app"]
162+
namespace_service_accounts = ["karpenter:karpenter"]
163+
}
164+
}
165+
166+
tags = local.tags
167+
}
168+
169+
module "load_balancer_controller_irsa_role" {
170+
source = "../../modules/iam-role-for-service-accounts-eks"
171+
172+
role_name = "load_balancer_controller"
173+
attach_load_balancer_controller_policy = true
174+
175+
oidc_providers = {
176+
ex = {
177+
provider_arn = module.eks.oidc_provider_arn
178+
namespace_service_accounts = ["kube-system:aws-load-balancer-controller"]
163179
}
164180
}
165181

modules/iam-eks-role/main.tf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ resource "aws_iam_role" "this" {
7373
}
7474

7575
resource "aws_iam_role_policy_attachment" "custom" {
76-
for_each = var.create_role ? toset(var.role_policy_arns) : []
76+
for_each = toset([for arn in var.role_policy_arns : arn if var.create_role])
77+
7778
role = aws_iam_role.this[0].name
7879
policy_arn = each.key
7980
}

modules/iam-eks-role/versions.tf

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ terraform {
22
required_version = ">= 0.12.6"
33

44
required_providers {
5-
aws = ">= 2.23"
5+
aws = {
6+
source = "hashicorp/aws"
7+
version = ">= 2.23"
8+
}
69
}
710
}

modules/iam-role-for-service-accounts-eks/README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
# IAM Role for Service Accounts in EKS
22

3-
Creates an IAM role which can be assumed by AWS EKS `ServiceAccount`s with optional policies for commonly used controllers/custom resources within EKS.
3+
Creates an IAM role which can be assumed by AWS EKS `ServiceAccount`s with optional policies for commonly used controllers/custom resources within EKS. The optional policies supported include:
4+
- [Cluster Autoscaler](https://github.com/kubernetes/autoscaler/blob/master/cluster-autoscaler/cloudprovider/aws/README.md)
5+
- [External DNS](https://github.com/kubernetes-sigs/external-dns/blob/master/docs/tutorials/aws.md#iam-policy)
6+
- [EBS CSI Driver](https://github.com/kubernetes-sigs/aws-ebs-csi-driver/blob/master/docs/example-iam-policy.json)
7+
- [VPC CNI](https://docs.aws.amazon.com/eks/latest/userguide/cni-iam-role.html)
8+
- [Node Termination Hanlder](https://github.com/aws/aws-node-termination-handler#5-create-an-iam-role-for-the-pods)
9+
- [Karpenter](https://github.com/aws/karpenter/blob/main/website/content/en/preview/getting-started/cloudformation.yaml)
10+
- [Load Balancer Controller](https://github.com/kubernetes-sigs/aws-load-balancer-controller/blob/main/docs/install/iam_policy.json)
411

512
This module is intended to be used with AWS EKS. For details of how a `ServiceAccount` in EKS can assume an IAM role, see the [EKS documentation](https://docs.aws.amazon.com/eks/latest/userguide/iam-roles-for-service-accounts.html).
613

@@ -105,20 +112,23 @@ No modules.
105112
| [aws_iam_policy.ebs_csi](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
106113
| [aws_iam_policy.external_dns](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
107114
| [aws_iam_policy.karpenter_controller](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
115+
| [aws_iam_policy.load_balancer_controller](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
108116
| [aws_iam_policy.node_termination_handler](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
109117
| [aws_iam_policy.vpc_cni](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
110118
| [aws_iam_role.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
111119
| [aws_iam_role_policy_attachment.cluster_autoscaler](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
112120
| [aws_iam_role_policy_attachment.ebs_csi](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
113121
| [aws_iam_role_policy_attachment.external_dns](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
114122
| [aws_iam_role_policy_attachment.karpenter_controller](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
123+
| [aws_iam_role_policy_attachment.load_balancer_controller](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
115124
| [aws_iam_role_policy_attachment.node_termination_handler](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
116125
| [aws_iam_role_policy_attachment.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
117126
| [aws_iam_role_policy_attachment.vpc_cni](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
118127
| [aws_iam_policy_document.cluster_autoscaler](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
119128
| [aws_iam_policy_document.ebs_csi](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
120129
| [aws_iam_policy_document.external_dns](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
121130
| [aws_iam_policy_document.karpenter_controller](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
131+
| [aws_iam_policy_document.load_balancer_controller](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
122132
| [aws_iam_policy_document.node_termination_handler](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
123133
| [aws_iam_policy_document.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
124134
| [aws_iam_policy_document.vpc_cni](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
@@ -132,6 +142,7 @@ No modules.
132142
| <a name="input_attach_ebs_csi_policy"></a> [attach\_ebs\_csi\_policy](#input\_attach\_ebs\_csi\_policy) | Determines whether to attach the EBS CSI IAM policy to the role | `bool` | `false` | no |
133143
| <a name="input_attach_external_dns_policy"></a> [attach\_external\_dns\_policy](#input\_attach\_external\_dns\_policy) | Determines whether to attach the External DNS IAM policy to the role | `bool` | `false` | no |
134144
| <a name="input_attach_karpenter_controller_policy"></a> [attach\_karpenter\_controller\_policy](#input\_attach\_karpenter\_controller\_policy) | Determines whether to attach the Karpenter Controller policy to the role | `bool` | `false` | no |
145+
| <a name="input_attach_load_balancer_controller_policy"></a> [attach\_load\_balancer\_controller\_policy](#input\_attach\_load\_balancer\_controller\_policy) | Determines whether to attach the Load Balancer Controller policy to the role | `bool` | `false` | no |
135146
| <a name="input_attach_node_termination_handler_policy"></a> [attach\_node\_termination\_handler\_policy](#input\_attach\_node\_termination\_handler\_policy) | Determines whether to attach the Node Termination Handler policy to the role | `bool` | `false` | no |
136147
| <a name="input_attach_vpc_cni_policy"></a> [attach\_vpc\_cni\_policy](#input\_attach\_vpc\_cni\_policy) | Determines whether to attach the VPC CNI IAM policy to the role | `bool` | `false` | no |
137148
| <a name="input_cluster_autoscaler_cluster_ids"></a> [cluster\_autoscaler\_cluster\_ids](#input\_cluster\_autoscaler\_cluster\_ids) | List of cluster IDs to appropriately scope permissions within the Cluster Autoscaler IAM policy | `list(string)` | `[]` | no |

modules/iam-role-for-service-accounts-eks/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ resource "aws_iam_role" "this" {
3939
}
4040

4141
resource "aws_iam_role_policy_attachment" "this" {
42-
for_each = var.create_role ? toset(var.role_policy_arns) : []
42+
for_each = toset([for arn in var.role_policy_arns : arn if var.create_role])
4343

4444
role = aws_iam_role.this[0].name
4545
policy_arn = each.key

0 commit comments

Comments
 (0)