Skip to content

Commit 407e8dc

Browse files
Merge pull request #10 from truefoundry/hl/update-efs
feat(efs): add EFS file system policy and update module source
2 parents 8f9f3ef + acf862c commit 407e8dc

File tree

6 files changed

+105
-52
lines changed

6 files changed

+105
-52
lines changed

README.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,17 @@ Truefoundry AWS EFS Module
1919

2020
| Name | Source | Version |
2121
|------|--------|---------|
22-
| <a name="module_efs"></a> [efs](#module\_efs) | terraform-aws-modules/efs/aws | 1.6.3 |
22+
| <a name="module_efs"></a> [efs](#module\_efs) | cloudposse/efs/aws | 1.1.0 |
2323
| <a name="module_iam_assumable_role_admin"></a> [iam\_assumable\_role\_admin](#module\_iam\_assumable\_role\_admin) | terraform-aws-modules/iam/aws//modules/iam-assumable-role-with-oidc | 5.27.0 |
2424

2525
## Resources
2626

2727
| Name | Type |
2828
|------|------|
29+
| [aws_efs_file_system_policy.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/efs_file_system_policy) | resource |
2930
| [aws_iam_policy.efs](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
3031
| [aws_iam_policy_document.efs](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
31-
| [aws_subnet.selected](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/subnet) | data source |
32+
| [aws_iam_policy_document.efs_file_system_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
3233

3334
## Inputs
3435

@@ -42,7 +43,8 @@ Truefoundry AWS EFS Module
4243
| <a name="input_k8s_service_account_namespace"></a> [k8s\_service\_account\_namespace](#input\_k8s\_service\_account\_namespace) | The k8s efs namespace | `string` | n/a | yes |
4344
| <a name="input_performance_mode"></a> [performance\_mode](#input\_performance\_mode) | the performance mode for EFS | `string` | n/a | yes |
4445
| <a name="input_private_subnets_cidrs"></a> [private\_subnets\_cidrs](#input\_private\_subnets\_cidrs) | List of CIDR of private subnets | `list(string)` | n/a | yes |
45-
| <a name="input_private_subnets_id"></a> [private\_subnets\_id](#input\_private\_subnets\_id) | List of IDs of private subnets | `list(string)` | n/a | yes |
46+
| <a name="input_private_subnets_id"></a> [private\_subnets\_id](#input\_private\_subnets\_id) | List of private subnet IDs | `list(string)` | n/a | yes |
47+
| <a name="input_region"></a> [region](#input\_region) | The region where the EFS will be provisioned | `string` | n/a | yes |
4648
| <a name="input_tags"></a> [tags](#input\_tags) | AWS Tags common to all the resources created | `map(string)` | `{}` | no |
4749
| <a name="input_throughput_mode"></a> [throughput\_mode](#input\_throughput\_mode) | the throughput mode for EFS | `string` | n/a | yes |
4850
| <a name="input_vpc_id"></a> [vpc\_id](#input\_vpc\_id) | ID of the VPC where the cluster and its nodes will be provisioned | `string` | `null` | no |

UPGRADE.md

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Upgrade Guide
2+
3+
This guide helps you upgrade the EFS module from version v0.3.5 and below to newer versions.
4+
5+
## From v0.3.5 to v0.4.0
6+
7+
### Breaking Changes
8+
9+
1. The EFS filesystem resource name has been changed from [aws_efs_module](https://registry.terraform.io/modules/terraform-aws-modules/efs/aws/latest) to [cloudposse_efs](https://registry.terraform.io/modules/cloudposse/efs/aws/latest), due to count dependency on data block inputs. This helps us generate a one-shot plan for the whole Truefoundry installation.
10+
11+
### Required Actions
12+
13+
1. First, create a backup of your terraform state:
14+
15+
```bash
16+
# For local state
17+
terraform state pull > terraform.tfstate.backup.$(date +%Y%m%d_%H%M%S)
18+
```
19+
20+
2. Before running `terraform plan`, you need to migrate the state of the EFS resources to prevent destruction and recreation. Execute the following commands in order:
21+
22+
```bash
23+
# Move EFS file system
24+
terraform state mv 'module.efs.module.efs.aws_efs_file_system.default[0]' 'module.efs.module.efs.aws_efs_file_system.this[0]'
25+
26+
# Move mount targets
27+
terraform state mv 'module.efs.module.efs.aws_efs_mount_target.default[0]' 'module.efs.module.efs.aws_efs_mount_target.this[0]'
28+
terraform state mv 'module.efs.module.efs.aws_efs_mount_target.default[1]' 'module.efs.module.efs.aws_efs_mount_target.this[1]'
29+
terraform state mv 'module.efs.module.efs.aws_efs_mount_target.default[2]' 'module.efs.module.efs.aws_efs_mount_target.this[2]'
30+
31+
# Move file system policy
32+
terraform state mv 'module.efs.aws_efs_file_system_policy.default' 'module.efs.aws_efs_file_system_policy.this'
33+
34+
# Move backup policy
35+
terraform state mv 'module.efs.module.efs.aws_efs_backup_policy.default[0]' 'module.efs.module.efs.aws_efs_backup_policy.policy[0]'
36+
37+
### Verification Steps
38+
39+
1. Run all the state move commands mentioned above
40+
2. Execute `terraform plan`
41+
3. Verify that the plan does not show destruction of any EFS resources
42+
4. The plan should only show changes related to the resource name changes
43+
5. If the plan shows EFS resource destruction, DO NOT APPLY and review the state migration steps
44+
45+
### Notes
46+
47+
- Always backup your Terraform state before performing any state migrations
48+
- Test these changes in a non-production environment first
49+
- If you encounter any issues during the upgrade, please refer to the module documentation or open an issue in the repository
50+
- Make sure to execute the state move commands in the order specified above
51+
- If you have a different number of mount targets, adjust the mount target migration commands accordingly

efs.tf

+40-36
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ resource "aws_iam_policy" "efs" {
44
policy = data.aws_iam_policy_document.efs.json
55
tags = local.tags
66
}
7+
8+
resource "aws_efs_file_system_policy" "this" {
9+
file_system_id = module.efs.id
10+
bypass_policy_lockout_safety_check = false
11+
policy = data.aws_iam_policy_document.efs_file_system_policy.json
12+
}
13+
14+
715
# https://github.com/kubernetes-sigs/aws-efs-csi-driver/blob/master/docs/iam-policy-example.json
816
data "aws_iam_policy_document" "efs" {
917
statement {
@@ -14,9 +22,7 @@ data "aws_iam_policy_document" "efs" {
1422
"elasticfilesystem:DescribeMountTargets",
1523
"ec2:DescribeAvailabilityZones"
1624
]
17-
resources = [
18-
"*"
19-
]
25+
resources = ["*"]
2026
}
2127
statement {
2228
effect = "Allow"
@@ -62,44 +68,42 @@ data "aws_iam_policy_document" "efs" {
6268
}
6369
}
6470

71+
# EFS file system policy
72+
data "aws_iam_policy_document" "efs_file_system_policy" {
73+
statement {
74+
sid = "EFS-CSI-Driver-Access"
75+
effect = "Allow"
76+
actions = ["elasticfilesystem:ClientMount", "elasticfilesystem:ClientWrite", "elasticfilesystem:ClientRootAccess"]
77+
principals {
78+
type = "AWS"
79+
identifiers = [var.efs_node_iam_role_arn]
80+
}
81+
condition {
82+
test = "Bool"
83+
values = ["true"]
84+
variable = "elasticfilesystem:AccessedViaMountTarget"
85+
}
86+
}
87+
}
88+
6589
module "efs" {
66-
source = "terraform-aws-modules/efs/aws"
67-
version = "1.6.3"
90+
source = "cloudposse/efs/aws"
91+
version = "1.1.0"
92+
93+
region = var.region
94+
vpc_id = var.vpc_id
95+
subnets = var.private_subnets_id
96+
allow_all_egress = false
6897

69-
name = "${var.cluster_name}-efs"
98+
allowed_cidr_blocks = var.private_subnets_cidrs
99+
create_security_group = true
100+
name = "${var.cluster_name}-efs"
70101

71-
mount_targets = local.mount_targets
72102
security_group_description = "${var.cluster_name} EFS"
73-
security_group_vpc_id = var.vpc_id
74-
attach_policy = true
75103
bypass_policy_lockout_safety_check = false
76-
policy_statements = [
77-
{
78-
sid = "EFS-CSI-Driver-Access"
79-
actions = ["elasticfilesystem:ClientMount", "elasticfilesystem:ClientWrite", "elasticfilesystem:ClientRootAccess"]
80-
principals = [
81-
{
82-
type = "AWS"
83-
identifiers = [var.efs_node_iam_role_arn]
84-
}
85-
]
86-
conditions = [{
87-
test = "Bool"
88-
values = ["true"]
89-
variable = "elasticfilesystem:AccessedViaMountTarget"
90-
}]
91-
}
92-
]
93-
throughput_mode = var.throughput_mode
94-
performance_mode = var.performance_mode
95-
enable_backup_policy = var.enable_backup_policy
96-
security_group_rules = {
97-
vpc = {
98-
# relying on the defaults provdied for EFS/NFS (2049/TCP + ingress)
99-
description = "NFS ingress from VPC private subnets"
100-
cidr_blocks = var.private_subnets_cidrs
101-
}
102-
}
104+
throughput_mode = var.throughput_mode
105+
performance_mode = var.performance_mode
106+
efs_backup_policy_enabled = var.enable_backup_policy
103107

104108
tags = merge(
105109
local.tags

iam-sa.tf

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ module "iam_assumable_role_admin" {
1414
aws_iam_policy.efs.arn
1515
]
1616
tags = local.tags
17-
}
17+
}

locals.tf

+1-10
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
data "aws_subnet" "selected" {
2-
count = length(var.private_subnets_id)
3-
id = var.private_subnets_id[count.index]
4-
}
5-
61
locals {
72
tags = merge(
83
{
@@ -12,8 +7,4 @@ locals {
127
},
138
var.tags
149
)
15-
subnets = merge({ for _, v in data.aws_subnet.selected : v.availability_zone => v.id... })
16-
mount_targets = merge({ for k, v in local.subnets : k => {
17-
subnet_id = v[0]
18-
} })
19-
}
10+
}

variables.tf

+7-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ variable "vpc_id" {
1010
}
1111

1212
variable "private_subnets_id" {
13-
description = "List of IDs of private subnets"
13+
description = "List of private subnet IDs"
1414
type = list(string)
1515
}
1616

@@ -69,4 +69,9 @@ variable "tags" {
6969
type = map(string)
7070
default = {}
7171
description = "AWS Tags common to all the resources created"
72-
}
72+
}
73+
74+
variable "region" {
75+
description = "The region where the EFS will be provisioned"
76+
type = string
77+
}

0 commit comments

Comments
 (0)