- Newest
- Most votes
- Most comments
It should be /tmp/....
You missed the leading slash
The only directory you can write within the Lambda environment is /tmp
. All other directories are read-only
If you are trying to persist data between Lambda invocations, you will need to use something external to Lambda. The /tmp folder is temp storage that should only be used during the duration of a single Lambda invocation. However, there are many ways to persist data. Here are a couple common solutions.
- Store the file on S3. If it is a small enough file that can be easily loaded and written to during the course of your Lambda this might be a good choice.
- DynamoDB - You could store the data in a DynamoDB table, which is also serverless, very easy to set up, and less expensive than running an RDS instance. This is a very common pattern for persisting data with Lambda.
Here is an example of how to build a simple CRUD API with Lambda, DynamoDB and API Gateway.
https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-dynamo-db.html
In your code, you are trying to read a file before writing it, thus giving the file not found error. Please, review it. When the Lambda starts, the /tmp folder is empty. You also can't guarantee the file will be available between calls, as the Lambda function may be recycled.
Yeah, i understand you. For my use case i need to read data from a file use that data to execute some code and then write (replace) the result back into the file. So i may need this files to be permanent rather than temporary. So if that won't work i may need to opt for a database solution like RDS. Except you of course have any other option
It is not clear what you are trying to do exactly. Your code first tries to read a file and then write to it. Are you trying to write to a file that you included as part of the Lambda deployment?
If this is the case, you should be aware that the only location inside a Lambda runtime you can write to is /tmp (and not tmp as in your code), but you can't place files there during deployment time. So you will need to copy the file to /tmp and edit it there. Saying that, this is probably not what you want as the /tmp is not shares across Lambda runtime environments and different invocations can get to different environments.
The recommended solution would be to save the file to some external store which can be: EFS, S3 or some database. All depending on your exact usecase.
Relevant content
- AWS OFFICIALUpdated a year ago
- AWS OFFICIALUpdated 3 months ago
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated 3 years ago
I addressed that in the question, when i include that slash it doesn't find the file
"errorMessage": "[Errno 2] No such file or directory: '/tmp/Mentions_ID.txt'"
Lambda runtime is ephemeral
If you want to persist file, trying using EFS: https://aws.amazon.com/blogs/compute/using-amazon-efs-for-aws-lambda-in-your-serverless-applications/