0
votes

I have created an AWS API Gateway to invoke a Lambda function to generate random numbers:

Lambda Function :

exports.handler = (event, context, callback) => {
let min = parseInt(event.min);
let max = parseInt(event.max);
let generatedNumber = Math.floor(Math.random() * max) + min;
context.done(null, {generatedNumber: generatedNumber});
};

Body mapping Template in API gateway for get method:

{
    "min" : $input.params('min'),
    "max" : $input.params('max')
}

When I access API endpoint like below: https://abcdefgh.execute-api.ap-south-1.amazonaws.com/DEV/number?min=10&max=20

I get the proper response :

{"generatedNumber":28}

But when I try to access the API in node.js using aws-api-gateway-client I am receiving the below response :

_currentUrl: 'https://abcdefgh.execute-api.ap-south-1.amazonaws.com/DEV/number' },
  response: undefined

The current url should be set to 'https://abcdefgh.execute-api.ap-south-1.amazonaws.com/DEV/number?min=20&max=40' but it is set to 'https://abcdefgh.execute-api.ap-south-1.amazonaws.com/DEV/number'.

Here is my node.js code to access this api:

let AWS = require('aws-sdk');
AWS.config.loadFromPath('./config.json');
//AWS.config.region = 'ap-south-1';
let lambda = new AWS.Lambda();
let apigClientFactory = require('aws-api-gateway-client').default;
let config = {
    invokeUrl: 'https://abcdefgh.execute-api.ap-south-1.amazonaws.com/DEV',
    accessKey: '<access-key>',
    secretKey: '<secret-key>',
    region: 'ap-south-1'
};
let apigClient = apigClientFactory.newClient(config);
let apiParams = '{"min": 20,"max": 40}';
let body = {
    
}

let additionalParams = {
   
}
apigClient.invokeApi(apiParams, '/number', 'GET', additionalParams, body)
    .then(function (result) {
        console.log(result);
    })
    .catch(function (error) {
        console.log(error);
    });

I tried changing apiParams to :

let apiParams = {"min": 20,"max": 40};

The I receive the below error: '{"message": "Could not parse request body into json: Unexpected character (\\\',\\\' (code 44)): expected a value\\n at [Source: [B@42feb146; line: 2, column: 14]"}' } } What is wrong in my code?

Thanks in advance

2
Have you enabled CORS configuration? If yes, then have you set endpoint(means deployed API)? Maybe you should try body : JSON.stringify(data) in coming response. Also set Content-Type : application/json. Check in POST method that integration has lambda as integration-type. Also enable lambda proxy integration. - Meet Zaveri
CORS Enabled: yes. API Deployed : yes. Should I add body : JSON.stringify(data) in Integration Response or should I use JSON.stringify(result) in the then function. I don't have a POST method. I enabled lambda proxy integration. But now I am obtaining Internal Server Error both through node js and through directly visiting the link. - Abhilash D K
Internal server error occurs mainly due to lambda function's response. Check Cloudwatch logs. Yes if that is GET request then, JSON.parse(data) should be used. But if there was POST method, then only stringify method should be used. I don't have my personal aws account at current, I use company's account. So I can't assist you go-to-straight help dude! - Meet Zaveri
Thanks. I will look into Cloudwatch logs - Abhilash D K

2 Answers

0
votes

Try modifying the mapping template:

{
    "min" : "$input.params('min')",
    "max" : "$input.params('max')"
}

Source: input-variable-reference

0
votes

I found the problem. I need to pass parameters in additionalParmaeters object like :

let additionalParams = {
   queryParams: {
   min: 20, max: 40
   }
}

But the text

var params = {
    //This is where any header, path, or querystring request params go. The key is the parameter named as defined in the API
    userId: '1234',
};

is misleading because query parameters were not passed when parameters were added to params object ( maybe it was for me ), but were only passed when passed inside additionalPrams.

Hope it helps.