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

  1. 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;
    };
      
  2. Apply the requestFormat: APIGateway or responseFormat: APIGateway transformations 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
    EOF
      
  3. Create a Backend resource to reference the unwrap-test function. The required fields vary based on your authentication method.

  4. Create an HTTPRoute resource that references the lambda-unwrap Backend and the lambda-unwrap-transform GlooTrafficPolicy.

      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
    EOF
      
  5. Send a curl request to the unwrap-test function 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.

  1. 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-system
      
  2. Use the AWS Lambda console to delete the unwrap-test function.