How do I create a CSV in Lambda using Python?

The above answer by Repakula Srushith is correct but will be creating an empty csv as the file is not being closed. You can change the code to

f = open("/tmp/csv_file.csv", "w+")
temp_csv_file = csv.writer(f) 
temp_csv_file.writerow(["Account Name", "Month", "Cost"])

# writing rows in to the CSV file
for detail in response:
    temp_csv_file.writerow([detail['account_name'],
                            detail['month'],
                            detail['cost']
                            ])
f.close()

Here is a sample function to create a CSV file in Lambda using Python:

Assuming that the variable 'response' has the required data for creating the report for you, the following piece of code will help you create a temporary CSV file in the /tmp folder of the lambda function:

import csv
temp_csv_file = csv.writer(open("/tmp/csv_file.csv", "w+"))
# writing the column names
temp_csv_file.writerow(["Account Name", "Month", "Cost"])

# writing rows in to the CSV file
for detail in response:
    temp_csv_file.writerow([detail['account_name'],
                            detail['month'],
                            detail['cost']
                            ])

Once you have created the CSV file, you can upload it S3 and send it as an email or share it as link using the following piece of code:

client = boto3.client('s3')
client.upload_file('/tmp/csv_file.csv', BUCKET_NAME,'final_report.csv')

Points to remember:

  1. The /tmp is a directory storage of size 512 MB which can be used to store a few in memory/ temporary files
  2. You should not rely on this storage to maintain state across sub-sequent lambda functions.