I am hoping that someone can help me get a URL Parameter passed from the AWS API Gateway in Lambda.
I am a little confused how to access this inside the Lambda code.
For the API Gateway I have followed these steps:
1) Went to Resources -> Integration Request
2) Clicked on the plus icon next to templates dropdown
3) Explicitly typed application/json in the content-type field
4) For input mapping Entered:
{ "apiToken": "$input.params('apiToken')" }
So for clarification, How do I access the parameter apiToken in my Lambda Function?
I have been looking at the walkthroughts and other docs for a while now and my brain is a little frazzled.
I appreciate all help and feedback :)
Update
So, the issue seemed to be that the AWS website AJAX save did not save what I entered in the mapping (I am on hotel wifi so could be that).
Now, When I test it I get:
Received event: { appToken: '123456' }
In the console log for lambda. However, When I go to the API Gateway, and deploy The response is:
"errorMessage": "Process exited before completing request"
Any Ideas?
P.S The function works in the Lambda Console however, when I test it using Postman [POST + Param] it does not work, with the error message supplied just above
Update 2
Here is my Entire Lambda Function
console.log('Loading event');
var AWS = require('aws-sdk');
var dynamodb = new AWS.DynamoDB({apiVersion: '2012-08-10'});
var http = require('http');
exports.handler = function(event, context) {
var tableName = "clients";
var appToken = event.params.appToken; //gr5f4sgnca25hki98
dynamodb.getItem({
TableName: tableName,
Key: {
ClientID: { S: appToken } }
}, function (err, data)
{
if (err) {
context.done(err);
} else {
var url = data.Item.url.S + data.Item.api.S;
console.log(url);
console.log('Received event:', event.params.appToken);
http.get(url, function(res) {
// Continuously update stream with data
var body = '';
res.on('data', function(d) {
body += d;
});
res.on('end', function() {
var result = body.replace('\\', '');
context.succeed(JSON.parse(result));
});
res.on('error', function(e) {
context.fail("Got error: " + e.message);
});
});
}
});
};
In my test event I have:
{
"method": "POST",
"params": {
"appToken": "gr5f4sgnca25hki98"
}
}
When I run this in Lambda the respnse works, it connects ro an external JSON file and returns it.
However, In Postman I get the error:
"errorMessage": "Process exited before completing request"
Can anyone see why in the console it all works fine and when I try it out in deployment I still get the error?
When I open XCODE and test this url with Alamofire I get this error (if it helps)
Invalid Sequence around character 72
Update 3 (Test from API Console)
When tested from the API console, using:
{
"method": "POST",
"params": {
"appToken": "gr5f4sgnca25hki98"
}
}
in the Request Body. I get:
Request: /sendtoclient
Status: 200
Latency: 1839 ms
Response Body
{ "errorMessage": "Process exited before completing request" }
Response Headers
{"Content-Type":"application/json"}
Logs
Execution log for request test-request
Tue Mar 22 17:28:27 UTC 2016 : Starting execution for request: test-invoke-request
Tue Mar 22 17:28:27 UTC 2016 : HTTP Method: POST, Resource Path: /sendtoclient
Tue Mar 22 17:28:27 UTC 2016 : Method request path: {}
Tue Mar 22 17:28:27 UTC 2016 : Method request query string: {}
Tue Mar 22 17:28:27 UTC 2016 : Method request headers: {}
Tue Mar 22 17:28:27 UTC 2016 : Method request body before transformations: {
"method": "POST",
"params": {
"appToken": "gr5f4sgnca25hki98"
}
}
Tue Mar 22 17:28:27 UTC 2016 : Endpoint request body after transformations: { "appToken": "" }
Tue Mar 22 17:28:29 UTC 2016 : Endpoint response body before transformations: {"errorMessage":"Process exited before completing request"}
Tue Mar 22 17:28:29 UTC 2016 : Endpoint response headers: {x-amzn-Remapped-Content-Length=0, x-amzn-RequestId=123456789, Connection=keep-alive, Content-Length=59, X-Amz-Function-Error=Unhandled, Date=Tue, 22 Mar 2016 17:28:29 GMT, Content-Type=application/json}
Tue Mar 22 17:28:29 UTC 2016 : Method response body after transformations: {"errorMessage":"Process exited before completing request"}
Tue Mar 22 17:28:29 UTC 2016 : Method response headers: {Content-Type=application/json}
Tue Mar 22 17:28:29 UTC 2016 : Successfully completed execution
Tue Mar 22 17:28:29 UTC 2016 : Method completed with status: 200
I'm not sure how to decipher this. Any help would be very much appreciated. I have however noticed this:
Endpoint request body after transformations: { "appToken": "" }
Which for some reason I feel may be significant