|
| 1 | +data "aws_availability_zones" "available" {} |
| 2 | + |
| 3 | +locals { |
| 4 | + vpc_cidr = "10.0.0.0/16" |
| 5 | + azs = slice(data.aws_availability_zones.available.names, 0, 3) |
| 6 | +} |
| 7 | + |
| 8 | +module "fixtures" { |
| 9 | + source = "../fixtures" |
| 10 | +} |
| 11 | + |
| 12 | +module "vpc" { |
| 13 | + source = "terraform-aws-modules/vpc/aws" |
| 14 | + version = "~> 5.0" |
| 15 | + |
| 16 | + azs = local.azs |
| 17 | + cidr = local.vpc_cidr |
| 18 | + name = module.fixtures.output_function_name |
| 19 | + private_subnets = [for k, v in local.azs : cidrsubnet(local.vpc_cidr, 8, k)] |
| 20 | +} |
| 21 | + |
| 22 | +resource "aws_security_group" "efs" { |
| 23 | + name = "${module.fixtures.output_function_name}-efs" |
| 24 | + vpc_id = module.vpc.vpc_id |
| 25 | + |
| 26 | + ingress { |
| 27 | + from_port = 2049 |
| 28 | + to_port = 2049 |
| 29 | + protocol = "tcp" |
| 30 | + self = true |
| 31 | + } |
| 32 | + |
| 33 | + egress { |
| 34 | + from_port = 2049 |
| 35 | + to_port = 2049 |
| 36 | + protocol = "tcp" |
| 37 | + self = true |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +resource "aws_efs_file_system" "this" { |
| 42 | + creation_token = module.fixtures.output_function_name |
| 43 | + encrypted = true |
| 44 | + |
| 45 | + tags = { |
| 46 | + Name = module.fixtures.output_function_name |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +resource "aws_efs_mount_target" "this" { |
| 51 | + count = length(module.vpc.private_subnets) |
| 52 | + |
| 53 | + file_system_id = aws_efs_file_system.this.id |
| 54 | + subnet_id = module.vpc.private_subnets[count.index] |
| 55 | + security_groups = [aws_security_group.efs.id] |
| 56 | +} |
| 57 | + |
| 58 | +resource "aws_efs_access_point" "this" { |
| 59 | + file_system_id = aws_efs_file_system.this.id |
| 60 | + |
| 61 | + posix_user { |
| 62 | + gid = 1000 |
| 63 | + uid = 1000 |
| 64 | + } |
| 65 | + |
| 66 | + root_directory { |
| 67 | + path = "/lambda" |
| 68 | + |
| 69 | + creation_info { |
| 70 | + owner_gid = 1000 |
| 71 | + owner_uid = 1000 |
| 72 | + permissions = "755" |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +module "lambda" { |
| 78 | + source = "../../" |
| 79 | + |
| 80 | + architectures = ["arm64"] |
| 81 | + description = "Example AWS Lambda function with EFS file system." |
| 82 | + filename = module.fixtures.output_path |
| 83 | + function_name = module.fixtures.output_function_name |
| 84 | + handler = "index.handler" |
| 85 | + memory_size = 128 |
| 86 | + runtime = "nodejs22.x" |
| 87 | + publish = false |
| 88 | + snap_start = false |
| 89 | + source_code_hash = module.fixtures.output_base64sha256 |
| 90 | + timeout = 30 |
| 91 | + |
| 92 | + file_system_config = { |
| 93 | + arn = aws_efs_access_point.this.arn |
| 94 | + local_mount_path = "/mnt/efs" |
| 95 | + } |
| 96 | + |
| 97 | + vpc_config = { |
| 98 | + security_group_ids = [aws_security_group.efs.id] |
| 99 | + subnet_ids = module.vpc.private_subnets |
| 100 | + } |
| 101 | + |
| 102 | + // EFS mount targets must be in available state before Lambda can use them |
| 103 | + depends_on = [aws_efs_mount_target.this] |
| 104 | +} |
0 commit comments