Monday, January 16, 2017

Amazon API Gateway Velocity Template to Pass All Data to AWS Lambda

Lambda Integration Velocity Template

## See http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html
## This template will pass through all parameters including path, querystring, header, stage variables, and context through to the integration endpoint via the body/payload


## allParams contains three members ($type below): path, querystring, header
## each member in turn is object with 0..n members

## stage-variables can defined for a particular "stage" of API Gateway

#set($allParams = $input.params())
{


"body-json" : $input.json('$'),

"params" : {
#foreach($type in $allParams.keySet())
#set($params = $allParams.get($type))
"$type" : {
#foreach($paramName in $params.keySet())
"$paramName" : "$util.escapeJavaScript($params.get($paramName))"
#if($foreach.hasNext),#end
#end
}
#if($foreach.hasNext),#end
#end
},


"stage-variables" : {
#foreach($key in $stageVariables.keySet())
"$key" : "$util.escapeJavaScript($stageVariables.get($key))"
#if($foreach.hasNext),#end
#end
},


"context" : {
"account-id" : "$context.identity.accountId",
"api-id" : "$context.apiId",
"api-key" : "$context.identity.apiKey",
"authorizer-principal-id" : "$context.authorizer.principalId",
"caller" : "$context.identity.caller",
"cognito-authentication-provider" : "$context.identity.cognitoAuthenticationProvider",
"cognito-authentication-type" : "$context.identity.cognitoAuthenticationType",
"cognito-identity-id" : "$context.identity.cognitoIdentityId",
"cognito-identity-pool-id" : "$context.identity.cognitoIdentityPoolId",
"http-method" : "$context.httpMethod",
"stage" : "$context.stage",
"source-ip" : "$context.identity.sourceIp",
"user" : "$context.identity.user",
"user-agent" : "$context.identity.userAgent",
"user-arn" : "$context.identity.userArn",
"request-id" : "$context.requestId",
"resource-id" : "$context.resourceId",
"resource-path" : "$context.resourcePath"
}
}

Data Received by AWS Lambda

{
"body-json": {
"appid": "myapp",
"eventtime": "2017-01-16",
"eventtype": "init",
"key1": "k1",
"key2": "k2",
"json": {
"astr": "str",
"anum": 123,
"bool": true
}
},
"params": {
"path": {},
"querystring": {},
"header": {
"Accept": "application/json",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "en-US,en;q=0.8",
"Cache-Control": "no-cache",
"CloudFront-Forwarded-Proto": "https",
"CloudFront-Is-Desktop-Viewer": "true",
"CloudFront-Is-Mobile-Viewer": "false",
"CloudFront-Is-SmartTV-Viewer": "false",
"CloudFront-Is-Tablet-Viewer": "false",
"CloudFront-Viewer-Country": "US",
"Content-Type": "application/json",
"Host": "5tt5f8xji6.execute-api.eu-west-1.amazonaws.com",
"Origin": "chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop",
"Postman-Token": "bb496633-fe87-142d-95ea-31d1ee34ebca",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36",
"Via": "1.1 64184371bdc0c4545cde799b5949d17c.cloudfront.net (CloudFront)",
"X-Amz-Cf-Id": "k9f8O0GfedOrKH2UUI10lt0QHBMbsPGyz-0GdwJpTWasNB_n9fMdgA==",
"X-Forwarded-For": "23.120.xx.xxx, 54.240.xxx.xx",
"X-Forwarded-Port": "443",
"X-Forwarded-Proto": "https"
}
},
"stage-variables": {},
"context": {
"account-id": "",
"api-id": "5tt5f8xji6",
"api-key": "",
"authorizer-principal-id": "",
"caller": "",
"cognito-authentication-provider": "",
"cognito-authentication-type": "",
"cognito-identity-id": "",
"cognito-identity-pool-id": "",
"http-method": "POST",
"stage": "prod",
"source-ip": "23.120.xx.xxx",
"user": "",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36",
"user-arn": "",
"request-id": "f5ab9082-dc73-11e6-8ca5-15a998eb4e77",
"resource-id": "kmtrw2",
"resource-path": "/trace"

}
}