Building Serverless Applications with AWS Lambda & API Gateway
10 min read
Serverless is the way to go when it comes to developing highly scalable and available applications. Serverless does not mean that there is no server, but it means that the tasks associated with infrastructure provisioning and management are completely handled by the cloud service provider and invisible to the developer.
Building applications that are completely serverless enables developers to increase their productivity and bring products to market faster, and it allows organizations to better optimize resources and stay focused on innovation.
Both AWS Lambda and API Gateway are two commonly used serverless web services available on the AWS platform.
What is AWS Lambda?
Run code without thinking about servers and pay only for the compute time you consume. AWS Lambda is an event-driven, serverless computing service that lets you run code in response to events and triggers without having to provision or manage any servers.
AWS Lambda executes your code only when needed and scales automatically, from a few requests per day to thousands per second. You pay only for the compute time you consume and there is no charge when your code is not running.
What is API Gateway?
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. It offers a comprehensive platform for API management.
API Gateway allows you to process hundreds of thousands of concurrent API calls and handles traffic management, authorization, and access control, monitoring, and API version management.
Building A Simple Serverless App
As a quick and easy demonstration of how to build serverless applications on the AWS platform, we are going to develop a simple REST API application written with Python 3. The application would convert a given pair of Geographical coordinates in Latitude and Longitude to Universal Transverse Mercator (UTM) and returns the response in JSON.
As shown in the architectural diagram above, the computing layer of the application is going to be handled by AWS Lambda while API Gateway acts as a front-end interphase that allows the user/client to interact with the application. The Python code for this exercise would be deployed as a Lambda function that is only triggered by making a GET API request to the API Gateway endpoint.
1. Create a Lambda function
The code you run on AWS Lambda is uploaded as a “Lambda function”. Each function has associated configuration information, such as its name, description, entry point, and resource requirements. The Lambda functions must be stateless and can perform any kind of computing task, from serving web pages and processing streams of data to calling APIs and integrating with other AWS services.
To create a Lambda function, you navigate to the AWS Lambda page in the AWS console. Click on "Create function", select the "Author from scratch" option, input your function name, and select a Runtime for your application. Your runtime defines the language that your application is going to be written in. AWS Lambda natively supports runtimes for Python, Java, Go, PowerShell, Node.js, C#, and Ruby. Once this is done, leave all other defaults and click the "Create function" button again to create our Lambda function.
2. Setup a local Python environment (optional)
This second step is only required for applications that require external dependencies that can not be accessed on AWS Lambda. If your application does not require any external dependencies, you can write your code directly in the interactive Python environment provided by AWS Lambda.
To set up a local environment, you create an empty folder on your local system, install all the necessary dependencies that your application requires to run in that folder including your lambda function script that handles all the processing. Use the command below to make sure the dependencies are installed in the same folder as the script that is going to use them.
# installs the python library UTM in our application folder pip install utm -t.
Once we're sure that all our dependencies are working as expected, we would deploy the local environment as a Lambda function by converting the application folder along with the contents in it to a Zip file and upload the zipped file to AWS (Zipped files that are more than 10MB should be uploaded to Amazon S3 first and referenced in AWS Lambda).
For a better understanding of what the environment looks like, the source code repository that includes the uploaded zipped folder can be found on GitHub.
3. Create REST API Endpoint
After deploying our Python code to AWS Lambda, we need to create a REST API endpoint that points to our Lambda function. This endpoint would trigger the Lambda function each time it is called. The endpoint would accept a GET method and would take in two query parameters of "lat" and "lon". These parameters get passed to AWS Lambda as an Events.
a. Create New API: To create a new API, navigate to the API Gateway page in AWS Console. Select the create "New API" option, input the API name, and click the "Create API" button.
b. Create a Resource: A resource is a URL path for your API endpoint (E.g: ./convert). On the resources page of API Gateway, you can create a resource by clicking on the "Actions" dropdown menu and selecting the "Create Resource" option.
c. Create a Method: On the resources page of API Gateway, you can define an API method for interacting with the REST API by clicking on the "Actions" dropdown menu and selecting the "Create Method" option.
As shown in the above diagram, select Lambda as the integration type and input the name of the Lambda function that was created in Step 1. This would point the API endpoint to the Lambda function and makes sure the Lambda function gets triggered by calling the API endpoint.
d. Deploy API: In order to access our API via URL, we need to deploy our new API to a staging environment. Based on the version of your deployment, you can name your staging environment "test", "prod" or any name you wish. By clicking the same "Action" dropdown menu, we would see the "Deploy API" option.
After deploying the API, a URL would be generated. This URL can be used to invoke our Lambda function and it would return the necessary result.
4. Interacting with the application
Just like any other REST API with a GET method, we can interact with the application by invoking the API URL in a web browser of API testing tools like Postman. The URL endpoint takes in two parameters of "lat" and "lon".
API URL: https://r1jj65kh4k.execute-api.us-east-2.amazonaws.com/prod/convert?lat=47.9941214&lon=7.8509671
Invoking the URL as shown above would return a response in JSON that contains the value of the UTM Easting, UTM Northing, and the UTM Zone of the converted Latitude and Longitude coordinate.
The opportunities that serverless applications provide are endless and can eliminate to a reasonable extent a lot of human resources and overheads deployed to provisioning, securing, managing, and maintaining servers.
This tutorial is aimed at helping developers that are new to the concept of serverless architectures better understand the basics of building and deploying serverless applications on AWS.