This is a step-by-step guide for creating a simple serverless Web API using Lambda and API Gateway, and deploying it to AWS using Claudia.js. AWS Lambda is an event-driven compute service, that executes your code in reaction to various events. API Gateway is a HTTPS end-point that can receive requests and pass them on to Lambda. In this tutorial, we’ll connect them together to execute simple requests.
- AWS account with access to IAM, Lambda and API Gateway
- Node.js 6
If you’re completely new to using AWS from Node.js, check out Installing and configuring Claudia.js for information on how to set up access credentials.
Initialising the project
Create a directory, and initialise a new NPM project, for example, using
npm init. Make sure to give it a descriptive name.
Install Claudia.js in your global path:
Creating a web endpoint
Your main API module should export an instance of
claudia-api-builder. You can use the
.get method to create a
GET end-point. Similar functions exist for other HTTP methods. For now, we’ll just print out a simple static string. We’ll make that a bit more dynamic later.
app.js with the following code:
Deploying to AWS
You can now send your new API to the AWS cloud. Instead of setting the handler method, such as when creating a plain Lambda function, you’ll need to provide the name of the main API module using
--api-module. Because the file with the code is
app.js, the main module will be just
app. Run the following command:
In a few moments, Claudia will respond with the details of the new Lambda function and REST API, looking similar to the one below:
The result will contain the root URL of your new API. We created an endpoint for
/hello, so just add
/hello to the URL, and try it out in a browser or from the console. For example, execute:
(replace the API URL with the one you got from the deployment result).
You should see the ‘hello world’ response – and your first serverless API is now live in the cloud!
Using request parameters
Let’s make the API a bit more dynamic. Claudia API Builder provides an object to your request handlers with all the query string and form parameters, dynamic path component information and environment variables, so you can easily use them without creating any API Gateway template mapping files. Try this next:
superb module as a project dependency:
Add a new endpoint to the API by appending these lines to your
You can send the new version of your API to AWS simply by running the following command:
When the deployment completes, try out the new endpoint by adding `/greet?name=’ followed by your name. You’ll get a nice confidence boost.
Check out the other Tutorials for more advanced guides.
API Builder allows you to control almost anything you’d expect from a lightweight web server, including setting custom response headers, controlling response codes and content types, intercepting requests and more. Check out the API Documentation for information on how to do that.