I get the following methodArn
from the event payload when I invoke my Lambda Authorizer in my API Gateway:
arn:aws:execute-api:us-east-1:account-id:api-id/stage/GET/entity
I would like to get the name of the associated Lambda or, preferably, the ARN of the Lambda associated with that API Gateway method.
How can I do it?
Retreiving the Lambda can only be done through the AWS SDK. You need to go through the following steps to get to the implementation URI of the Lambda.
Steps:
In Code, this would look something like this.
var aws = require('aws-sdk');
var apigateway = new aws.APIGateway();
exports.handler = (event, context, callback) => {
//sample methodArn: 'arn:aws:execute-api:eu-west-1:1234567890:p2llm1cs/prod/GET/blah/gerfea'
var section = event.methodArn.substring(event.methodArn.lastIndexOf(':') + 1);
var restId = section.split('\/')[0];
var method = section.split('\/')[2];
var path = section.substring(section.indexOf(method) + method.length);
//getting the resources for the given API
var params = {
restApiId: restId,
};
apigateway.getResources(params, function(err, data) {
//iterate through all attached resources
for (var idx = 0; idx < data.items.length; idx++) {
var item = data.items[idx];
if (item.path == path) {
//get the integration for the matching resource.
var params = {
httpMethod: method,
resourceId: item.id,
restApiId: restId
};
apigateway.getIntegration(params, function(err, data) {
//sample data.uri arn:aws:apigateway:eu-west-1:lambda:path/2015-03-31/functions/arn:aws:lambda:eu-west-1:1234567890:function:echo/invocations
console.log(data.uri);
callback(null, 'Hello from Lambda');
});
}
}
});
};