Saving a json/txt file to S3 from Lambda

Question:

I am using lambda to run a program. Once it is run I need to save some data as a json file or txt. I tried to see if there is a way to write a file in lambda and save it but there are no write permissions it is read only. So I am now thinking of saving this json object in S3, so that i can retrieve it later. Is there a way to upload to s3 without writing a file and just upload an object

Answers:

AWS Lambda is just another runtime environment for your Python code. You can utilize the boto3 SDK to upload your file to S3.
Something along the lines of the following should work:

import json

import boto3

s3 = boto3.resource('s3')
obj = s3.Object('BUCKET_NAME', 'FILE_NAME.json')

obj.put(Body=(bytes(json.dumps(data).encode('UTF-8'))))
Answered By: cynhash