loading...
Cover image for Attach a FileSystem to your AWS Lambda Function
CDK Patterns

Attach a FileSystem to your AWS Lambda Function

nideveloper profile image Matt Coulter Updated on ・2 min read

Alt Text

This is a pattern that attaches an EFS file system to your lambda function to give it expandable, persistent storage. Having this level of storage in a Lambda Function opens the door to many new possibilities (multiple functions can even use the same file system).

Some Useful References:

Available Versions

What's Included In This Pattern?

This pattern covers the first half of Danilo Poccia's awesome blog post. After deployment you will have an API Gateway HTTP API where any url you hit gets directed to a Lambda Function that is integrated with EFS.

VPC

A VPC is bundled in this pattern because EFS requires it, this is using the default settings from CDK so if you want to put this in production you will have to review / refine this

EFS FileSystem

A FileSystem is included in the above VPC with a removal policy of destroy. In a production system you probably would want to retain your storage on stack deletion.

POSIX permissions are also setup for this File System

Lambda Function

A simple Python lambda function that interacts with the file system - storing, retrieving and deleting messages

API Gateway HTTP API

This is configured with the Lambda Function as the default handler for any url you hit.

How Do I Test This Pattern?

Our deployed Lambda Function is acting as a shared message broker. It allows you to send messages to it which it stores in EFS, then you can retrieve all messages to read them or delete all messages after you have finished reading.

The Lambda Function will behave differently based on the RESTful verb you use:

  • GET - Retrieve messages
  • POST - Send a message (whatever you send in the body is the message)
  • DELETE - Deletes all stored messages

The URL for the HTTP API to use these commands will be printed in the CloudFormation stack output after you deploy.

Note - After deployment you may need to wait 60-90 seconds before the implementation works as expected. There are a lot of network configurations happening so you need to wait on propagation

Discussion

pic
Editor guide