Read-only file system AWS Lambda

1

I am trying to write to a text file in Lambda but i get an error

"errorMessage": "[Errno 30] Read-only file system: 'tmp/Mentions_ID.txt'"

Apparently i can read from the same file and others but i can't write. My code is below

def lambda_handler(event, context):
    # TODO implement

    file1 = 'tmp/Mentions_ID.txt'

    # check for the last saved mention
    with open(file1, "r") as textfile:
        mention_id = textfile.read()

     """Some other code"""

    # save the last mentioned
    with open(file1, "w") as textfile:
        textfile.write(str(IDs[0]))

What i've tried: I have seen a lot of solutions addressing this issue, most point to the fact that i need to create a /tmp folder in the same directory as my lambder python file, i have since done that i still can't write. I have also copied the path by right clicking on the file but then it gives me a path beginning with a "/" '/tmp/Mentions_ID.txt', when i run this it gives me a FileNotFoundError. I do not know what else to try please help, my last option would be to use RDS but i don't want to tow that path, thank you.

4 Antworten
2

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

beantwortet vor 2 Jahren
profile picture
EXPERTE
überprüft vor einem Monat
2

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.

  1. 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.
  2. 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

profile pictureAWS
micah
beantwortet vor 2 Jahren
profile picture
EXPERTE
überprüft vor einem Monat
1

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.

profile pictureAWS
Eduardo
beantwortet vor 2 Jahren
profile picture
EXPERTE
überprüft vor einem Monat
  • 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

0

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.

profile pictureAWS
EXPERTE
Uri
beantwortet vor 2 Jahren
profile picture
EXPERTE
überprüft vor einem Monat

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen