Harshita
Harshita

Reputation: 392

How to pass a path parameter in API gateway to invoke lambda function?

I need to pass a path param in the API.

By using the mapping template, I am able to pass query params and use them in the function.

Mapping template:

{
     "Id": "$input.params('Id')" //this works fine after passing params as <url>?param=vale
}

With reference to this I created the mapping template as following-

{
    "Id": "$input.params().querystring.get('Id')" // requirement is to be able to use <url>/value
}

I tried using 'Method request template' under genrate template, but it didn't work either.

When I invoke the URL urlname.execute-api.us-east-2.amazonaws.com/stag/functionname, it gives the value as undefined.

This is how I'm using the param:

class Lambda {
    static run(event, context, callback) {
      callback(null, 'Id '+ event.Id);
    }  
  }
  module.exports = Lambda;

Also, please tell me how to use those params in code. Be kind :)

Upvotes: 0

Views: 1364

Answers (1)

Assael Azran
Assael Azran

Reputation: 2993

In order to be able to use <url>/value and get value from event follow this (Tested):

Configure your API gateway resource

enter image description here

enter image description here

Under /api2/{id} - GET - Integration Request configure your Mapping Templates

enter image description here

Execute request https://123456.execute-api.my-region.amazonaws.com/stage/api2/123

Lambda

console.log(event.id)
callback(null, {
    id:event.id
});

CloudWatch

enter image description here

Hope this helps

Upvotes: 2

Related Questions