2

This time I'm looking for passing parameters to lambda invoke using boto3.client instead of make a request to API gateway.

Assume I developed a lambda function that can be invoked through api get. ie: https://linktolambda/<parameter> and now I am trying to access same lambda function from another one using boto3 client.

I have read documentation from:

I have read stackoverflow questions, reddit questions, medium posts, etc. but I haven't found what I'm looking for.

Thanks in advance.

1
  • 1
    Perhaps you could explain why the linked documentation did not help. The first link, for example, does exactly what you appear to be asking for (one Lambda function can synchronously or asynchronously invoke a second Lambda function without having to do it via API Gateway). Commented Jun 15, 2022 at 0:37

3 Answers 3

4

If you don't want to update your lambda function, just simulate APIGateway event object by boto3 client:

If your api looks like https://linktolambda/{id} (ex: https://linktolambda/123456)

You will invoke with this code:

        payload = { "pathParameters": { "id":"123456" } } 
        result = client.invoke(FunctionName=conf.lambda_function_name,
                    InvocationType='RequestResponse',                                      
                    Payload=json.dumps(payload))

Or your API look like https://linktolambda?id=123456

        payload = { "queryStringParameters": { "id":"123456" } } 
        result = client.invoke(FunctionName=conf.lambda_function_name,
                    InvocationType='RequestResponse',                                      
                    Payload=json.dumps(payload))
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for your response. As we try your method, we get a 200 type response from invoke but a type 500 error in payload. Our lambda api looks like https://linktolambda/subpath/{id}. I have tried with all type of combinations of parameters explained in: link, always returning a 500 response.
This is the final response we get: {'headers': {},'multiValueHeaders': {}, 'statusCode': 500, 'body': '{"Code":"InternalServerError","Message":"Unknown request."}'}
@ninsignares are you using chalice to build the lambda function? github.com/aws/chalice/issues/1337
Yes I am. I use chalice to build and deploy my lambda function. I have tried github solution without success. I guess is more efficient and good practice to invoke lambdas instead to request apis.
0

There are actually two methods in BOTO3 that can be used to invoke a Lambda function. The first is: [invoke(**kwargs)][1]

and the second is: [invoke_async(**kwargs)][2]

See:

https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/lambda.html#Lambda.Client.invoke

https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/lambda.html#Lambda.Client.invoke_async

The first method makes a synchronous call and the second method makes an asynchronous call. Once your Lambda function has been deployed, you can use either method to invoke it.

1 Comment

When I went to the invoke_async link given in this answer, the page states that invoke_async is deprecated. Instead, you can use the invoke method with the parameter InvocationType='Event'
0

Look at the Official AWS Code Example Github. For Python Code examples look here:

https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/python

You can find how to invoke a Lambda function and pass parameters (exactly what you are looking for) in this code example:

https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/python/example_code/lambda/lambda_basics.py

Look for the method invoke_function.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.