2017-07-26 156 views

回答

1

只能通過AWS SDK完成Retreiving Lambda。您需要完成以下步驟以獲取Lambda的實現URI。

步驟:

  • 獲取API資源的API
  • 獲取整合爲資源
  • 從目標提取LAMBDA ARN URI

代碼,這將看起來像這樣。

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'); 
     }); 
     } 
    } 
    }); 
}; 
相關問題