'Can I Limit Value used by python in Uri of an Api Gateway
Asking a new question as my original was closed by admin who said a similar question had the answer. I checked it and it didnt have the answer. That answer was related to user input in Python itself. I am using a front end in the form of an Api Uri which I have included in this question.
In my code below I use this to call an Api using 'Method' and 'Value' parameters. For example on the URL they use to call the Api they enter Method2=Min and Value2=2 This works fine and allows the user to input whatever they want into Value2 so they can change the settings of an AutoScaling Group.
If the user makes a mistake and inputs Value as 100 instead of 10 in the Uri, for example Value3=100, I would not want the Autoscaling Group Max size to change to 100. Therefore is there of editing my script so I can stop that Max size from changing to 100? Like some sort of max size value in the script itself?
To invoke the Api I use powershell command like this
invoke-webrequest -Uri 'https://name-of-api.amazonaws.com/?TagKey=tag-key-name&TagValue=tag-value-name&Method1=Capacity&Value1=1&Method2=Min&Value2=1&Method3=Max&Value3=4' -Headers @{"X-Api-Key"="name-of-api-key"}
As you can see the user has the power to change 'Method3=Max' to have a 'Value3' of 100 if they want
Thanks
import boto3
import botocore
import os
import json
import logging
# Set up logger.
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
# Set up clients.
client = boto3.client('autoscaling')
def handler(event, context):
# Step one: get all AutoScaling Groups.
response = client.describe_auto_scaling_groups(
MaxRecords=100
)
# Make an empty list for ASG info storage.
allAutoScaling = [] # empty list of no items.
# Get the initial results and posts them to CloudWatch logs
allAutoScaling.append(response['AutoScalingGroups'])
logger.info(allAutoScaling)
# If 'Marker' is present in the response, we have more to get.
while 'Marker' in response:
old_marker = response['Marker']
response = client.describe_auto_scaling_groups(
MaxRecords=100,
Marker = old_marker
)
allAutoScaling.append(response['AutoScalingGroups'])
# Cycles back up to repeat the pagination.
# Now to find the tags specified in the Api Uri
for autoscaling in allAutoScaling:
for key in autoscaling:
for tag in key['Tags']:
if tag['Key'] == event['TagKey'] and tag['Value'] == event['TagValue']:
if event['Method2'] == "Min":
if event['Value2']:
Value2 = int(event['Value2'])
response = client.update_auto_scaling_group(
AutoScalingGroupName=key['AutoScalingGroupName'],
MinSize=Value2
)
else:
print('Min already at required level')
if event['Method3'] == "Max":
if event['Value3']:
Value3 = int(event['Value3'])
response = client.update_auto_scaling_group(
AutoScalingGroupName=key['AutoScalingGroupName'],
MaxSize=Value3
)
else:
print('Max already at required level')
if event['Method1'] == "Capacity":
if event['Value1']:
Value1 = int(event['Value1'])
response = client.set_desired_capacity(
AutoScalingGroupName=key['AutoScalingGroupName'],
DesiredCapacity=Value1
)
else:
print('Capacity already at required level')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
