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
1 change: 0 additions & 1 deletion roles/aws/aws_admin_tools/tasks/create_methods.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
--http-method "{{ item.type }}"
--type AWS
--content-handling CONVERT_TO_TEXT
--request-templates '{ "application/json": "{\"statusCode\": 200}" }'
--integration-http-method POST
--uri "arn:aws:apigateway:{{ _aws_region }}:lambda:path/2015-03-31/functions/arn:aws:lambda:{{ _aws_region }}:{{ _acc_id }}:function:API_{{ item.name }}/invocations"
--region {{ _aws_region }}
Expand Down
47 changes: 19 additions & 28 deletions roles/aws/aws_admin_tools/templates/API_ChangeASGScaling.py.j2
Original file line number Diff line number Diff line change
@@ -1,39 +1,30 @@
import json
import calendar
from datetime import datetime
import boto3

costExpl = boto3.client('ce')
asg_cli = boto3.client('autoscaling', region_name="{{ _aws_region }}")

def lambda_handler(event, context):
currDay=datetime.now().day
currMonth=datetime.now().month
print(currMonth)
currYear=datetime.now().year
print(currYear)
lastDay=calendar.monthrange(currYear, currMonth)

if currMonth < 10:
currMonth = '0' + str(currMonth)
nextDay = currDay + 1
if currDay < 10:
currDay = '0' + str(currDay)
if nextDay < 10:
nextDay = '0' + str(nextDay)
policies = asg_cli.describe_policies(
AutoScalingGroupName=event['asg_name']
)

startDate=str(currYear) + '-' + str(currMonth) + '-' + str(currDay)
endDate=str(currYear) + '-' + str(currMonth) + '-' + str(nextDay)
if policies['ScalingPolicies'][0]['Enabled']:
enable = False
else:
enable = True

for inst in policies['ScalingPolicies']:
put_result = asg_cli.put_scaling_policy(
AutoScalingGroupName=inst['AutoScalingGroupName'],
PolicyName=inst['PolicyName'],
PolicyType=inst['PolicyType'],
AdjustmentType=inst['AdjustmentType'],
ScalingAdjustment=inst['ScalingAdjustment'],
Enabled=enable
)

estimatedCost = costExpl.get_cost_forecast(
TimePeriod={
'Start': startDate,
'End': endDate
},
Granularity='MONTHLY',
Metric='BLENDED_COST'
)
return {
'statusCode': 200,
'Amount': estimatedCost['Total']['Amount'] + ' ' + estimatedCost['Total']['Unit'],
'Between': estimatedCost['ForecastResultsByTime'][0]['TimePeriod']['Start'] + ' - ' + estimatedCost['ForecastResultsByTime'][0]['TimePeriod']['End']
'body': event
}
Loading