Transform responses
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
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; };Create a Backend resource to reference the
unwrap-testfunction. The required fields vary based on your authentication method.Make sure to add thepayloadTransformMode: Nonesetting to your Backend. By default, the payload transformation mode is set toEnvoy, which automatically transforms the payload. To prevent double transformation for AWS Lambdas later, change this setting toNone.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 EOFkubectl 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 EOFCreate an HTTPRoute resource that references the
lambda-unwrapBackend and thelambda-unwrap-transformEnterpriseKgatewayTrafficPolicy.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 EOFBefore you apply the transformation policy, send a curl request to the
unwrap-testfunction 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}"}Apply the
requestFormat: APIGatewayorresponseFormat: APIGatewaytransformations 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 EOFSend a curl request to the
unwrap-testfunction 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.
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-systemUse the AWS Lambda console to delete the
unwrap-testfunction.