Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a CSV in Lambda using Python?

I would like to create a report in Lambda using Python that is saved in a CSV file. So you will find the code of the function:

import boto3
import datetime
import re

def lambda_handler(event, context):

    client = boto3.client('ce')

    now = datetime.datetime.utcnow()
    end = datetime.datetime(year=now.year, month=now.month, day=1)

    start = end - datetime.timedelta(days=1)
    start = datetime.datetime(year=start.year, month=start.month, day=1)

    start = start.strftime('%Y-%m-%d')
    end = end.strftime('%Y-%m-%d')


    response = client.get_cost_and_usage(
        TimePeriod={
            'Start': "2019-02-01",
            'End':  "2019-08-01"
        },
        Granularity='MONTHLY',
        Metrics=['BlendedCost'],
        GroupBy=[
            {
                'Type': 'TAG',
                'Key': 'Project'
            },
        ]
    )

How can I create a CSV file from it?

like image 415
mango5k Avatar asked Aug 09 '19 11:08

mango5k


People also ask

How do I create a CSV file from a list in Python?

The most common method to write data from a list to CSV file is the writerow() method of writer and DictWriter class. Example 1: Creating a CSV file and writing data row-wise into it using writer class.

How do I create a CSV file in AWS S3?

Once you have created your S3 bucket and IAM user, you can set up the data export in the Adjust dashboard. Navigate to your app and select your app options caret ( ^ ). Navigate to All Settings > Raw Data Export > CSV Upload.


2 Answers

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()
like image 127
NALIN ABROL Avatar answered Oct 01 '22 09:10

NALIN ABROL


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.
like image 20
Repakula Srushith Avatar answered Oct 01 '22 11:10

Repakula Srushith