Skip to content

Commit

Permalink
Add support for EFS filesystem access in Lambda functions
Browse files Browse the repository at this point in the history
  • Loading branch information
clebert committed Feb 29, 2024
1 parent 664a10d commit fdc728e
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 3 deletions.
Empty file modified ci.sh
100644 → 100755
Empty file.
54 changes: 51 additions & 3 deletions src/cdk/create-lambda-function.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { LambdaRoute, StackConfig } from '../parse-stack-config.js';
import type { Stack, aws_iam } from 'aws-cdk-lib';
import type { Stack } from 'aws-cdk-lib';

import { getDomainName } from '../utils/get-domain-name.js';
import { getHash } from '../utils/get-hash.js';
import { getNormalizedName } from '../utils/get-normalized-name.js';
import { Duration, aws_lambda, aws_logs } from 'aws-cdk-lib';
import { Duration, aws_ec2, aws_efs, aws_iam, aws_lambda, aws_logs } from 'aws-cdk-lib';
import { basename, dirname, extname, join } from 'path';

export interface LambdaFunctionConstructDependencies {
Expand All @@ -29,6 +29,7 @@ export function createLambdaFunction(
memorySize = 128,
timeoutInSeconds = maxTimeoutInSeconds,
environment,
filesystem,
} = route;

if (timeoutInSeconds > maxTimeoutInSeconds) {
Expand All @@ -53,7 +54,37 @@ export function createLambdaFunction(

const { monitoring } = stackConfig;

return new aws_lambda.Function(stack, `Function${getHash(uniqueFunctionName)}`, {
const filesystemProps = filesystem
? {
vpc: aws_ec2.Vpc.fromLookup(stack, `Vpc${getHash(uniqueFunctionName)}`, {
vpcId: filesystem.vpcId,
}),
filesystem: aws_lambda.FileSystem.fromEfsAccessPoint(
aws_efs.AccessPoint.fromAccessPointAttributes(
stack,
`AccessPoint${getHash(uniqueFunctionName)}`,
{
accessPointId: filesystem.accessPointId,
fileSystem: aws_efs.FileSystem.fromFileSystemAttributes(
stack,
`FileSystem${getHash(uniqueFunctionName)}`,
{
fileSystemId: filesystem.fileSystemId,
securityGroup: aws_ec2.SecurityGroup.fromSecurityGroupId(
stack,
`SecurityGroup${getHash(uniqueFunctionName)}`,
filesystem.securityGroupId,
),
},
),
},
),
filesystem.mountPath,
),
}
: undefined;

const fn = new aws_lambda.Function(stack, `Function${getHash(uniqueFunctionName)}`, {
functionName: uniqueFunctionName,
code: aws_lambda.Code.fromAsset(dirname(path)),
handler: `${basename(path, extname(path))}.handler`,
Expand All @@ -69,5 +100,22 @@ export function createLambdaFunction(
: undefined,
logRetention: aws_logs.RetentionDays.TWO_WEEKS,
role: lambdaServiceRole,
...filesystemProps,
});

if (filesystem) {
fn.addToRolePolicy(
new aws_iam.PolicyStatement({
effect: aws_iam.Effect.ALLOW,
actions: [
`ec2:DescribeNetworkInterfaces`,
`ec2:CreateNetworkInterface`,
`ec2:DeleteNetworkInterface`,
],
resources: [`*`],
}),
);
}

return fn;
}
9 changes: 9 additions & 0 deletions src/parse-stack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ const LambdaRouteSchema = z.object({
authenticationEnabled: z.boolean().optional(),
corsEnabled: z.boolean().optional(),
onSynthesize: z.function().optional(),
filesystem: z
.object({
vpcId: z.string(),
fileSystemId: z.string(),
securityGroupId: z.string(),
accessPointId: z.string(),
mountPath: z.string(),
})
.optional(),
});

const S3RouteSchema = z.object({
Expand Down

0 comments on commit fdc728e

Please sign in to comment.