Fixing AWS Lambda Error “Read-only file system” when downloading a file from S3

AWS Lambda Error “Read-only file system” when downloading a file from S3 is a very popular error which occurs a lot when you try write a file in AWS Lambda.

In this article I am going to show you why this error is happening and how you can solve this error. I am also going to share with you other possible solutions which have worked for many developers who suffered from this error.

Exploring the AWS Lambda Error “Read-only file system” when downloading a file from S3

This is a very popular error which occurs a lot when you try write a file in AWS Lambda.

Your error message should match the one bellow. We want to avoid confusion by not mixing between error messages.

                                                                       #
IOError: [Errno 30] Read-only file system: u'/mydoc.csv'
                                                                       #

Bellow is a number of tested solutions that I have tried and that have worked for me.

How to get rid of the issue : use /tmp/ in AWS Lambda instead

If you try to write the file like this in AWS Lambda. You can read more about /tmp/ in the official AWS documentation.

                                                                       #
bucket = event_record["s3"]["bucket"]["name"]
key = event_record["s3"]["object"]["key"]
filepath = '/' + key
                                                                       #

You will certainly face the issue. you should use ‘/tmp/’.

The code above should be replaced with

                                                                       #
bucket = event_record["s3"]["bucket"]["name"]
key = event_record["s3"]["object"]["key"]
filepath = '/tmp/' + key
                                                                       #

This is the only solution that has worked for me since you can only write in /tmp/ in AWS Lambda.

The solutions above should be enough to solve the problem, if you like our effort make sure to consider donating to our Kofi account, there is a red button that you can use if you are feeling generous.

Summing-up : 

This is the end of our article, this error could be confusing but with a little bit of investigating the error could be solved easily by renaming your files, thank you for reading.

To support us consider donating to our Kofi account above. Keep coding, keep learning and cheers. If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/