DEV Community

Cover image for How do I add an API Gateway trigger to a AWS Lambda function?
Tomasz Łakomy
Tomasz Łakomy

Posted on • Updated on • Originally published at tlakomy.com

How do I add an API Gateway trigger to a AWS Lambda function?

Before we start

If you don't feel like reading and prefer to watch a bunch of quick (less than 3 minutes) videos - this blog series is based on a 💰free 💰egghead.io video collection I've published recently, check it out here: Learn AWS Lambda from scratch.


Once we create an AWS Lambda function we probably want to be able to call it (otherwise, why bother creating a function at all?).

As we established in the previous posts, an AWS Lambda function can be triggered by various events - including an HTTP request, which [citation needed] is probably the most common use case.

With AWS Lambda you can create a serverless REST API in no time!

Let's start with a brand new lambda function, which as you can see - by default has no triggers.

A brand new AWS Lambda function in AWS Console

Consider clicking on the Add trigger button, who knows what will happen!

AWS Lambda trigger menu

As we can see, there's a surprising number of things that can trigger a lambda function. Maybe one day we'll be able to trigger lambda by just thinking about it, who knows.

In any case - let's select API Gateway from the list and let's create an API from scratch:

API Gateway creation screen

HTTP API and REST API are the most googleable names *EVER*

We can choose between HTTP API and REST API. HTTP API is still in beta but it's cutting edge and we want to one of the cool kids - let's go with that.

Leave everything as default and click on Add.

(Quick note: by default the newly created API will be open, so we'll be able to access it from the Internet. Not a huge deal now, but we might consider being more careful once our lambda functions deal with sensitive data etc.)

Lo and behold!
We have created an API Gateway and connected it to our Lambda function

API Gateway trigger connected to AWS Lambda

(Consider adding this skill to your LinkedIn, more than happy to endorse you)

Our brand new API endpoint will look more or less like this:

https://RANDOM_STUFF.execute-api.eu-central-1.amazonaws.com/default/MY_FUNCTION_NAME

And once we paste it into our browser, something magical will happen - a "Hello from Lambda!" string will appear on our screen 🎉

This is fantastic - now we're able to create our own APIs in no time, and the whole process takes minutes (if not seconds!)


I hope you've learned something interesting - in the next post we're going to learn how to add logs to AWS Lambda function and review them in AWS CloudWatch 🌟

Or you can check the video series here and feel free to reach out to me on Twitter!

Top comments (1)

Collapse
 
omarhoumz profile image
Omar Houmz

How can we configure CORS for our functions?