0

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

JamesG
  • 1,552
  • 8
  • 39
  • 86
  • Add this to your Lambda function (it's already in most of the examples) `console.log('Received event:', JSON.stringify(event, null, 2));` Then the event object should be printed nicely in your logs and you will be able to see all the properties that are available on it for you to access in your code. – Mark B Mar 22 '16 at 03:28
  • This gives me the output: **Received event: {}** When I test the lambda function – JamesG Mar 22 '16 at 03:31
  • Then you didn't properly map it in API Gateway. – Mark B Mar 22 '16 at 03:32
  • I posted the steps I took to map it. Can you see anything I have done wrong? – JamesG Mar 22 '16 at 03:34
  • p.s I saw you're AWS Certified in your profile - Thank you for helping me, much appreciated. – JamesG Mar 22 '16 at 03:35
  • @MarkB - Hey, I updated my answer, if you could help me that would be grand. Thank you – JamesG Mar 22 '16 at 04:10
  • You aren't calling context.done when your function is done. – Mark B Mar 22 '16 at 05:04
  • @MarkB - I do not believe this is the issue. It works in the lambda console - the result is the JSON I am getting however, it wont work when I test it user Postman – JamesG Mar 22 '16 at 05:38
  • @MarkB - Added new update to OQ – JamesG Mar 22 '16 at 06:01
  • http://stackoverflow.com/questions/31627950/aws-lambda-process-exited-before-completing-request – Mark B Mar 22 '16 at 16:52
  • @MarkB The accepted answer to that questions suggests that there is an error in the code that is not working. However, my code runs fine with no errors in the Lambda Console when I run test. This means my code has no errors, correct? – JamesG Mar 22 '16 at 16:57
  • You are arguing that the error message doesn't mean what everybody is telling you it means. How about adding some debug logging or something to work out what the error is then instead of spamming this site with every issue you run into and then arguing when we give you the answer. – Mark B Mar 22 '16 at 17:01
  • I am trying to understand why it works in one thing and not the other - I have not argued with everyone, I have tested all the methods provided. The problem seems to be with the api side. and stop getting arsy that your solutions are not working. If you dont want to help then don't its that simple. – JamesG Mar 22 '16 at 17:10
  • Obviously it's behaving differently when it is called differently. So you should debug it to understand why that is. Perhaps it isn't receiving a parameter your code is expecting or something. What does the event object look like in the logs when you call it from Postman? – Mark B Mar 22 '16 at 17:32
  • I am not sure how to get that in postman as I cannot see anything apart from the error stated in answer. Postman was only downloaded yesterday so not familiar with it. I have posted the API Call results from the API Gateway (Method > Test) is that is any help. – JamesG Mar 22 '16 at 17:42
  • You wouldn't get that "within postman" you would get it from the CloudWatch logs for your Lambda function. Learning how to view those logs is going to be key to solving all the issues you may run into using Lambda. – Mark B Mar 22 '16 at 17:47
  • Ah, this is different then the Lambda output: – JamesG Mar 22 '16 at 17:52
  • TypeError: Cannot read property 'appToken' of undefined at exports.handler (/var/task/index.js:12:35) – JamesG Mar 22 '16 at 17:52
  • I would suggest adding this as the very first line in your handler function: `console.log('Received event:', JSON.stringify(event, null, 2));` Then you will be able to see in your logs exactly what the function is being passed whenever it is called. I'm guessing you might need to be getting `event.apiToken` instead of `event.params.apiToken`. – Mark B Mar 22 '16 at 17:56
  • I have added console.log('Received event:', JSON.stringify(event, null, 2)); at the very end, and then tried it all over the function - From the API Gateway Results it does not even output the 'Received Event' In CloudWatch logs the result is: "method": "POST", "params": { "appToken": "gr5f4sgnca25hki98" } – JamesG Mar 22 '16 at 18:19
  • Sorry, I don't understand your last comment. You need to place that log as the FIRST line in your handler function, because your function is crashing so it won't log anything after the point that it crashes. – Mark B Mar 22 '16 at 18:28
  • Apologies for the delay, Dr had to change me Etoposide drip. Right, so I added that line in. It only shows in the Lambda Console not API Test or CloudWatch logs for Postman – JamesG Mar 22 '16 at 18:56

1 Answers1

0

You should be using 'event.apiToken' in your function based upon the mapping you gave us. {apiToken} is in your resource name right?

prestomation
  • 7,225
  • 3
  • 39
  • 37
  • Do you mean Gateway Resource? Nope, apiToken is the parameter. It works in the lambda console so that's correct? When I change to your edit I get: One or more parameter values were invalid: An AttributeValue may not contain an empty string. – JamesG Mar 22 '16 at 07:24