-
Notifications
You must be signed in to change notification settings - Fork 1
/
lambda_email_bill_report.py
118 lines (96 loc) · 3.38 KB
/
lambda_email_bill_report.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import json
from datetime import date
import boto3
from email.mime.application import MIMEApplication
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
def get_data():
# Accessing Cost Explorer API
client = boto3.client('ce')
# StartDate = 1st date of current month, EndDate = Todays date
start_date=str(date(year=date.today().year, month=date.today().month, day=1).strftime('%Y-%m-%d'))
end_date=str(date.today())
print(f'StartDate:{start_date} - EndDate:{end_date}\n')
# The get_cost_and_usage operation is a part of the AWS Cost Explorer API, which allows you to programmatically retrieve cost and usage data for your AWS accounts.
response = client.get_cost_and_usage(
TimePeriod={
'Start':start_date,
'End': end_date
},
Granularity='MONTHLY',
Metrics=['UnblendedCost'],
Filter={
"Not":
{
'Dimensions':{
'Key': 'RECORD_TYPE',
'Values':['Credit','Refund']
}
}
},
GroupBy=[
{
'Type':'DIMENSION',
'Key':'SERVICE'
}
]
)
mydict=response
resource_name=[]
resource_cost=[]
total_resources_active = len(mydict['ResultsByTime'][0]['Groups'])
for i in range (total_resources_active):
a=(mydict['ResultsByTime'][0]['Groups'][i].values())
b=list(a)
resource_name.append(b[0][0])
resource_cost.append(float(b[1]['UnblendedCost']['Amount']))
dict0={}
for i in range(total_resources_active):
dict0[resource_name[i]]=resource_cost[i]
billed_resources={k: v for k, v in dict0.items() if v}
print(f'Current Billed Resources of this month:-',json.dumps(billed_resources, indent=4, sort_keys=True))
print(f'Active Resources:-', json.dumps(resource_name, indent=4, sort_keys=True))
#print(billed_resources,resource_name)
return billed_resources, resource_name
def send_email(bill, resources, sender, recipient):
TodayDate = str(date.today())
subject = f"AWS Bill Report - {TodayDate}"
total_cost = sum(v for v in bill.values())
# Email body template
email_body = """
<h3>Current Billed Resources for this month:</h3>
<table>
<tr>
<th>Resource</th>
<th>Cost</th>
</tr>
{bill}
</table>
<h3>Total Cost: {total_cost} dollars</h3>
<h3>Active Resources:</h3>
<ul>
{resources}
</ul>
"""
# Format the email body
formatted_body = email_body.format(
bill='\n'.join(f"<tr><td>{k}</td><td>{v}</td></tr>" for k, v in bill.items()),
resources='\n'.join(f"<li>{r}</li>" for r in resources),
total_cost=total_cost
)
# Create the email
msg = MIMEMultipart()
msg["Subject"] = subject
msg["From"] = sender
msg["To"] = recipient
msg.attach(MIMEText(formatted_body,"html"))
# Send the email using Amazon SES
ses = boto3.client("ses")
ses.send_raw_email(RawMessage={"Data": msg.as_bytes()})
def lambda_handler(event, context):
bill,resource=get_data()
send_email(bill, resource, "[email protected]","[email protected]")
return {
'statusCode': 200,
'body': json.dumps('Email sent successfully')
}