7

I have simple lambda function that is located under following endpoint:

https://******.execute-api.eu-west-2.amazonaws.com/lambda/add?x=1&y=2

AWS Chalice was used for adding simple endpoints here.

@app.route('/{exp}', methods=['GET'])
def add(exp):
    app.log.debug("Received GET request...")
    request = app.current_request 
    app.log.debug(app.current_request.json_body)
    x = request.query_params['x']
    y = request.query_params['y']
    if exp == 'add':
        app.log.debug("Received ADD command...")
        result = int(x) + int(y)
        return {'add': result}

Basically, it checks if the path is equal to add and sums two values from query_params.

Now, I am trying to invoke this lambda in another lambda.

My question:

How I can pass the path and query_params to my original lambda function using boto3 lambda client?

What I have tried so far:

I added two lines to policy.json file that allow me to invoke original function.

I saw a lot of similar question on StackOverflow, but most of them pass payload as a json.

@app.route('/')
def index():
    lambda_client = boto3.client('lambda')
    invoke_response = lambda_client.invoke(
        FunctionName="function-name",
        InvocationType="RequestResponse"
    )
    app.log.debug(invoke_response['Payload'].read())

Thank you in advance!

Kyrylo
  • 651
  • 1
  • 6
  • 15
  • Can you just send a post request to the API endpoing instead of invoking? – Sudharsan Sivasankaran Mar 13 '18 at 16:17
  • @SudharsanSivasankaran, Yes, I can. But if it is possible I would like to invoke it using boto3. I think that later I would like to add authentication mechanism (using AWS Cognito, for instance) to my original lambda function and it will be much easier. – Kyrylo Mar 13 '18 at 16:21

1 Answers1

13

Maybe you can add the next code to your add function, so it accepts payloads too:

@app.route('/{exp}', methods=['GET'])
def add(exp, *args, **kwargs):
    if isinstance(exp, dict):
        # exp is event here
        request = exp.get('request', {'x': 0, 'y': 0})
        exp = exp.get('exp', 'add')

I'm going to write a general example, and you can easily modify it to match your needs. In your case the data dictionary would have request and exp keys, and you need to find your lambda function's arn.

AWS Documentation Lambda.invoke

Let's assume from now on we have 2 Lambdas named "master" and "slave". master will call slave.

At the moment there are 3 types of invocations:

  1. RequestResponse (Default): master calls and waits for slave response
  2. Event: Async, master calls and forgets
  3. DryRun: Do some verification before running

I keep with #1 RequestResponse:

Slave:

def lambda_handler(event, context):
    result = {}
    result['event'] = event
    result['result'] = "It's ok"
    return result

And its arn is something like arn:aws:lambda:us-east-1:xxxxxxxxxxxxxx:function:slave

In the example, slave is just an echo function

Now, the master needs the necessary role's permission to call it, and the arn or name. Then you can write something like this:

import boto3
from datetime import datetime
import json

client = boto3.client('lambda')

def lambda_handler(event, context):
    arn = 'arn:aws:lambda:us-east-1:xxxxxxxxxxxxxx:function:slave'
    data = {'my_dict': {'one': 1, 'two': 2}, 'my_list': [1,2,3], 'my_date': datetime.now().isoformat()}

    response = client.invoke(FunctionName=arn,
                             InvocationType='RequestResponse',
                             Payload=json.dumps(data))

    result = json.loads(response.get('Payload').read())
    return result

Usually you would get arn with something like os.environ.get('slave_arn')

All data from/to lambdas must be JSON serializable.

dadiaar
  • 418
  • 5
  • 13
  • Will this work if the worker/slave lambda is invoked in a loop so that multiple instances are running concurrently? Could I use the same client to invoke a lambda multiple times asynchronously, or would i need to dynamically create boto3 clients to do so? – Maile Cupo Dec 23 '20 at 22:41
  • 1
    Both, Async and Sync https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/lambda.html#Lambda.Client.invoke Invokes a Lambda function. You can invoke a function synchronously (and wait for the response), or asynchronously. To invoke a function asynchronously, set InvocationType to Event . – dadiaar Dec 24 '20 at 09:50