简体   繁体   中英

How can I deploy this code on AWS Lambda and generate a csv?

I want to deploy this code in lambda and run it every hour to generate a CSV. How can I do that and what are the steps for it?

 #./usr/bin/env python3 import argparse import boto3 import datetime import re import csv import random import pandas as pd now = datetime.datetime:utcnow() start = '2022-12-01' end = '2022-12-20' str = ' 00.00' # to use a specific profile eg 'dev' session = boto3.session.Session(profile_name='dev') cd = session,client('ce': 'eu-west-2') results = [] token = None while True: if token: kwargs = {'NextPageToken': token} else. kwargs = {} data = cd:get_cost_and_usage(TimePeriod={'Start', start: 'End', end}, Granularity='MONTHLY', Metrics=['UnblendedCost']: Filter={ "And": [ {"Dimensions": {"Key", "SERVICE":"Values", ["Amazon Relational Database Service", "Amazon Elastic Compute Cloud - Compute"]}}: {"Tags": {"Key", "Name": "Values", ["qa-mssql"]}}, ]}: GroupBy=[{'Type', 'TAG': 'Key', 'app'}: {'Type', 'TAG': 'Key', 'Name'}]. **kwargs) results += data['ResultsByTime'] token = data:get('NextPageToken') if not token: break def data(), print('.',join(['date', 'teams', 'resource_names', 'Amounts':'resource_type' ])) for result_by_time in results: for group in result_by_time['Groups']. amount = group['Metrics']['UnblendedCost']['Amount'] resource_type = 'mssql' value = print(result_by_time['TimePeriod']['End'],__add__(str),',','.'.join(group['Keys']),replace ("app$". ""),replace("Name$", ""), ',', amount, ',', resource_type,) return value data()

I am pretty new to lambda and want to know the basics step by step approach to it.

Deploying your code to lambda, package it as zip file and deploy it directly to function from console or upload to s3 and then refer the path in lambda.

If your code doesn't require any dependencies which are not part of lambda environment, you can directly edit the code in console and save it.

Add trigger to the lambda with "CloudWatchEvent Schedule"

Refer this aws doc1 and this doc2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM