diff --git a/roles/aws/aws_admin_tools/tasks/create_methods.yml b/roles/aws/aws_admin_tools/tasks/create_methods.yml index 0db30c164..baf864a01 100644 --- a/roles/aws/aws_admin_tools/tasks/create_methods.yml +++ b/roles/aws/aws_admin_tools/tasks/create_methods.yml @@ -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 }} diff --git a/roles/aws/aws_admin_tools/templates/API_ChangeASGScaling.py.j2 b/roles/aws/aws_admin_tools/templates/API_ChangeASGScaling.py.j2 index 6bed7668b..2cfc32e09 100644 --- a/roles/aws/aws_admin_tools/templates/API_ChangeASGScaling.py.j2 +++ b/roles/aws/aws_admin_tools/templates/API_ChangeASGScaling.py.j2 @@ -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 }