API Gateway can proxy all requests directly to a Lambda function, making it easy to wire up complex integrations. This is a good alternative to using
claudia-api-builder if you want to process all request information and routing yourself, or if you want to pass it on to a third-party module that provides routing (such as an express server, for example).
In this tutorial, you’ll learn how to create and deploy a simple proxy API. Make sure you use Claudia.js version 2.0 or later.
Creating a proxy Lambda function
Proxy functions are just normal Lambda functions, that need to reply in a specific format. The response from the function needs to be a object with three properties:
statusCode: numeric HTTP status code
body: string, body of the response
headers: object, key value mapping between HTTP response header names and values.
The event received by the Lambda function contains all the request information, such as HTTP headers, query string variables, request path and so on. Check out the AWS Proxy request object for more information.
Here is a trivially simple Lambda function that just sends back all the request information.
Deploying to Lambda
From Claudia version 2.0, you can easily deploy a proxy API with your lambda function. Just specify
claudia create. Save the Lambda function from the previous section into a file called
main.js, initialize the directory as an NPM project, and then just execute the following command line to send it to Lambda and API Gateway:
The command will print a URL when it completes. You can now open that URL in a browser, and try adding sub-paths, query string parameters, posting to it, and generally using it as a HTTP API.