Transform responses
Configure Gloo Gateway to wrap the request to or unwrap the response from an AWS Lambda as if the gateway proxy is an AWS API Gateway.
When a Lambda function is exposed by an AWS API Gateway, the response of the function must be returned in a specific format. The gateway proxy in Gloo Gateway can understand this format and process the response in the same way as an AWS API gateway. Additionally, you can wrap requests to the Lambda function in the same format that an AWS API gateway uses. Applying a transformation to format requests to and responses from your function can help provide a smooth migration from AWS gateways to Gloo Gateway.
Wrap requests or unwrap responses
Use the AWS Lambda console to create a Node.js function named
unwrap-test. Use the following code for the function.export const handler = async(event) => { const response = { "statusCode": 201, "headers": { "key": "value" }, "isBase64Encoded": false, "multiValueHeaders": { "X-Custom-Header": ["My value", "My other value"], }, "body": JSON.stringify({TotalCodeSize: 104330022,FunctionCount: 26}) } return response; };Apply the
requestFormat: APIGatewayorresponseFormat: APIGatewaytransformations in a GlooTrafficPolicy resource. Note that you can apply one or both transformations in one resource.kubectl apply -f - <<EOF apiVersion: gloo.solo.io/v1alpha1 kind: GlooTrafficPolicy metadata: name: lambda-unwrap-transform namespace: gloo-system spec: glooTransformation: awsLambda: requestFormat: APIGateway responseFormat: APIGateway EOFCreate a Backend resource to reference the
unwrap-testfunction. The required fields vary based on your authentication method.Create an HTTPRoute resource that references the
lambda-unwrapBackend and thelambda-unwrap-transformGlooTrafficPolicy.kubectl apply -f - <<EOF apiVersion: gateway.networking.k8s.io/v1 kind: HTTPRoute metadata: name: lambda-unwrap namespace: gloo-system spec: parentRefs: - name: http namespace: gloo-system rules: - matches: - path: type: PathPrefix value: /unwrap-test backendRefs: - name: lambda-unwrap namespace: gloo-system group: gateway.kgateway.dev kind: Backend filters: - type: ExtensionRef extensionRef: name: lambda-unwrap-transform group: gloo.solo.io kind: GlooTrafficPolicy EOFSend a curl request to the
unwrap-testfunction to check the response body format.The response is unwrapped in the format required by AWS API gateways:
{"TotalCodeSize":104330022,"FunctionCount":26}
Cleanup
You can optionally remove the resources that you set up as part of this guide.
Delete the HTTPRoute, Backend, and GlooTrafficPolicy resources.
kubectl delete HTTPRoute lambda-unwrap -n gloo-system kubectl delete Backend lambda-unwrap -n gloo-system kubectl delete GlooTrafficPolicy lambda-unwrap-transform -n gloo-systemUse the AWS Lambda console to delete the
unwrap-testfunction.