Skip to content

Commit

Permalink
Allow multiple efs on aws clusters
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgianaElena committed Jul 25, 2024
1 parent 5baf62d commit 40eeea8
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 6 deletions.
31 changes: 25 additions & 6 deletions terraform/aws/efs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ data "aws_security_group" "cluster_nodes_shared_security_group" {
}

resource "aws_efs_file_system" "homedirs" {
tags = merge(var.tags, { Name = "hub-homedirs" })
for_each = var.filestores
tags = merge(var.tags, {
Name = each.value.name_suffix == null ? "hub-homedirs" : "hub-homedirs-${each.value.name_suffix}"
})

# Transition files to a slower, cheaper backing medium 90 days
# after they were last *accessed*. They will be transferred back to regular
Expand All @@ -65,22 +68,38 @@ resource "aws_efs_file_system" "homedirs" {
}
}

locals {
fs_ids = toset(values(aws_efs_file_system.homedirs)[*].id)
fs_dns_names = toset(values(aws_efs_file_system.homedirs)[*].dns_name)

subnet_ids = toset(data.aws_subnets.cluster_node_subnets.ids)

efs_mount_targets = [
for pair in setproduct(local.subnet_ids, local.fs_ids) : {
file_system_id = pair[0]
subnet_id = pair[1]
}
]
}
resource "aws_efs_mount_target" "homedirs" {
for_each = toset(data.aws_subnets.cluster_node_subnets.ids)
for_each = tomap({
for mount_target in local.efs_mount_targets : "${mount_target.subnet_id}.${mount_target.file_system_id}" => mount_target
})

file_system_id = aws_efs_file_system.homedirs.id
subnet_id = each.key
file_system_id = each.value.file_system_id
subnet_id = each.value.subnet_id
security_groups = [data.aws_security_group.cluster_nodes_shared_security_group.id]
}

output "nfs_server_dns" {
value = aws_efs_file_system.homedirs.dns_name
value = values(aws_efs_file_system.homedirs)[*].dns_name
}

# Enable automatic backups for user homedirectories
# Documented in https://docs.aws.amazon.com/efs/latest/ug/awsbackup.html#automatic-backups
resource "aws_efs_backup_policy" "homedirs" {
file_system_id = aws_efs_file_system.homedirs.id
for_each = aws_efs_file_system.homedirs
file_system_id = each.value.id

backup_policy {
status = "ENABLED"
Expand Down
27 changes: 27 additions & 0 deletions terraform/aws/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -196,3 +196,30 @@ variable "default_budget_alert" {
A boilerplate budget alert initially setup for AWS accounts we pay the bill for.
EOT
}

variable "filestores" {
type = map(object({
name_suffix : optional(string, null),
tags : optional(map(string), {}),
}))
default = {
"filestore" : {}
}
description = <<-EOT
Deploy one or more AWS ElasticFileStores for home directories.
This provisions a managed NFS solution that can be mounted as
home directories for users. If this is not enabled, a manual or
in-cluster NFS solution must be set up.
- name-suffix: Suffix to append to the name of the FileStore. This
prevents name-clashing. Default: null.
- tags: Tags to apply to the homedir. The value is an object as we
are appending existing tags to homedir specific tags.
We use CamelCase for tag names to match AWS's tagging style.
Default tag is:
1. Name: This tag will indicate the name of the homedir.
By default, this will be set to "hub-homedirs-{name_suffix}".
EOT
}

0 comments on commit 40eeea8

Please sign in to comment.