Updated in aws lambbda function runtime fromt python2.7 to python 3.7 and getting
Unable to import module 'lambda_function': No module named 'commands'
from datetime import datetime, timedelta
from datetime import datetime
import boto3
import os
import json
import datetime
import calendar
import logging
import commands
import commands
from base64 import b64decode
from urllib3 import request, urlopen, URLError, HTTPError
date = datetime.datetime.today()
#print d
date = datetime.datetime.now()
ses = boto3.client('ses')
HOOK_URL = "https://hooks.slack.com"
s3 = boto3.client('s3')
logger = logging.getLogger()
logger.setLevel(logging.INFO)
dict = {}
dict1 = {}
seconds_in_one_day = 86400 # used for granularity
client = boto3.client('ce')
def lambda_handler(event, context):
print(commands.getstatusoutput('uname -a'))
next_day = (date + timedelta(days=1)).strftime("%Y-%m-%d")
yest_day = (date - timedelta(days=1)).strftime("%Y-%m-%d")
month_first_day = date.strftime("%Y-%m-01")
print (next_day)
print (month_first_day)
print (yest_day)
today = date.strftime("%Y-%m-%d")
print (today)
day_befroe_yest = (date - timedelta(days=2)).strftime("%Y-%m-%d")
print (day_befroe_yest)
#############################
response = client.get_cost_and_usage(
TimePeriod={
'Start': month_first_day,
'End': next_day
},
Granularity='MONTHLY',
Metrics=[
'UnblendedCost',
],
)
#print(response)
# print((response["ResultsByTime"][0]["Total"]["UnblendedCost"]["Amount"]))
TOTAL_COST = (response["ResultsByTime"][0]["Total"]["UnblendedCost"]["Amount"])
# print(TOTAL_COST)`enter code here`
TOTAL_COST=(TOTAL_COST[:-7])
#print(TOTAL_COST)
##############################
response = client.get_cost_and_usage(
TimePeriod={
'Start': day_befroe_yest,
'End': yest_day
},
Granularity='DAILY',
Metrics=[
'UnblendedCost',
],
)
yest_cost = (response)["ResultsByTime"][0]["Total"]["UnblendedCost"]["Amount"]
print(yest_cost)
#["Total"]["UnblendedCost"]["Amount"]
yest_cost=(yest_cost[:-6])
print(yest_cost)
#############################
cost = {}
cost['Date'] = today
cost['TOTAL_COST'] = "$"+TOTAL_COST
cost['YEST_COST'] = "$"+yest_cost
#cost['']
with open('/tmp/daily.txt', 'a+') as outfile:
json.dump(cost, outfile)
s3.upload_file('/tmp/daily.txt', '-cost', 'cost.txt')
slack_message = {
}
req = Request(HOOK_URL, json.dumps(slack_message))
try:
response = urlopen(req)
response.read()
logger.info("Message posted to %s", slack_message['channel'])
except HTTPError as e:
logger.error("Request failed: %d %s", e.code, e.reason)
except URLError as e:
logger.error("Server connection failed: %s", e.reason)
return "something"