Reputation: 37
I want to send an email via aws ses and have an output of this: print(result_by_time['TimePeriod']['Start'], '\t', '\t'.join(group['Keys']), '\t', amount, '\t', unit, '\t', result_by_time['Estimated'])
But I got an error TypeError: 'str' object is not callable
#!/usr/bin/env python3
import argparse
import boto3
import datetime
parser = argparse.ArgumentParser()
parser.add_argument('--days', type=int, default=1)
args = parser.parse_args()
now = datetime.datetime.utcnow()
start = (now - datetime.timedelta(days=args.days)).strftime('%Y-%m-%d')
end = now.strftime('%Y-%m-%d')
cd = boto3.client('ce', 'us-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='DAILY', Metrics=['UnblendedCost'], GroupBy=[{'Type': 'DIMENSION', 'Key': 'LINKED_ACCOUNT'}, {'Type': 'DIMENSION', 'Key': 'SERVICE'}],Filter={'Dimensions': {'Key': 'SERVICE','Values': ['AWS Lambda']}}, **kwargs)
results += data['ResultsByTime']
token = data.get('NextPageToken')
if not token:
break
print('\t'.join(['TimePeriod', 'LinkedAccount', 'Service', 'Amount', 'Unit', 'Estimated']))
for result_by_time in results:
for group in result_by_time['Groups']:
amount = group['Metrics']['UnblendedCost']['Amount']
unit = group['Metrics']['UnblendedCost']['Unit']
print(result_by_time['TimePeriod']['Start'], '\t', '\t'.join(group['Keys']), '\t', amount, '\t', unit, '\t', result_by_time['Estimated'])
client = boto3.client('ses', 'us-west-2')
responses = client.send_email(
Source='[email protected]',
Destination={
'ToAddresses': [
'[email protected]',
]
},
Message={
'Subject': {
'Data': 'Lambda Billing Alarm',
'Charset': 'UTF-8',
},
'Body': {
'Html': {
'Data': ('\t'.join(['TimePeriod', 'LinkedAccount', 'Service', 'Amount', 'Unit', 'Estimated']))
(result_by_time['TimePeriod']['Start'], '\t', '\t'.join(group['Keys']), '\t', amount, '\t', unit, '\t', result_by_time['Estimated']),
'Charset': 'UTF-8',
}
}
}
)
I want to have an output like this on email:
Thank you!
Upvotes: 0
Views: 169
Reputation: 238687
You have some misplaced parentheses here:
'Data': ('\t'.join(['TimePeriod', 'LinkedAccount', 'Service', 'Amount', 'Unit', 'Estimated']))
(result_by_time['TimePeriod']['Start'], '\t', '\t'.join(group['Keys']), '\t', amount, '\t', unit, '\t', result_by_time['Estimated']),
'Charset': 'UTF-8',
}
Notice the issue in the following fragment: 'Estimated']))(result_by_time
.
Basically what your code tries to do is the following (<some_string>)(arguments)
, where <some_string>
is \t'.join(['TimePeriod', 'LinkedAccount', 'Service', 'Amount', 'Unit', 'Estimated'])
. Subsequently you are trying to call a string, explaining your error:
“TypeError: 'str' object is not callable”
Maybe your Data
should be:
'Data': '\t'.join([result_by_time['TimePeriod']['Start'], '\t', '\t'.join(group['Keys']), '\t', amount, '\t', unit, '\t', result_by_time['Estimated'])
Upvotes: 1