aws-sam-cli
aws-sam-cli copied to clipboard
Emulate EFS locally by mounting a local filesystem
Describe your idea/feature/enhancement
I have a lambda function that needs to store short-lived pieces of data, things like token keys for api access or the most-recently-sent notification to an api to prevent spamming users with repetitive messages. This data needs to persist beyond the lifetime of the lambda function.
As I was exploring how to implement this, I read that AWS had recently added support for EFS to lambda, and I wanted to test how that might work in my functions, using the efs filesystem to store data compared to alternatives such as DynamoDB
Proposal
sam local invoke -e ./events/twitch-webhook.json --filesystem=./efs-tmp/
Things to consider:
- Will this require any updates to the SAM Spec?
It should not, this is a local invocation feature.
Additional Details
This is a limited feature that might not fill all customer needs; some customers might prefer to be able to access remote EFS file systems while running their function locally. In this use case simply having a filesystem available is enough. In the use case of running sam local start-api to exercise the app's api, the will need to mock out whatever filesystem they depend on locally.
For reference - this is how we get around this issue - at least for start-api.
https://github.com/dacgray/sam-local-start-api-container-image-volume-mount
Work Around
I personally compiled the entire BE using webpack. So the entry point of the lambda is ./dist/main.js
If I need to access file that should "act" like EFS mounting I create a global variable for my mounting point based on the environment the lambda is.
So on local, I put the file I need to access in my ./dist/efs
This way the mounting point on the lambda /VAR/TASK will contains the efs folder in my dist
It's a work around if you need to access files and test some processes on them locally.
It cannot work across multiple lambdas only on the one you uploaded files
The work around of @benjaminfiliatrault works for the Java runtime too.
In this case, I added the files in src/main/resources/efs/.
Then, I use an environment variable to access them in /var/task/efs/