VOOZH about

URL: https://thenewstack.io/turn-aws-lambda-functions-stateful-with-amazon-elastic-file-system/

⇱ Turn AWS Lambda Functions Stateful with Amazon Elastic File System - The New Stack


TNS
SUBSCRIBE
Join our community of software engineering leaders and aspirational developers. Always stay in-the-know by getting the most important news and exclusive content delivered fresh to your inbox to learn more about at-scale software development.
REQUIRED
It seems that you've previously unsubscribed from our newsletter in the past. Click the button below to open the re-subscribe form in a new tab. When you're done, simply close that tab and continue with this form to complete your subscription.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.
Welcome and thank you for joining The New Stack community!
Please answer a few simple questions to help us deliver the news and resources you are interested in.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Great to meet you!
Tell us a bit about your job so we can cover the topics you find most relevant.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Welcome!

We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.

What’s next?

Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.

Follow TNS on your favorite social media networks.

Become a TNS follower on LinkedIn.

Check out the latest featured and trending stories while you wait for your first TNS newsletter.

PREV
1 of 2
NEXT
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
Thanks for your opinion! Subscribe below to get the final results, published exclusively in our TNS Update newsletter:
NEW! Try Stackie AI
From clobbered drafts to real-time sync
Apr 14th 2026 10:00am, by David Moore
TypeScript 6.0 RC arrives as a bridge to a faster future
Mar 14th 2026 9:00am, by Darryl K. Taft
Mastra empowers web devs to build AI agents in TypeScript
Jan 28th 2026 11:00am, by Loraine Lawson
2020-11-02 10:55:21
Turn AWS Lambda Functions Stateful with Amazon Elastic File System
feature,tutorial,
Cloud Services / Serverless / Storage

Turn AWS Lambda Functions Stateful with Amazon Elastic File System

This tutorial series covers all the aspects of using Amazon EFS with AWS Lambda to host the serverless machine learning API. Part 1: Get to know EFS.
Nov 2nd, 2020 10:55am by Janakiram MSV
👁 Featued image for: Turn AWS Lambda Functions Stateful with Amazon Elastic File System
Feature image by moren hsu on Unsplash.

Amazon Web Services’ Lambda is one of the first serverless platforms in the industry. Since its launch in 2014, Amazon has added multiple features to make it the most mature Functions as a Service (FaaS). The platform supports various language runtimes, including Node.js, Python, Java, Ruby, C#, Go, and PowerShell. There is tight integration with mainstream AWS managed services that act as event sources to trigger Lambda functions.

Traditionally, serverless compute platforms and FaaS offerings such as AWS Lambda are associated with stateless functions. Since the functions are invoked and terminated based on events, there is no intrinsic persistence layer available. The state is always externalized by moving it to object storage, NoSQL database, in-memory database, or a relational database instance. It’s common to maintain state in Lambda functions by writing it to an object in an S3 bucket or a DynamoDB or RDS table.

But certain use cases such as machine learning inference demand a new approach. Downloading a large model from an Amazon S3 bucket increases the startup time, which results in latency. Some functions require external libraries that may be too large. Though AWS Lambda Layers’ concept addresses this problem, there is a limitation of 50MB (zipped, for direct upload), which defeats the purpose. Layers are static once they are deployed, which means the contents can be changed only by deploying a new layer.

In June 2020, AWS has added support for Amazon Elastic File System (EFS) for Lambda, enabling many exciting use cases.

This tutorial series covers all the aspects of using Amazon EFS with AWS Lambda to host the serverless machine learning API.

What Is Amazon EFS?

Amazon Elastic File System (EFS) provides a managed elastic NFS file system for AWS services and on-premises resources. It can scale to petabytes without disrupting applications, growing and shrinking automatically as the files or added and removed, eliminating the need to provision and manage capacity to accommodate growth.

Since EFS uses NFS v4, the industry standard for the shared file system, the file system can be easily attached to EC2 instances running Linux.

Amazon EFS exposes well-known access points that can be configured per application. EFS access points represent a flexible way to manage application access in NFS environments with increased scalability, security, and ease of use. One EFS file system can have multiple access points. Each access point can be configured with permissions associated with POSIX-compliant user id and group id. Combined with IAM, EFS file systems can have fine-grained security and access control.

It’s important to understand that Amazon EFS is available only within a VPC. Only those consumers within the same VPC can access the EFS file system. On-premises servers can mount EFS shares only after establishing connectivity through AWS Direct Connect or AWS VPN.

Accessing Amazon EFS Filesystem from AWS Lambda

When an EFS file system is attached to an AWS Lambda function, it can access existing data and store data in it. This approach makes it possible to populate the filesystem with the dependencies and additional files that become available to all the Lambda instances.

👁 Image

👁 Image

The prerequisite for an AWS Lambda to access an EFS file system is that the function should be in the same VPC as the EFS. It should also have explicit permission to access the file system and create Elastic Network Interfaces (ENI) for the subnets of the VPC. Once these conditions are met, a Lambda function can read and write to the EFS file system.

Populating Content in EFS through an Amazon EC2 Instance

The easiest way to populate the EFS file system accessed by a Lambda function is by mounting it to an EC2 instance. Using the standard NFS conventions, an EFS file system shows up in the /mnt directory.

👁 Image

When launching an EC2 instance from the AWS Console, it has an option to mount an existing EFS filesystem. The wizard automatically adds appropriate user script to permanently mount the file system by adding an entry in the /etc/fstab.

Use Case: Hosting Serverless ML Inference API on AWS Lambda

The powerful combination of EFS and Lambda functions can be used to host deep learning inference API in serverless mode. Since the size of a TensorFlow or PyTorch model may exceed the size limits of Lambda layers and /tmp directory, EFS comes in handy in storing the models.

👁 Image

The EFS storage backend for Lambda can also have all the dependencies such as OpenCV or PIL which are not only large but take time to install. A Lambda Python function can be pointed to an existing directory through the PYTHONPATH environment variable. The same file system will also have the fully trained model stored in a separate directory which is loaded by the function.

To populate the EFS file system with the Python modules and pre-trained model, we can use an Amazon EC2 instance or even a SageMaker Notebook. Both options give us the ability to mount the file system and add the dependencies either through a Python virtual environment or through the pip installer.

The below workflow highlights the steps involved in this approach:

  1. Create an EFS file system in an existing VPC
  2. Launch an EC2 instance in the same VPC and mount the EFS
  3. Populate the EFS file system with Python modules and a PyTorch model
  4. Create an AWS Lambda Python function in the same VPC
  5. Add IAM roles for accessing EFS and creating network interfaces in VPC
  6. Attach the same EFS file system used in the EC2 instance
  7. Add environment variables to point the Python runtime to existing modules in EFS
  8. Attach an API Gateway to expose the function as an HTTP API
  9. Configure VPC with a NAT Gateway to allow outbound traffic to the internet (Optional)
  10. Invoke the serverless API

This week The New Stack will feature a series of upcoming tutorials on this subject, where I will walk you through all the steps involved in hosting serverless machine learning inference API in AWS Lambda. Check in tomorrow for the next installment!

Janakiram MSV’s Webinar series, “Machine Intelligence and Modern Infrastructure (MI2)” offers informative and insightful sessions covering cutting-edge technologies. Sign up for the upcoming MI2 webinar at http://mi2.live.

TRENDING STORIES
Janakiram MSV (Jani) is a practicing architect, research analyst, and advisor to Silicon Valley startups. He focuses on the convergence of modern infrastructure powered by cloud-native technology and machine intelligence driven by generative AI. Before becoming an entrepreneur, he spent...
Read more from Janakiram MSV
SHARE THIS STORY
TRENDING STORIES
Amazon Web Services is a sponsor of The New Stack.
SHARE THIS STORY
TRENDING STORIES
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.