8

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?

2
  • Where are you trying to create the CSV in the code above? Give it a try and let us know if you get stuck. Commented Aug 9, 2019 at 20:52
  • That's the point, I know in theory what I want to do and how, but I don't know how to implement it in the code. So my goal is to create a CSV file where I have 3 columns: 1. account name 2. month 3. cost. And my problem is that I don't know how to pack the information I get into a CSV code Commented Aug 10, 2019 at 19:30

2 Answers 2

5

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.
Sign up to request clarification or add additional context in comments.

5 Comments

i tried to embed the code, but unfortunately get the following error: "errorMessage": "name 'participant_details' is not defined"
Oops, sorry. I have updated the code now, it was just variable name mistake. But, please make sure to change the way you add data in the temp_csv_file.writerow() function based on the way data structure in the response variable
Thanks for your help! Works now finally. Well the CSV is empty without any row, but thats another topic :D
with this file uploaded is empty, size 0B as highlighted by @mango5k as well
File need to close before uploading to S3 bucket. f ile= open("/tmp/csv_file.csv", "w+") ... file.close()
4

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.