Skip to content
You are viewing the latest documentation for Solo Enterprise for kgateway, formerly known as Gloo Gateway. To access the documentation for older Gloo Gateway versions, such as 2.0 and 1.x, use the version switcher.

Transform responses

Page as Markdown

Configure the control plane 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 Solo Enterprise for kgateway 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 Solo Enterprise for kgateway.

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. Create a Backend resource to reference the unwrap-test function. The required fields vary based on your authentication method.

    kubectl apply -f - <<EOF
    apiVersion: gateway.kgateway.dev/v1alpha1
    kind: Backend
    metadata:
      name: lambda-unwrap
      namespace: kgateway-system
    spec:
      type: AWS
      aws:
        region: ${AWS_LAMBDA_REGION}
        accountId: "${AWS_ACCOUNT_ID}"
        auth:
          type: Secret
          secretRef:
            name: aws-creds
        lambda:
          functionName: unwrap-test
          payloadTransformMode: None
    EOF
    kubectl apply -f - <<EOF
    apiVersion: gateway.kgateway.dev/v1alpha1
    kind: Backend
    metadata:
      name: lambda-unwrap
      namespace: kgateway-system
    spec:
      type: AWS
      aws:
        region: ${AWS_LAMBDA_REGION}
        accountId: "${AWS_ACCOUNT_ID}"
        lambda:
          functionName: unwrap-test
          payloadTransformMode: None
    EOF

  3. Create an HTTPRoute resource that references the lambda-unwrap Backend and the lambda-unwrap-transform EnterpriseKgatewayTrafficPolicy.

    kubectl apply -f - <<EOF
    apiVersion: gateway.networking.k8s.io/v1
    kind: HTTPRoute
    metadata:
      name: lambda-unwrap
      namespace: kgateway-system
    spec:
      parentRefs:
        - name: http
          namespace: kgateway-system
      rules:
       - matches:
         - path:
             type: PathPrefix
             value: /unwrap-test
         backendRefs:
         - name: lambda-unwrap
           namespace: kgateway-system
           group: gateway.kgateway.dev
           kind: Backend
    EOF
  4. Before you apply the transformation policy, send a curl request to the unwrap-test function to check the raw response format.

    curl $INGRESS_GW_ADDRESS:8080/unwrap-test -H "Host: lambda.${AWS_LAMBDA_REGION}.amazonaws.com"
    curl localhost:8080/unwrap-test -H "Host: lambda.${AWS_LAMBDA_REGION}.amazonaws.com"

    Example raw response:

    {"statusCode":201,"headers":{"key":"value"},"isBase64Encoded":false,"multiValueHeaders":{"X-Custom-Header":["My value","My other value"]},"body":"{\"TotalCodeSize\":104330022,\"FunctionCount\":26}"}
  5. Apply the requestFormat: APIGateway or responseFormat: APIGateway transformations in a EnterpriseKgatewayTrafficPolicy resource. Note that you can apply one or both transformations in one resource.

    kubectl apply -f - <<EOF
    apiVersion: enterprisekgateway.solo.io/v1alpha1
    kind: EnterpriseKgatewayTrafficPolicy
    metadata:
      name: lambda-unwrap-transform
      namespace: kgateway-system
    spec:
      targetRefs:
      - group: gateway.networking.k8s.io
        kind: HTTPRoute
        name: lambda-unwrap
      entTransformation:
        awsLambda:
          requestFormat: APIGateway
          responseFormat: APIGateway
    EOF
  6. Send a curl request to the unwrap-test function to check the updated response body format.

    curl $INGRESS_GW_ADDRESS:8080/unwrap-test -H "Host: lambda.${AWS_LAMBDA_REGION}.amazonaws.com"
    curl localhost:8080/unwrap-test  -H "Host: lambda.${AWS_LAMBDA_REGION}.amazonaws.com"

    The response is now 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 EnterpriseKgatewayTrafficPolicy resources.

    kubectl delete HTTPRoute lambda-unwrap -n kgateway-system
    kubectl delete Backend lambda-unwrap -n kgateway-system
    kubectl delete EnterpriseKgatewayTrafficPolicy lambda-unwrap-transform -n kgateway-system
  2. Use the AWS Lambda console to delete the unwrap-test function.