Skip to content

Commit 21fb8d9

Browse files
josmobryantbiggs
andauthored
feat: Add support for Mountpoint S3 CSI driver to EKS IRSA (#459)
Co-authored-by: Bryant Biggs <[email protected]>
1 parent ccb4f25 commit 21fb8d9

File tree

8 files changed

+111
-3
lines changed

8 files changed

+111
-3
lines changed

.github/workflows/pre-commit.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ jobs:
3232
matrix:
3333
directory: ${{ fromJson(needs.collectInputs.outputs.directories) }}
3434
steps:
35+
# https://github.com/orgs/community/discussions/25678#discussioncomment-5242449
36+
- name: Delete huge unnecessary tools folder
37+
run: |
38+
rm -rf /opt/hostedtoolcache/CodeQL
39+
rm -rf /opt/hostedtoolcache/Java_Temurin-Hotspot_jdk
40+
rm -rf /opt/hostedtoolcache/Ruby
41+
rm -rf /opt/hostedtoolcache/go
42+
3543
- name: Checkout
3644
uses: actions/checkout@v3
3745

@@ -64,6 +72,14 @@ jobs:
6472
runs-on: ubuntu-latest
6573
needs: collectInputs
6674
steps:
75+
# https://github.com/orgs/community/discussions/25678#discussioncomment-5242449
76+
- name: Delete huge unnecessary tools folder
77+
run: |
78+
rm -rf /opt/hostedtoolcache/CodeQL
79+
rm -rf /opt/hostedtoolcache/Java_Temurin-Hotspot_jdk
80+
rm -rf /opt/hostedtoolcache/Ruby
81+
rm -rf /opt/hostedtoolcache/go
82+
6783
- name: Checkout
6884
uses: actions/checkout@v3
6985
with:

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
repos:
22
- repo: https://github.com/antonbabenko/pre-commit-terraform
3-
rev: v1.85.0
3+
rev: v1.88.0
44
hooks:
55
- id: terraform_fmt
66
- id: terraform_wrapper_module_for_each

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ Run `terraform destroy` when you don't need these resources.
5252
| <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 |
5353
| <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 |
5454
| <a name="module_load_balancer_controller_targetgroup_binding_only_irsa_role"></a> [load\_balancer\_controller\_targetgroup\_binding\_only\_irsa\_role](#module\_load\_balancer\_controller\_targetgroup\_binding\_only\_irsa\_role) | ../../modules/iam-role-for-service-accounts-eks | n/a |
55+
| <a name="module_mountpoint_s3_csi_irsa_role"></a> [mountpoint\_s3\_csi\_irsa\_role](#module\_mountpoint\_s3\_csi\_irsa\_role) | ../../modules/iam-role-for-service-accounts-eks | n/a |
5556
| <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 |
5657
| <a name="module_velero_irsa_role"></a> [velero\_irsa\_role](#module\_velero\_irsa\_role) | ../../modules/iam-role-for-service-accounts-eks | n/a |
5758
| <a name="module_vpc"></a> [vpc](#module\_vpc) | terraform-aws-modules/vpc/aws | ~> 5.0 |

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,24 @@ module "efs_csi_irsa_role" {
136136
tags = local.tags
137137
}
138138

139+
module "mountpoint_s3_csi_irsa_role" {
140+
source = "../../modules/iam-role-for-service-accounts-eks"
141+
142+
role_name = "mountpoint-s3-csi"
143+
attach_mountpoint_s3_csi_policy = true
144+
mountpoint_s3_csi_bucket_arns = ["arn:aws:s3:::mountpoint-s3-csi-bucket"]
145+
mountpoint_s3_csi_path_arns = ["arn:aws:s3:::mountpoint-s3-csi-bucket/example/*"]
146+
147+
oidc_providers = {
148+
ex = {
149+
provider_arn = module.eks.oidc_provider_arn
150+
namespace_service_accounts = ["kube-system:s3-csi-driver-sa"]
151+
}
152+
}
153+
154+
tags = local.tags
155+
}
156+
139157
module "external_dns_irsa_role" {
140158
source = "../../modules/iam-role-for-service-accounts-eks"
141159

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# IAM Role for Service Accounts in EKS
22

33
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+
45
- [Cert-Manager](https://cert-manager.io/docs/configuration/acme/dns01/route53/#set-up-an-iam-role)
56
- [Cluster Autoscaler](https://github.com/kubernetes/autoscaler/blob/master/cluster-autoscaler/cloudprovider/aws/README.md)
67
- [EBS CSI Driver](https://github.com/kubernetes-sigs/aws-ebs-csi-driver/blob/master/docs/example-iam-policy.json)
@@ -85,10 +86,10 @@ module "karpenter_irsa_role" {
8586
8687
module "eks" {
8788
source = "terraform-aws-modules/eks/aws"
88-
version = "~> 18.6"
89+
version = "~> 20.4"
8990
9091
cluster_name = "my-cluster"
91-
cluster_version = "1.21"
92+
cluster_version = "1.29"
9293
9394
vpc_id = module.vpc.vpc_id
9495
subnet_ids = module.vpc.private_subnets
@@ -135,6 +136,7 @@ No modules.
135136
| [aws_iam_policy.karpenter_controller](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
136137
| [aws_iam_policy.load_balancer_controller](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
137138
| [aws_iam_policy.load_balancer_controller_targetgroup_only](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
139+
| [aws_iam_policy.mountpoint_s3_csi](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
138140
| [aws_iam_policy.node_termination_handler](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
139141
| [aws_iam_policy.velero](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
140142
| [aws_iam_policy.vpc_cni](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
@@ -154,6 +156,7 @@ No modules.
154156
| [aws_iam_role_policy_attachment.karpenter_controller](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
155157
| [aws_iam_role_policy_attachment.load_balancer_controller](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
156158
| [aws_iam_role_policy_attachment.load_balancer_controller_targetgroup_only](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
159+
| [aws_iam_role_policy_attachment.mountpoint_s3_csi](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
157160
| [aws_iam_role_policy_attachment.node_termination_handler](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
158161
| [aws_iam_role_policy_attachment.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
159162
| [aws_iam_role_policy_attachment.velero](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
@@ -173,6 +176,7 @@ No modules.
173176
| [aws_iam_policy_document.karpenter_controller](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
174177
| [aws_iam_policy_document.load_balancer_controller](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
175178
| [aws_iam_policy_document.load_balancer_controller_targetgroup_only](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
179+
| [aws_iam_policy_document.mountpoint_s3_csi](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
176180
| [aws_iam_policy_document.node_termination_handler](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
177181
| [aws_iam_policy_document.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
178182
| [aws_iam_policy_document.velero](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
@@ -202,6 +206,7 @@ No modules.
202206
| <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 |
203207
| <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 |
204208
| <a name="input_attach_load_balancer_controller_targetgroup_binding_only_policy"></a> [attach\_load\_balancer\_controller\_targetgroup\_binding\_only\_policy](#input\_attach\_load\_balancer\_controller\_targetgroup\_binding\_only\_policy) | Determines whether to attach the Load Balancer Controller policy for the TargetGroupBinding only | `bool` | `false` | no |
209+
| <a name="input_attach_mountpoint_s3_csi_policy"></a> [attach\_mountpoint\_s3\_csi\_policy](#input\_attach\_mountpoint\_s3\_csi\_policy) | Determines whether to attach the Mountpoint S3 CSI IAM policy to the role | `bool` | `false` | no |
205210
| <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 |
206211
| <a name="input_attach_velero_policy"></a> [attach\_velero\_policy](#input\_attach\_velero\_policy) | Determines whether to attach the Velero IAM policy to the role | `bool` | `false` | no |
207212
| <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 |
@@ -227,6 +232,8 @@ No modules.
227232
| <a name="input_karpenter_tag_key"></a> [karpenter\_tag\_key](#input\_karpenter\_tag\_key) | Tag key (`{key = value}`) applied to resources launched by Karpenter through the Karpenter provisioner | `string` | `"karpenter.sh/discovery"` | no |
228233
| <a name="input_load_balancer_controller_targetgroup_arns"></a> [load\_balancer\_controller\_targetgroup\_arns](#input\_load\_balancer\_controller\_targetgroup\_arns) | List of Target groups ARNs using Load Balancer Controller | `list(string)` | <pre>[<br> "arn:aws:elasticloadbalancing:*:*:targetgroup/*/*"<br>]</pre> | no |
229234
| <a name="input_max_session_duration"></a> [max\_session\_duration](#input\_max\_session\_duration) | Maximum CLI/API session duration in seconds between 3600 and 43200 | `number` | `null` | no |
235+
| <a name="input_mountpoint_s3_csi_bucket_arns"></a> [mountpoint\_s3\_csi\_bucket\_arns](#input\_mountpoint\_s3\_csi\_bucket\_arns) | S3 bucket ARNs to allow Mountpoint S3 CSI to list buckets | `list(string)` | `[]` | no |
236+
| <a name="input_mountpoint_s3_csi_path_arns"></a> [mountpoint\_s3\_csi\_path\_arns](#input\_mountpoint\_s3\_csi\_path\_arns) | S3 path ARNs to allow Mountpoint S3 CSI driver to manage items at the provided path(s). This is required if `attach_mountpoint_s3_csi_policy = true` | `list(string)` | `[]` | no |
230237
| <a name="input_node_termination_handler_sqs_queue_arns"></a> [node\_termination\_handler\_sqs\_queue\_arns](#input\_node\_termination\_handler\_sqs\_queue\_arns) | List of SQS ARNs that contain node termination events | `list(string)` | <pre>[<br> "*"<br>]</pre> | no |
231238
| <a name="input_oidc_providers"></a> [oidc\_providers](#input\_oidc\_providers) | Map of OIDC providers where each provider map should contain the `provider_arn` and `namespace_service_accounts` | `any` | `{}` | no |
232239
| <a name="input_policy_name_prefix"></a> [policy\_name\_prefix](#input\_policy\_name\_prefix) | IAM policy name prefix | `string` | `"AmazonEKS_"` | no |

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,50 @@ resource "aws_iam_role_policy_attachment" "efs_csi" {
422422
policy_arn = aws_iam_policy.efs_csi[0].arn
423423
}
424424

425+
################################################################################
426+
# Mountpoint S3 CSI Driver Policy
427+
################################################################################
428+
429+
#https://github.com/awslabs/mountpoint-s3/blob/main/doc/CONFIGURATION.md#iam-permissions
430+
data "aws_iam_policy_document" "mountpoint_s3_csi" {
431+
count = var.create_role && var.attach_mountpoint_s3_csi_policy ? 1 : 0
432+
433+
statement {
434+
sid = "MountpointFullBucketAccess"
435+
actions = ["s3:ListBucket"]
436+
resources = coalescelist(var.mountpoint_s3_csi_bucket_arns, ["arn:${local.partition}:s3:::*"])
437+
}
438+
439+
statement {
440+
sid = "MountpointFullObjectAccess"
441+
actions = [
442+
"s3:GetObject",
443+
"s3:PutObject",
444+
"s3:AbortMultipartUpload",
445+
"s3:DeleteObject"
446+
]
447+
resources = var.mountpoint_s3_csi_path_arns
448+
}
449+
}
450+
451+
resource "aws_iam_policy" "mountpoint_s3_csi" {
452+
count = var.create_role && var.attach_mountpoint_s3_csi_policy ? 1 : 0
453+
454+
name_prefix = "${var.policy_name_prefix}Mountpoint_S3_CSI-"
455+
path = var.role_path
456+
description = "Mountpoint S3 CSI driver policy to allow management of S3"
457+
policy = data.aws_iam_policy_document.mountpoint_s3_csi[0].json
458+
459+
tags = var.tags
460+
}
461+
462+
resource "aws_iam_role_policy_attachment" "mountpoint_s3_csi" {
463+
count = var.create_role && var.attach_mountpoint_s3_csi_policy ? 1 : 0
464+
465+
role = aws_iam_role.this[0].name
466+
policy_arn = aws_iam_policy.mountpoint_s3_csi[0].arn
467+
}
468+
425469
################################################################################
426470
# External DNS Policy
427471
################################################################################

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,25 @@ variable "attach_efs_csi_policy" {
145145
default = false
146146
}
147147

148+
# S3 CSI
149+
variable "attach_mountpoint_s3_csi_policy" {
150+
description = "Determines whether to attach the Mountpoint S3 CSI IAM policy to the role"
151+
type = bool
152+
default = false
153+
}
154+
155+
variable "mountpoint_s3_csi_bucket_arns" {
156+
description = "S3 bucket ARNs to allow Mountpoint S3 CSI to list buckets"
157+
type = list(string)
158+
default = []
159+
}
160+
161+
variable "mountpoint_s3_csi_path_arns" {
162+
description = "S3 path ARNs to allow Mountpoint S3 CSI driver to manage items at the provided path(s). This is required if `attach_mountpoint_s3_csi_policy = true`"
163+
type = list(string)
164+
default = []
165+
}
166+
148167
# External DNS
149168
variable "attach_external_dns_policy" {
150169
description = "Determines whether to attach the External DNS IAM policy to the role"

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ module "wrapper" {
2121
attach_karpenter_controller_policy = try(each.value.attach_karpenter_controller_policy, var.defaults.attach_karpenter_controller_policy, false)
2222
attach_load_balancer_controller_policy = try(each.value.attach_load_balancer_controller_policy, var.defaults.attach_load_balancer_controller_policy, false)
2323
attach_load_balancer_controller_targetgroup_binding_only_policy = try(each.value.attach_load_balancer_controller_targetgroup_binding_only_policy, var.defaults.attach_load_balancer_controller_targetgroup_binding_only_policy, false)
24+
attach_mountpoint_s3_csi_policy = try(each.value.attach_mountpoint_s3_csi_policy, var.defaults.attach_mountpoint_s3_csi_policy, false)
2425
attach_node_termination_handler_policy = try(each.value.attach_node_termination_handler_policy, var.defaults.attach_node_termination_handler_policy, false)
2526
attach_velero_policy = try(each.value.attach_velero_policy, var.defaults.attach_velero_policy, false)
2627
attach_vpc_cni_policy = try(each.value.attach_vpc_cni_policy, var.defaults.attach_vpc_cni_policy, false)
@@ -46,6 +47,8 @@ module "wrapper" {
4647
karpenter_tag_key = try(each.value.karpenter_tag_key, var.defaults.karpenter_tag_key, "karpenter.sh/discovery")
4748
load_balancer_controller_targetgroup_arns = try(each.value.load_balancer_controller_targetgroup_arns, var.defaults.load_balancer_controller_targetgroup_arns, ["arn:aws:elasticloadbalancing:*:*:targetgroup/*/*"])
4849
max_session_duration = try(each.value.max_session_duration, var.defaults.max_session_duration, null)
50+
mountpoint_s3_csi_bucket_arns = try(each.value.mountpoint_s3_csi_bucket_arns, var.defaults.mountpoint_s3_csi_bucket_arns, [])
51+
mountpoint_s3_csi_path_arns = try(each.value.mountpoint_s3_csi_path_arns, var.defaults.mountpoint_s3_csi_path_arns, [])
4952
node_termination_handler_sqs_queue_arns = try(each.value.node_termination_handler_sqs_queue_arns, var.defaults.node_termination_handler_sqs_queue_arns, ["*"])
5053
oidc_providers = try(each.value.oidc_providers, var.defaults.oidc_providers, {})
5154
policy_name_prefix = try(each.value.policy_name_prefix, var.defaults.policy_name_prefix, "AmazonEKS_")

0 commit comments

Comments
 (0)