AWS Lambda Function File System is Read-Only

While coding a Twitter bot as an AWS Lambda Function for a presentation I wanted to maintain the state of a single value. Using an external service, like DynamoDB, RDS, Redis, or SQS, was something I really didn’t want to dive into for an introductory presentation.

Although Lambda Functions are all about being stateless, I was curious if I could use a JSON configuration file uploaded as part of a Python deployment package for simple, writable storage.

Turns out the answer is no as I suspected, but it’s good to try these things for ourselves.

I uploaded a config.json file as part of my Python deployment package, which contained a single id.

    "id" : 0

I also included a Python script as part of the package, which reads, increments, and writes the id to the config.json file.

import json

def lambda_handler(event, context):
        with open('config.json', 'r') as f:
            id = json.load(f)['id']
        id = id + 100

        with open('config.json', 'w') as f:
            json.dump({'id': id}, f)

if __name__ == '__main__':
    lambda_handler(None, None)

When you run this Lambda Function in AWS you will get an error.

  "errorMessage": "[Errno 30] Read-only file system: 'config.json'",
  "errorType": "OSError",
  "stackTrace": [
      "with open('config.json', 'w') as f:"

The AWS Lambda Function File System is definitely Read-Only :)