Deploying a Proxy API

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:

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.

main.js

exports.handler = function (event, context) {
	'use strict';
	context.succeed({
		statusCode: 200,
		headers: { 'Content-Type': 'application/json' },
		body: JSON.stringify(event)
	});
};

Deploying to Lambda

From Claudia version 2.0, you can easily deploy a proxy API with your lambda function. Just specify --deploy-proxy-api with 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:

claudia create --region us-east-1 --deploy-proxy-api --handler main.handler

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.

More information

Did you like this tutorial? Get notified when we publish the next one.

Once a month, high value mailing list, no ads or spam. (Check out the past issues)