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.

smac2020
  • 9,637
  • 4
  • 24
  • 38
ninsignares
  • 29
  • 1
  • 5
  • 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). – jarmod Jun 15 '22 at 00:37

3 Answers3

2

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))
hoangdv
  • 15,138
  • 4
  • 27
  • 48
  • 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](https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html), always returning a 500 response. – ninsignares Jun 16 '22 at 17:54
  • This is the final response we get: `{'headers': {},'multiValueHeaders': {}, 'statusCode': 500, 'body': '{"Code":"InternalServerError","Message":"Unknown request."}'}` – ninsignares Jun 16 '22 at 18:09
  • @ninsignares are you using `chalice` to build the lambda function? https://github.com/aws/chalice/issues/1337 – hoangdv Jun 17 '22 at 01:35
  • 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. – ninsignares Jun 21 '22 at 23:06
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 at line 184.

smac2020
  • 9,637
  • 4
  • 24
  • 38
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.

Irene
  • 94
  • 4
  • 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'``` – mherzog Aug 14 '22 at 18:40