mango5k
mango5k

Reputation: 79

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?

Upvotes: 8

Views: 14025

Answers (2)

NALIN ABROL
NALIN ABROL

Reputation: 41

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()

Upvotes: 4

Repakula Srushith
Repakula Srushith

Reputation: 464

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.

Upvotes: 4

Related Questions