1

I have a multi-endpoint webservice written in Flask and running on API Gateway and Lambda thanks to Zappa.

I have a second, very tiny, lambda, written in Node, that periodically hits one of the webservice endpoints. I do this by configuring the little lambda to have Internet access then use Node's https.request with these options:

const options = {
  hostname: 'XXXXXXXXXX.execute-api.us-east-1.amazonaws.com',
  port: 443,
  path: '/path/to/my/endpoint',
  method: 'POST',
  headers: {
    'Authorization': `Bearer ${s3cretN0tSt0r3d1nTheC0de}`,
  }
};

and this works beautifully. But now I am wondering whether I should instead make the little lambda invoke the API endpoint directly using the AWS SDK. I have seen other S.O. questions on invoking lambdas from lambdas but I did not see any examples where the target lambda was a multi-endpoint webservice. All the examples I found used new AWS.Lambda({...}) and then called invokeFunction with params.

Is there a way to pass, say, an event to the target lambda which contained the path of the specific endpoint I want to call? (and the auth headers, etc.) * * * * OR * * * * is this just a really dumb idea, given that I have working code already? My thinking is that a direct SDK lambda invocation might (is this true?) bypass API Gateway and be cheaper, BUT, hitting the endpoint directly via API Gateway is better for logging. And since the periodic lambda runs once a day, it's probably free anyway.

If what I have now is best, that's a fine answer. A lambda invocation answer would be cool too, since I've not been able to find a good example in which the target lambda had multiple https endpoints.

2
  • What is the purpose of your periodic Lambda? Is it simply for reducing cold starts or is there some business purpose for it (such as cron tasks)? Commented Dec 28, 2018 at 0:11
  • It's a cron.... one of the endpoints does a little analytic computation. Yes the "little lambda doing the cron thing" could have had access to the (Aurora) database, but the way it is now, the API is the sole thing sitting in front of the database for now. Commented Dec 28, 2018 at 5:23

1 Answer 1

1

You can invoke the Lambda function directly using the invoke method in AWS SDK.

 var params = {
  ClientContext: "MyApp", 
  FunctionName: "MyFunction", 
  InvocationType: "Event", 
  LogType: "Tail", 
  Payload: <Binary String>, 
  Qualifier: "1"
 };
 lambda.invoke(params, function(err, data) {
   if (err) console.log(err, err.stack); // an error occurred
   else     console.log(data);           // successful response
   /*
   data = {
    FunctionError: "", 
    LogResult: "", 
    Payload: <Binary String>, 
    StatusCode: 123
   }
   */
 });

Refer the AWS JavaScript SDK lambda.invoke method for more details.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks. I'm well aware of that documentation page, but what I don't know is where to put the /path/to/my/endpoint in the invocation. Nothing on that page seems to indicate how this is done. What am I missing?
What are you referring as endpoint? Are you referring to API Gateway endpoint?
Well I wrote a Flask app with 30 "endpoints" or whatever you call them. I used Zappa to push the Flask app up to AWS as a single lambda function. I want to make an HTTPS request to just one of those 30 endpoints. I realize API Gateway has its own notion of endpoint. But I mean endpoint as in one of the things in a classic REST API. This seems like such a simple thing to do, yet I'm flustered by not seeing any example anywhere, which leads me to believe that what I am trying to do is stupid, and I should leave well enough alone and go with what I already have, which is working.
To elaborate. My lambda is a flask app at XXXXXXXXXX.execute-api.us-east-1.amazonaws.com. As a webservice, it exposes things like POST /things, GET /things, GET /things/{id}, DELETE /things/{id}, POST /dailyreport/things. My question: How do I do a lambda invocation that makes an HTTPS POST call to ONLY the POST /dailyreport/things "endpoint." I understand that "endpoint" might not be the right word in AWS-speak, but I was wondering if the AWS SDK can do this. This lambda does not look like a "single function" that is easy to hit with lambda invoke. Or is it???
It seems like you have already created an API Gateway mapping. The URL xxxx.execute-api.us.... is an API Gateway URL. You won&#39;t need any SDK to connect the Lambda function. Check your API Gateway configurations in API Gateway section in AWS Web Console.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.