Why Do You Need Apache APISIX When Using Amazon Lambda
Serverless and Amazon Lambda
What is Serverless?
Serverless computing, or simply serverless, is a popular trend in the current software market. More and more companies are migrating their operations from traditional server-oriented architecture to a faster and cost-effective serverless architecture.
Serverless architecture is a way to build and run applications without managing infrastructure. It deploys the infrastructure required in the cloud vendors or some self-deployed serverless platform for management. So that engineers can focus on the application's business logic without spending much effort on the construction, management, and scaling of the infrastructure. The Big-3 cloud vendors (Amazon Web Services, Microsoft Azure, and Google Cloud) show immense interest in offering the best serverless services. For example, Amazon Lambda is the core serverless computing service for AWS (Amazon Web Services).
As shown in the below picture, different from the traditional deployment method, using Amazon Lambda, you only need to upload the source file, pick the execution environment and execute it. Then you can get the operation result. Freeing yourself from the steps of uploading and compiling in a server. In this process, server deployment, runtime installation, and compilation are all managed and executed by the Amazon Serverless computing platform. Engineers only need to maintain their source code and the relevant configuration of the execution environment.
The technology related to this is BaaS (Backend as a Service), which means that we no longer write and manage server-side components and completely outsource all parts of the application. Serverless is a new hosting environment in which the code runs.
Why Do We Need Serverless?
Serverless has quite a few advantages:
- For developers, serverless is more friendly. It can abstract the details of server management, allowing engineers to focus on the business code itself.
- In terms of cost, serverless goes by a pay-per-usage method.
- In terms of performance, it can autoscale according to the request traffics.
- It can also optimize code execution and response time by adjusting the amount of memory allocated to the function.
Why Do We Need an API Gateway When Using Serverless?
Serverless also has its weaknesses:
- The function URL is hardcoded into the application.
- Cumbersome authorization and authentication logic.
- Highly dependent on the cloud vendor: If the cloud vendor is down, the service is also down.
- Different cloud vendors have very different schemes with no specific standards, as migrating from one to another is usually costly.
API Gateways can solve these problems inherently. Combined together, serverless can become a robust architecture. The picture below describes how to assemble a simple Web Service with Amazon Serverless services quickly. API gateway is vital in providing authorization, route definition, and proxy requests. Taking Apache APISIX as an example, it offers serverless support for AWS and Azure. You can define a route to enable a serverless plugin instead of hardcoding function URLs into applications. With the flexibility of hot-updating function URIs, updating to different FaaS (Function as a service) cloud service providers becomes easy. Moreover, this approach alleviates the pain of cumbersome authorization and authentication.
Apache APISIX is a cloud-native API gateway under the Apache Software Foundation (ASF). Being dynamic, real-time, and high-performance, APISIX provides features like load balancing, dynamic upstream, canary release, circuit breaking, authentication, and observability. We can use Apache APISIX to handle traditional north-south and east-west traffic between services. At the same time, it can also be used as a K8s Ingress Controller. APISIX expands its ecosystem through built-in plugins, covering various usage scenarios of API gateways, such as authentication, security, observability, traffic management, multi-protocol access, etc. Of course, it also includes many serverless-related plugins.
The aws-lambda plugin integrates AWS Lambda with APISIX as a dynamic upstream to proxy all requests for a particular URI to the AWS cloud. When enabled, the plugin terminates the ongoing request of the configured URI and initiates a new request to the AWS Lambda Gateway URI on behalf of the client. This new request carries the previously configured authorization details, including request headers, body, and parameters (all three passed from the original request). It returns the response with headers, a status code, and the body to the client that initiated the request with APISIX. This plugin supports authorization via AWS API key and AWS IAM secrets.
Refer to APISIX or this blog for more information.
Serverless-related Plugins in Apache APISIX
In addition to Amazon Lambda, Apache APISIX also supports integration with serverless-related ecosystems such as Azure Function, Lua function, and Apache OpenWhisk, and provides the corresponding serverless plugins.
Table 1 Apache APISIX Serverless related plugin
|serverless||Users can upload custom Lua scripts through the serverless plugin and specify the code execution phase according to the configuration. For example, perform access control on requests in the access phase, modify the response header or response body in the header filter and body filter phases, print personalized logs in the log phase, etc. Also, since the serverless plugin is hot-reloaded, the modifications can take effect immediately without restarting Apache APISIX.|
|azure-functions||Used to integrate APISIX with Azure Serverless Function as a dynamic upstream to proxy all requests for a particular URI to the Microsoft Azure Cloud. When enabled, the azure-functions plugin terminates the ongoing request to the configured URI and initiates a new request to Azure Functions on behalf of the client. The new request carries the previously configured authorization details, including request header, body, and parameters (all three passed from the original request). Then, it returns back the response with headers, status code, and body to the client who initiated the request with APISIX.|
|openwhisk||Used to integrate APISIX with the open-source distributed serverless platform Apache OpenWhisk. When enabled, the plugin terminates the ongoing request to the configured URI and initiates a new request to OpenWhisk's API Host endpoint on behalf of the client, and the openwhisk plugin returns the response information to the client.|
|openfunction||Used to integrate APISIX with the open source distributed serverless platform CNCF OpenFunction as a dynamic upstream. When enabled, the plugin terminates the ongoing request for the configured URI and initiates a new request to the OpenFunction function on behalf of the client, and then the openfunction plugin returns the response information to the client.|
In recent years, with the emergence of the microservice architecture, everything has been migrating to the cloud, and many cloud service providers are also launching serverless-related products. Serverless-based development has become a very convenient development model. Keeping pace with cloud-natice trends, APISIX supports serverless computing by providing serverless, azure-functions, openwhisk, and openfunction plugins.
If you want to learn more about API gateway and its serverless features, contact us at https://api7.ai/contact.