From 75ac44047353b68082c19898180e93b8cbdeb704 Mon Sep 17 00:00:00 2001 From: Matej Stajduhar Date: Mon, 31 Mar 2025 11:14:13 +0200 Subject: [PATCH 1/4] Start-using-latest-version-of-lambda-with-API --- roles/aws/aws_admin_tools/tasks/create_methods.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/roles/aws/aws_admin_tools/tasks/create_methods.yml b/roles/aws/aws_admin_tools/tasks/create_methods.yml index 0db30c164..996d10c8e 100644 --- a/roles/aws/aws_admin_tools/tasks/create_methods.yml +++ b/roles/aws/aws_admin_tools/tasks/create_methods.yml @@ -55,7 +55,7 @@ --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" + --uri "arn:aws:apigateway:{{ _aws_region }}:lambda:path/functions/arn:aws:lambda:{{ _aws_region }}:{{ _acc_id }}:function:API_{{ item.name }}/invocations" --region {{ _aws_region }} - name: Add method response. From 884294d66bf33c1d0014f95e2d257317b6efe3eb Mon Sep 17 00:00:00 2001 From: Matej Stajduhar Date: Mon, 31 Mar 2025 11:17:14 +0200 Subject: [PATCH 2/4] Start-using-latest-version-of-lambda-with-API-2 --- roles/_meta/aws_region/meta/main.yml | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/roles/_meta/aws_region/meta/main.yml b/roles/_meta/aws_region/meta/main.yml index a11491c0e..9fd95f4d9 100644 --- a/roles/_meta/aws_region/meta/main.yml +++ b/roles/_meta/aws_region/meta/main.yml @@ -1,11 +1,11 @@ --- dependencies: - - role: aws/aws_provision_ec2_keypair - - role: aws/aws_vpc - - role: aws/aws_vpc_subnet - - role: aws/aws_iam_role - - role: aws/aws_acl - - role: aws/aws_cloudwatch_log_group - - role: aws/aws_backup - - role: aws/aws_backup_sns +# - role: aws/aws_provision_ec2_keypair +# - role: aws/aws_vpc +# - role: aws/aws_vpc_subnet +# - role: aws/aws_iam_role +# - role: aws/aws_acl +# - role: aws/aws_cloudwatch_log_group +# - role: aws/aws_backup +# - role: aws/aws_backup_sns - role: aws/aws_admin_tools From d5d95f151c1448769c79e46897d6ef27041ccc6b Mon Sep 17 00:00:00 2001 From: Matej Stajduhar Date: Mon, 31 Mar 2025 14:59:26 +0200 Subject: [PATCH 3/4] Updating-put-integration-command --- roles/aws/aws_admin_tools/tasks/create_methods.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/roles/aws/aws_admin_tools/tasks/create_methods.yml b/roles/aws/aws_admin_tools/tasks/create_methods.yml index 996d10c8e..baf864a01 100644 --- a/roles/aws/aws_admin_tools/tasks/create_methods.yml +++ b/roles/aws/aws_admin_tools/tasks/create_methods.yml @@ -53,9 +53,8 @@ --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/functions/arn:aws:lambda:{{ _aws_region }}:{{ _acc_id }}:function:API_{{ item.name }}/invocations" + --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 }} - name: Add method response. From 4a09a9920696e878da6bae49873a1fdb1e942bbd Mon Sep 17 00:00:00 2001 From: Matej Stajduhar Date: Mon, 31 Mar 2025 16:12:20 +0200 Subject: [PATCH 4/4] Updating-ChangeASGScaling-event-function --- roles/_meta/aws_region/meta/main.yml | 16 +++---- .../templates/API_ChangeASGScaling.py.j2 | 47 ++++++++----------- 2 files changed, 27 insertions(+), 36 deletions(-) diff --git a/roles/_meta/aws_region/meta/main.yml b/roles/_meta/aws_region/meta/main.yml index 9fd95f4d9..a11491c0e 100644 --- a/roles/_meta/aws_region/meta/main.yml +++ b/roles/_meta/aws_region/meta/main.yml @@ -1,11 +1,11 @@ --- dependencies: -# - role: aws/aws_provision_ec2_keypair -# - role: aws/aws_vpc -# - role: aws/aws_vpc_subnet -# - role: aws/aws_iam_role -# - role: aws/aws_acl -# - role: aws/aws_cloudwatch_log_group -# - role: aws/aws_backup -# - role: aws/aws_backup_sns + - role: aws/aws_provision_ec2_keypair + - role: aws/aws_vpc + - role: aws/aws_vpc_subnet + - role: aws/aws_iam_role + - role: aws/aws_acl + - role: aws/aws_cloudwatch_log_group + - role: aws/aws_backup + - role: aws/aws_backup_sns - role: aws/aws_admin_tools 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 }