1

I'm new to Chalice and I'm trying to call an aws lambda using boto3 in a python script. I need to know how to call a specific 'route' in that lambda. Maybe set client context or something in the event?

The python script can call a lambda function but not sure how I call (pass in) a route in that lambda.

Code inside app.py:

@app.route('/dosomething', methods=['GET'], cors=True)
def doSomething():
    results = somethingWasDone()
    return {"result": results}

So if the lambda's name is myLambda I want to call myLambda and tell it to trigger the above code for dosomething route. Thanks

Pete
  • 169
  • 2
  • 14
  • 1
    I think you have to make a test event and send the test event. The test event form should look like an AWS API Gateway event form. The API gateway event have a `path` parameter and this is the key. – Lamanus Aug 31 '19 at 07:32
  • @Lamanus has the answer. Note that frameworks like Chalice provide the actual Lambda entry point, then uses the `route` decorator to decide how to process the incoming event (API Gateway or ALB). See https://docs.aws.amazon.com/lambda/latest/dg/with-on-demand-https.html and https://docs.aws.amazon.com/lambda/latest/dg/services-alb.html for sample events. – bimsapi Sep 01 '19 at 14:18
  • @Lamanus do you have a link or something to sample code? I couldn't find any and I can't use api gw client as I want to invoke the lambda directly. – Pete Sep 03 '19 at 14:41
  • @bimsapi I can't find any code that invokes lambda and passing in the path in your links. – Pete Sep 03 '19 at 14:42
  • I guess I didn't understand the question. Normally, you invoke a Chalice app's Lambda via HTTP. See the `curl` example in https://chalice.readthedocs.io/en/latest/quickstart.html. `chalice deploy` creates an API Gateway for HTTP and a Lambda. You submit an HTTP request to the Gateway, which maps HTTP protocol elements to fields in the event passed to the Chalice Lambda, which decides which route/function to call. If you invoke the Lambda directly via boto instead of HTTP, you emulate the API Gateway by providing the equivalent of its event (which is what those links point to). – bimsapi Sep 03 '19 at 21:19
  • See [Tutorial: Local Mode](https://github.com/aws/chalice#tutorial-local-mode) for exactly what you describe, assuming your request is for testing. – dmulter Sep 04 '19 at 20:21
  • @bimsapi I want to invoke the lambda function via cli. Not via a curl command or API GW. Just plain old boto3 invoke call (https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/lambda.html#Lambda.Client.invoke). – Pete Sep 09 '19 at 18:59
  • @dmulter Thanks but this is running the http server locally. What I want to do is invoke a specific app route in the already deployed lambda. – Pete Sep 09 '19 at 19:01
  • **Via CLI:** `aws lambda invoke --function-name --payload file://./event.json --invocation-type RequestResponse `, where `event.json` is a JSON file corresponding the structures in those links I had originally posted. `RequestResponse` makes the call synchronous; `output file` is where the actual data returned from your view will get written. **Via boto3:** `boto3.client('lambda').invoke(**kwargs)` where `kwargs` are the same, but `CamelCase` instead of `kebab-case`. – bimsapi Sep 10 '19 at 13:30
  • I think you are not understanding Chalice Lambda functions and routes completely. The `doSomething()` route is not an invokable Lambda function. The only way to exercise that code path is with an HTTP request. Chalice does have a CLI command to invoke a Lambda function, but this won't help in your case. If you want a CLI command, just wrap an HTTP request yourself since Chalice does not offer their own CLI to make HTTP requests. – dmulter Sep 20 '19 at 15:32

1 Answers1

2

To invoke any Lambda function, you need to use the lambda:Invoke API call. From the AWS CLI, it is called via aws lambda invoke; from boto3 it is called via boto3.client('lambda').invoke(**kwargs).

The most relevant arguments are:

  • FunctionName. The name of the function to invoke (on the CLI as --function-name)
  • InvocationType. Determines whether to invoke the API sync (RequestResponse) or async (Event) (CLI: --invocation-type)
  • Payload. The data that becomes the event argument passed to the Lambda handler (CLI: --payload, and can be provided as a file URL. E.g., --payload file://./relative/path.json

Additionally, when invoking via aws lambda invoke you need to specify an output file, where the results of the Lambda will get written.

See:

When using Chalice, or any framework that implements "web routing"-like behavior, invoking just the desired route requires you to provide an event that looks like the event generated by API Gateway or an Application Load Balancer. I.e., if you have N routes, the framework will typically deploy just a single Lambda function and use the HTTP information encoded in the event to determine which route to invoke within the function.

See:

A minimal event file (payload.json) could look like the following. Of particularly relevance are the path and resource argument. You may need to make the resource match the value in the API Gateway Chalice generates for you.

{
  "path": "/",
  "resource": "/",
  "queryStringParameters": {},
  "headers": {},
  "httpMethod": "GET",
  "requestContext": {
    "httpMethod": "GET"
  },
  "body": {}
}

The resulting CLI invocation would look like this:

aws lambda invoke --function-name fn \
  --payload file://./payload.json \
  --invocation-type RequestResponse \
  output.txt

Hope that helps.

bimsapi
  • 4,985
  • 2
  • 19
  • 27
  • Thanks! I will try this out. I know how to invoke the lambda but passing in the required route for chalice routing was the part I needed to figure out. Also I'm making the call using boto3 but the same payload file should work there as well. – Pete Sep 13 '19 at 14:06