63

Is it possible to invoke a AWS Lambda function directly by a http request (either GET or POST)? If not, is there a workaround going through SNS or S3? Because I can't think of one...

Specifically, I would like to create a small API using JSON

Moshe Shaham
  • 15,448
  • 22
  • 74
  • 114
  • Could you explain a bit more your use case? Invoke from where? `curl`? The browser? By default, [invoke](http://docs.aws.amazon.com/lambda/latest/dg/API_Invoke.html) has an invocation type of `RequestResponse` which is essentially an HTTP request. You can even see the request syntax via the link. – William Gaul Apr 26 '15 at 18:36
  • We have built a few tools for this. It's all public. Feel free to fork it. https://github.com/bespoken/bstpy and https://github.com/bespoken/bst. – Bela Vizy Nov 06 '16 at 14:51

6 Answers6

51

The AWS API Gateway is the only way to expose your lambda function over HTTP. The AWS lambda web console should create one automatically for you if you use the microservice-http-endpoint blueprint when creating a new lambda function.

enter image description here

Or you can set it from the following screen enter image description here

Or create it from the AWS API Gateway web console. enter image description here


You may also explore invoking it on demand using the SDK in your client, mobile or web app. More information here.

user3526
  • 1,412
  • 1
  • 19
  • 26
  • 5
    But once you have this API gateway setup, how do you hit it? Under stages/prod I'm given every possible HTTP METHOD and a common URL for them. Hitting the URL gives `{"message":"Missing Authentication Token"}` – Bruno Bronosky Feb 09 '17 at 22:02
  • 6
    The new Application Load Balancer can now invoke Lambda function – tu4n Mar 26 '19 at 17:05
  • 4
    You can use an Amazon SDK to invoke a Lambda function, which under the hood uses HTTP to the AWS Lambda API. Such a request requires security credentials in order to be properly signed. – Graham Lea Nov 12 '19 at 04:17
  • 3
    Be careful with statements like "X is the only way to do Y". Even when the answer was posted, you could trigger lambda from SNS, which in turn could be triggered from HTTP – DiegoAlfonso Feb 02 '21 at 17:29
23

Yes, you can invoke AWS Lambda function using HTTP POST method, for this you need to make sigv4 signed request with valid AWS IAM credentials.

I tested this using POSTMAN:

curl -X POST https://lambda.us-west-2.amazonaws.com/2015-03-31/functions/Function_NAME/invocations

Link: Invoke Syntax

Tejas
  • 239
  • 2
  • 3
  • 7
    Can you please share how to sigv4 sign the request? How did you generate the authorization header? – Binod Karunanayake Oct 31 '19 at 09:41
  • 2
    @BinodKarunanayake It's pretty complex. You're better off using an AWS SDK if you can. But here's the details anyway: https://docs.aws.amazon.com/general/latest/gr/sigv4-signed-request-examples.html – Graham Lea Nov 12 '19 at 04:18
  • 1
    I think you can add the `X-Amz-Invocation-Type: ` header to invoke with a specific invocation type if you're doing it this way, just as an addendum. – trademark Jun 07 '21 at 20:59
9

Just add a trigger in the lambda function and you can already send a GET or POST requests in the link that will be generated

Go to your lambda function and click the trigger section

Create a trigger and set the security to "open", if you want it to be publicly accessible. Else, configure based on your own needs

Carmela
  • 687
  • 1
  • 8
  • 15
9

As of 28 Nov 2018, you can create an internet facing LoadBalancer with an HTTP listener, then configure the load balancer to have Lambda as its target group.

After the load balancer is created, you can use its DNS name as the HTTP endpoint to sent requests to Lambda.

More details here: https://aws.amazon.com/blogs/networking-and-content-delivery/lambda-functions-as-targets-for-application-load-balancers/

LiriB
  • 814
  • 8
  • 12
7

Lambda now has built-in HTTPS endpoints that you can use to invoke the lambda. This can be enabled in the Advanced settings pane with the Enable function URL option:

enter image description here

You can find more information on this feature here.

Paolo
  • 21,270
  • 6
  • 38
  • 69
1

Yes, you can. There's an API for this: Invoke

MatteoSp
  • 2,940
  • 5
  • 28
  • 36
  • 2
    You might clarify with "but you will need to [sign it](http://docs.aws.amazon.com/general/latest/gr/signing_aws_api_requests.html)". – Tim Erickson May 15 '16 at 05:37