Hands-On Serverless Applications with Kotlin
上QQ阅读APP看书,第一时间看更新

Lambda proxy integration

AWS recommends that Lambda proxy integration is the way to integrate the API Gateway with Lambda.

The request that is received by the API Gateway is passed through to the Lambda function in a specific format. The Lambda function is expected to parse this payload into an appropriate type that it requires to perform the action.

The following code block shows the input passed to the Lambda function from the API Gateway in the case of a Lambda proxy integration:

{  
"resource":"Resource path",
"path":"Path parameter",
"httpMethod":"Incoming request's method name",
"headers":{
"key":"value"
},
"queryStringParameters":{
"key":"value"
},
"pathParameters":{
"key":"value"
},
"stageVariables":{
"key":"value"
},
"requestContext":{
"accountId":"12345678912",
"resourceId":"roq9wj",
"stage":"testStage",
"requestId":"deef4878-7910-11e6-8f14-25afc3e9ae33",
"identity":{
"cognitoIdentityPoolId":null,
"accountId":null,
"cognitoIdentityId":null,
"caller":null,
"apiKey":null,
"sourceIp":"192.168.196.186",
"cognitoAuthenticationType":null,
"cognitoAuthenticationProvider":null,
"userArn":null,
"userAgent":"PostmanRuntime/2.4.5",
"user":null
},
"body":"A JSON string of the request payload.",
"isBase64Encoded":"A boolean flag to indicate if the applicable request payload is Base64-encode"
}
}

The Lambda function is also expected to set the HTTP error codes and return the response object in a particular format:

{
"isBase64Encoded": false,
"statusCode": 200,
"headers": { "headerName": "headerValue"},
"body": "{\"key\":\"value\"}"
}

For more information on the Lambda proxy integration, please refer to the official documentation: API gateway documentation for Lambda Proxy integration.