How to use EFS with AWS Lambda?
Tags: cloud, aws, serverless, tutorial
Published: 2020-06-22 18:26:33

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.

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

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

<i>1- Consistency:</i>

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.

<i>2- More space:</i>

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.

<i>3- More space 2:</i>

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.

<h2>Creating EFS:</h2>

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

![](https://dev-to-uploads.s3.amazonaws.com/i/q46ue5o8cpo47vv04lml.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/iglb8ndvjps79dt9wufn.png)

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

![](https://dev-to-uploads.s3.amazonaws.com/i/or1anxwxk2oivon8fg92.png)

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

![](https://dev-to-uploads.s3.amazonaws.com/i/qllnu91fpj1p7asjk1x6.png)

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

![](https://dev-to-uploads.s3.amazonaws.com/i/jdychu1xxehhsuvyu2x3.png)

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

![](https://dev-to-uploads.s3.amazonaws.com/i/pf70meusvf7y1c7btj7i.png)

<h2>Connect it with Lambda:</h2>

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

![](https://dev-to-uploads.s3.amazonaws.com/i/u6at0ljuni0z9hilubrn.png)


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

![](https://dev-to-uploads.s3.amazonaws.com/i/qkna43zotpx65c6j5u19.png)

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

![](https://dev-to-uploads.s3.amazonaws.com/i/reph7gh5a85g1y6ahm90.png)

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

![](https://dev-to-uploads.s3.amazonaws.com/i/fmjemhf8vdv3pwp0jz7n.png)

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

![](https://dev-to-uploads.s3.amazonaws.com/i/kik0da12yh3y19pzm8xk.png)

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.

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

![](https://dev-to-uploads.s3.amazonaws.com/i/d60ocs881f7rc35jmvkg.png)

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

![](https://dev-to-uploads.s3.amazonaws.com/i/hz8r8ivjlwyo821qumos.png)

8- Bingo!! We made it!

![](https://dev-to-uploads.s3.amazonaws.com/i/1qqu57sziw0lfyz3wn6g.png)

<h2>Conclusion:</h2>

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.