Serverless architecture allows you to write and deploy applications without managing the underlying servers. It means software developers can focus on writing code without worrying about the infrastructure required to execute it. That can translate to greater scalability, flexibility, and faster time to value.
You can achieve even more when you combine serverless compute abilities with ’s API management. Here’s how.
and AWS Lambda – the perfect pairing
Many vendors offer serverless compute abilities. Today, we’re focusing on AWS Lambda. Lambda is a compute service that allows developers to build, debug and run applications. We previously discussedhow to leverage Lambda function URLs. Now, it’s time to look at how you can invoke AWS Lambda functions directly from ’s powerful, extensible API gateway!
Figure 1: An image depicting the invocation of an AWS Lambda function directly from -Gateway.
Methodology and code
You’ll need between 10 and 20 minutes to do this, along with the following system prerequisites:
Docker
GO Version: go1.20.10 darwin/amd64
git
IAM credentials with Lambda access
A Lambda function provisioned named “custom_lambda_function”
Follow these steps to begin:
Clone thecustom-go-plugin repository with the following:
This repository can be used with open source or licensed ; for this blog, we will assume you have a licensed version of . In order to obtain a license key, please visit the following link here.
The contents of reference 1 go in custom-go-plugin//bundle/manifest.json
The contents of reference 2 go in custom-go-plugin/go/src/CustomGoPlugin.go
Bring up the stack with the following command:
```
make
```
The contents of reference 3 are imported into the -Dashboard as an API.
Finally, reference 4 is an example of how to invoke the API which invokes an AWS Lambda behind the scenes.
Click to expand – Reference 1: A manifest.json file illustrating which plugin is being executed, where it’s executed and when it’s executed.
Click to expand – Reference 2: A CustomGoPlugin.go file specifying the required Go dependencies as well as the function InvokeLambda that will be triggered by the -Gateway.
package main
import (
"context"
"encoding/json"
"fmt"
"github.com/Technologies//log"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/service/lambda"
"github.com/aws/aws-sdk-go-v2/service/lambda/types"
"net/http"
)
var logger = log.Get()
func InvokeLambda(rw http.ResponseWriter, r *http.Request) {
// Read AWS Configuration
cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithRegion("us-east-1"),
config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(
"AWS_ACCESS_KEY_ID",
"AWS_SECRET_ACCESS_KEY",
"AWS_SESSION_TOKEN")),
)
if err != nil {
fmt.Println("Couldn't load default configuration. Have you set up your AWS account?")
fmt.Println(err)
return
}
function := r.Header.Get("function")
if len(function) == 0 {
function = "custom_lambda_function"
}
logger.Info("Function: '%v'", function)
lambdaClient := lambda.NewFromConfig(cfg)
payload, err := json.Marshal("")
invokeOutput, err := lambdaClient.Invoke(context.TODO(), &lambda.InvokeInput{
FunctionName: aws.String(function),
LogType: types.LogTypeNone,
Payload: payload,
})
rw.WriteHeader(http.StatusOK)
write, err := rw.Write(invokeOutput.Payload[:])
if err != nil {
return
}
if write > 0 {
}
}
func main() {}
func init() {
logger.Info("--- Go custom plugin v5 init success! ---- ")
}
Click to expand – Reference 3: An API definition that you can import through the Dashboard in order to leverage the Lambda
Click to expand – Reference 4: A sample curl call illustrating how you can invoke the API loaded from Reference 3, and specifying a function to invoke from the header.
Putting the power of serverless architecture in your hands
Server-based architectures rely on always-on servers to handle application processing. Serverless, by contrast, can offer you superior scalability and flexibility – plus the chance of faster time to value from end-to-end development. If that sounds like the approach you need, why not use with AWS Lambda to leverage the true power of serverless architecture?
You can chat to our team for further details or dive straight in and get started.
API platform governance & optimisation Build & architect API platforms Scaling API platforms