Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ zulip:

### How to fix "Permission denied" error on AWS

You need `AWSBudgetsReadOnlyAccess` to fetch the AWS usage data.
You need `AWSBudgetsReadOnlyAccess` and ` AmazonEC2ReadOnlyAccess` to fetch the AWS usage data.

Check your current permission policy on IAM > Users.

Expand Down
1 change: 1 addition & 0 deletions config.yaml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ zulip:
{year}年{month}月の月額コストをお知らせします。
* 本日{day}日までの利用分は ${cost:.2f} です。
* AWSの予測によると、今月の合計費用は ${forecast:.2f} です。
* 現在、EC2で起動しているサーバーは{nserver}台です。
19 changes: 16 additions & 3 deletions report.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ def get_monthly_cost(config, aws_profile_name):
forecast = resp['Budgets'][0]['CalculatedSpend']['ForecastedSpend']['Amount']
return (float(cost), float(forecast))

def get_server_stats(config, aws_profile_name):
# TODO Define AWSUsage() class to factor out the boilarplate.
session = boto3.Session(profile_name=aws_profile_name)
client = session.client("ec2")
resp = client.describe_instances()
nserver = 0
for resv in resp["Reservations"]:
for inst in resv['Instances']:
if inst['State']['Name'] != 'terminated':
nserver += 1
return nserver

def send_message(config, message):
client = zulip.Client(site=config['zulip']['site'],
email=config['zulip']['email'],
Expand All @@ -35,16 +47,17 @@ def send_message(config, message):
'content': message
})

def format_message(config, cost, forecast):
def format_message(config, cost, forecast, nserver):
today = datetime.date.today()
template = config['zulip']['message']
return template.format(year=today.year, month=today.month, day=today.day,
cost=cost, forecast=forecast)
cost=cost, forecast=forecast, nserver=nserver)

def main(aws_profile_name = "default", dryrun=False):
config = load_config()
cost, forecast = get_monthly_cost(config, aws_profile_name)
message = format_message(config, cost, forecast)
nserver = get_server_stats(config, aws_profile_name)
message = format_message(config, cost, forecast, nserver)
if dryrun:
print(message)
else:
Expand Down