Using AWS Lambda with Amazon API Gateway (from the perspective of an on-premise API developer)
This is a quick overview of creating a basic REST API using two AWS resources: API Gateway (as the trigger) and Lambda (as the function). This is not a step-by-step tutorial, but rather a supplementary writeup (that introduces on-premise API developers to how REST API works in the cloud)
It is assumed that the person reading this article knows the foundations of a REST API. If not, I suggest you take a look at this YouTube video below (helps if you’re going thru an API developer interview *wink*)
Now that you’re up to speed, let’s run thru API Gateway and Lambda.
Amazon API Gateway is a fully managed service that makes it easy for developers to create, publish, maintain, monitor, and secure APIs at any scale. APIs act as the “front door” for applications to access data, business logic, or functionality from your backend services. Using API Gateway, you can create RESTful APIs and WebSocket APIs that enable real-time two-way communication applications. API Gateway supports containerized and serverless workloads, as well as web applications.
AWS Lambda is a serverless compute service that lets you run code without provisioning or managing servers, creating workload-aware cluster scaling logic, maintaining event integrations, or managing runtimes. With Lambda, you can run code for virtually any type of application or backend service — all with zero administration. Just upload your code as a ZIP file or container image, and Lambda automatically and precisely allocates compute execution power and runs your code based on the incoming request or event, for any scale of traffic. You can set up your code to automatically trigger from 140 AWS services or call it directly from any web or mobile app. You can write Lambda functions in your favorite language (Node.js, Python, Go, Java, and more) and use both serverless and container tools, such as AWS SAM or Docker CLI, to build, test, and deploy your functions.
Talk about nosebleed! Let’s keep it simple. Take a look at the diagram below. Basically, API Gateway is the trigger and Lambda is the function (API logic). There’s a lot more options to choose from as the trigger, but this is the AWS recommended serverless setup for APIs.
You can follow this AWS documentation to checkout how to create a basic API Gateway-Lambda integration.
API Documentation and Testing from API Gateway
API Gateway provides the API documentation in the console itself. Navigate to the resources section of your API, click on the method (in this case, ANY), then click on the TEST link
This will bring you to a screen similar to something like below, where you can do a test trigger passing either Query Strings or Query Parameters.
Hit the trigger button to see the result. Cool right?
API Testing
API testing can be done in API gateway or on the logic layer (Lambda). From lambda, you should be able to see a “Test” tab, where you can create a test event and trigger that from the Lambda console and see if you get the correct response.
Monitoring and Logs
Lambda functions, when triggered, create logs in CloudWatch which you can review under the Monitor tab in the Lambda console. This can also be viewed in CloudWatch.
This is a more comprehensive AWS documentation on this topic: https://docs.aws.amazon.com/apigateway/latest/developerguide/getting-started-with-lambda-integration.html.
Further specific readings: https://docs.aws.amazon.com/lambda/latest/dg/services-apigateway.html