DEV Community

Cover image for How to use EFS with AWS Lambda?
Mohamed Latfalla
Mohamed Latfalla

Posted on

How to use EFS with AWS Lambda?

AWS recently launched a new feature that lets the customer make an EFS (Elastic File System) that works with Lambda. This is really cool! But, Why EFS?

EFS is a storage unit that lives independently inside a machine. This file can be attached to other services like EC2 and can be accessed from multiple instances at once. Files that are inside this storage unit is accessible from any connected instance or Lambda.

Why do we need something like this to work with Lambda? Extra complicated step?

Actually, this feature is amazing if you looked at it from different angles, let’s start with some of them:

1- Consistency:

If you need multiple Lambdas to use (read and write) BIG files, you’ll need them to be in a place that doesn’t delay the function to get the resources, which leads to less computing power and time -> less money.

2- More space:

When you’re working with files from S3, you’re limited to the max storage size of 512 MiB. Which is not enough is some cases. Plus, you might need to work with this file in different processes stages, like cleaning, segmenting, processing, and exporting readable reports/formats of this file. Imagine the amount of code involved in this scenario.

3- More space 2:

Using layers will share the resources between the functions, but Layers sometimes can’t handle the size of the resources and binaries you called to run this function. Using EFS will give you more room to store these resources and call when is needed.

I can list more points. But, you got the idea. Let’s dive into how to use it with lambda.

Creating EFS:

1- Open AWS console and search for “EFS”.

2- Click on “Create file system”.

3- At step 1, select your VPC. If your lambda is configured within a VPC, choose it, if not, remember when VPC you’ve selected.

Alt Text

4- Step 2, Add a descriptive name for your file system. Then click the next step.

5- In step 3, go down and click on “Add access point”. Then fill it with what’s in the image.

6- Review the configuration, then click “Create file system”.

7- Done! wait for a few seconds and your EFS will be active.

Connect it with Lambda:

1- Click on Service and search for “Lambda”.

2- Create a new function and choose your preferred runtime language. In this article, I’ll use Python.

3- Go down a little bit and you’ll see a section called “VPC”, click on “Edit”.

4- Select your VPC, and choose the Subnets and the security group. Then save.

5- Under the VPC section, click on “Add file system” from “file System” section.

6- Select the EFS File System we made, remember that we gave it a descriptive name. Then choose the Access point that is associated with the Access Point and finally, give it a path.

NOTE: This path needs to start with ‘/mnt/‘. You can keep it as it is or if you want to have a custom folder, defiantly you can.

7- A small piece of code to test if the file system is attached to the function.

8- Bingo!! We made it!

Conclusion:

Adding EFS to Lambda is a huge new milestone in The Serverless architecture. You can have new use-cases that will be doable and before were a nightmare to accomplish. Easy steps with the cheap price make Lambda an option that can battle EC2 in some new modern use-cases.

Top comments (0)