Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass parameters to an AWS Lambda Function using Python

I have written an AWS Lambda function in Python that filters through instances and turns them on or off depending on how they are tagged. This will show you a working function, and the set-up needed to get it working. If you have questions on anything post it in the comments.

Here is my Lambda Function as of now:

import boto3
def lambda_handler(event, context):
    startStop = event['startStop']
    vertical = event['vertical']
    isRunning = ''

    if(startStop == 'start'):
        isRunning = 'stopped'
    elif (startStop == 'stop'):
        isRunning = 'running'

    ec2 = boto3.resource('ec2')
    filters = [
        {
            'Name': 'instance-state-name', 
            'Values': [isRunning] 
        },
        {
            'Name': 'tag:Vertical',
            'Values': [vertical]
        }
    ]

    instances = ec2.instances.filter(Filters=filters)

    runningInstances = [instance.id for instance in instances]

    if len(runningInstances) > 0:
        if(startStop == 'start'):
            shuttingDown = ec2.instances.filter(InstanceIds=runningInstances).start()
        elif (startStop == 'stop'):
            shuttingDown = ec2.instances.filter(InstanceIds=runningInstances).stop()

For reference, here is my mapping template:

{
    "startStop":"$input.params('startStop')",
    "vertical":"$input.params('vertical')"
}

And this is how I am passing in the parameters within the URL:

https://awslambdaapiurl.../prod/-params?startStop=start&vertical=TEST
like image 711
Doug Andres Avatar asked Jul 19 '17 18:07

Doug Andres


People also ask

How do you pass parameters in lambda function in Python?

A Python lambda function behaves like a normal function in regard to arguments. Therefore, a lambda parameter can be initialized with a default value: the parameter n takes the outer n as a default value. The Python lambda function could have been written as lambda x=n: print(x) and have the same result.

How do you pass multiple parameters in lambda expression Python?

Just like a normal function, a Lambda function can have multiple arguments with one expression. In Python, lambda expressions (or lambda forms) are utilized to construct anonymous functions. To do so, you will use the lambda keyword (just as you use def to define normal functions).

How do you call a lambda function in Python?

We can declare a lambda function and call it as an anonymous function, without assigning it to a variable. Above, lambda x: x*x defines an anonymous function and call it once by passing arguments in the parenthesis (lambda x: x*x)(5) .

Can Python Lambda have default arguments?

Defaults in Python Lambda ExpressionIn Python, and in other languages like C++, we can specify default arguments.


2 Answers

Pass in parameter values in a mapping template:

{
    "startStop":"$input.params('startStop')",
    "vertical":"$input.params('vertical')"
}

Read parameter values via event Object:

    startStop = event['startStop']
    vertical = event['vertical']
like image 63
cellepo Avatar answered Sep 18 '22 07:09

cellepo


You can use the queryStringParameters on event object.

request_data = event['queryStringParameters']
startStop = request_data['startStop']
vertical = request_data['vertical']
like image 38
Chandra Shekhar Goka Avatar answered Sep 18 '22 07:09

Chandra Shekhar Goka