Any way to write data in json file located in root location AWS lambda

Viewed 188

I am looking way to write to json file located in my root location of project explorer of my lambda function. screenshot for ref. enter image description here

I tried this:

import json
import boto3
print('Loading function')

def lambda_handler(event, context):
    #print("Received event: " + json.dumps(event, indent=2))
    bdy = """[
    {
    "source_name": "3455",
    "freq": 10,
    "incremental_column": [
      "DateTimeStamp"
     ],
    "target_name": "gett45",
    "sql": "Select r,t,t from tbl where date > '{}'"
    },
    {
    "source_name": "eter334",
    "freq": 10,
    "target_name": "get66",
    "sql": "Select r,t,t from tbl where date > '{}'"
    },{
    "source_name": "etwl",
    "freq": 10,
    "target_name": "g8777",
    "sql": "Select r,t,t from tbl where date > '{}'"
    }
    ]"""
    data = json.loads(bdy)
    with open('/data.json', 'w') as f:
      f.write(data)

But getting error :

    {
  "errorMessage": "[Errno 30] Read-only file system: '/data.json'",
  "errorType": "OSError",
  "stackTrace": [
    "  File \"/var/task/lambda_function.py\", line 31, in lambda_handler\n    with open('/data.json', 'w') as f:\n"
  ]
}
1 Answers

Only part of the Lambda file system is writable. If you want to write a file from a Lambda function it must be inside the /tmp folder.

Related