'LAMBDA function for auto scaling group

I'm using below function in lambda for setting up auto scaling capacity

import boto3
import os
region = 'ap-south-1'
ASG_name = ['test-scheduler']
asg = boto3.client('autoscaling', 'os.environ[region]')

def lambda_handler(event, context):
    response = asg.update_auto_scaling_group(AutoScalingGroupName=os.environ['test-scheduler'],MinSize=os.environ['1'],DesiredCapacity=os.environ['1'],MaxSize=os.environ['1'])
  

AFTER TEST ERROR IS SHOWN AS BELOW it is showing provided region doesn't match a supported format

{ "errorMessage": "Provided region_name 'os.environ[aws_region]' doesn't match a supported format.", "errorType": "InvalidRegionError", "requestId": "", "stackTrace": [ " File "/var/lang/lib/python3.9/importlib/init.py", line 127, in import_module\n return _bootstrap._gcd_import(name[level:], package, level)\n", " File "", line 1030, in _gcd_import\n", " File "", line 1007, in _find_and_load\n", " File "", line 986, in _find_and_load_unlocked\n", " File "", line 680, in _load_unlocked\n", " File "", line 850, in exec_module\n", " File "", line 228, in _call_with_frames_removed\n", " File "/var/task/lambda_function.py", line 5, in \n asg = boto3.client('autoscaling','os.environ[aws_region]')\n", " File "/var/runtime/boto3/init.py", line 93, in client\n return _get_default_session().client(*args, **kwargs)\n", " File "/var/runtime/boto3/session.py", line 258, in client\n return self._session.create_client(\n", " File "/var/runtime/botocore/session.py", line 810, in create_client\n region_name = self._resolve_region_name(region_name, config)\n", " File "/var/runtime/botocore/session.py", line 866, in _resolve_region_name\n validate_region_name(region_name)\n", " File "/var/runtime/botocore/utils.py", line 1026, in validate_region_name\n raise InvalidRegionError(region_name=region_name)\n" ] }



Solution 1:[1]

I had the same mistake.

It comes from the format for the "region" variable.

It is already used by AWS Lambda so you have to follow the format imposed by AWS.

In short, you can't override the aws format because they are reserved environment variables.

In the link below, choose "Python" and you will see the list of reserved variables.

https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html

  region = os.environ['AWS_REGION']

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Boris