How To Build Your First Serverless API with AWS Lambda and API Gateway

An in-depth tutorial

Andrew Bestbier
Oct 14, 2019 · 6 min read

Hi there. My name’s Andrew and I’m a JavaScript developer based in London.

In this piece I’ll be guiding you through the process of building a Serverless API on AWS.

I have written up a more complex follow up article here that would be an excellent next step once you have read this one.

Technologies Used

Prerequisites

Creating Your First Lambda Function

Click Create Function and you should see the following configuration screen:

We will use the default Author from scratch option for now.

Name your function (I called mine myFirstFunction), select Node.js for the runtime and Create a new role with basic Lambda permissions.

This means that our Lambda function will use JavaScript and AWS will create a role for us. If you are unfamiliar with AWS roles, they are essentially used to give AWS services access to other AWS services internally.

Finally, click Create function.

We now have our first function!

As you can see above, the page has an editor where you can modify the function code.

Change the “Hello From Lambda!” to something more interesting and click Save.

Now we can test the function. Click Test and a modal will appear where you will need to create a test event. We won’t be doing anything special here so name your event (I called mine MyTestEvent) and click Create.

Finally, click Test again and your function will run with the test event you just created. As you can see below, my function returned “Hello Andrew” in the body of the response:

Creating Your First Serverless API

Click Get Started, select REST and New API. Name your API (I called mine My First API), select Regional and click Create API. We use a Regional API here as using CloudFront to spin up our API around the world is out of the scope of this guide:

Now that your API has been created, you have many options available to you. You can create API Keys, use Custom Domain Names, implement authorisers etc. I recommend you conduct a thorough exploration after this tutorial!

For now, we are going to create a resource on your API. Click the Actions dropdown and then click Create Resource.

Name your resource (I named mine events) and click Create Resource.

Now we can add various methods on our new resource. Click the Actions dropdown and this time click Create Method:

A small dropdrown then appears with various HTTP methods to choose from. In our case, we are going to use the GET method. Once selected, click the circle tick icon.

On the right, you can now configure your new method. We are now going to link the GET events method to the Lambda function we created earlier. Select Lambda Function as the integration type and type the name of the function you created earlier before clicking Save.

A modal will appear requesting permission for API Gateway to invoke your function. Click OK.

AWS then shows a really neat diagram of the entire flow. You can click into any of the steps to modify your configuration if you wish.

Our final step is to deploy our new API resource and method. Click Actions again and Deploy API:

A modal then appears asking which stage you wish to deploy the API to. Create a new stage and name it production before clicking Deploy.

You will then be taken to the Stages screen where your deployed domain will be made available for you. Click the Invoke URL to test it out.

Remember to add events to the end of your provided URL. You should see the response from your Lambda function:

Now navigate back to your Lambda function and modify it to be more interesting. In this case, I return some mock events:

Conclusion

  • Add request validation
  • Use AWS Cognito to require authentication to access your APIs
  • Deploy the API to your own custom domain
  • Use API keys to limit the API use

As I mentioned earlier, I have written up a more complex article here that would be an excellent next step if you are interested in pursuing these ideas further.

Looking for technology-related content? We can create a newsfeed for you. It’s easy. 👋 Join us today and receive similar stories each week in your inbox!

️Follow us on LinkedIn, Twitter 🐦 and Facebook 👥 , and Instagram 📷

If this post was helpful, please click the clap 👏 button below a few times to show your support! ⬇

For any questions, contact AVM Consulting: blog@avmconsulting.net.

AVM Consulting Blog

AVM Consulting — Clear strategy for your cloud

Sign up for AVM Consulting

By AVM Consulting Blog

We are developing blogging to help community with Cloud/DevOps services Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Andrew Bestbier

Written by

Hi! I am a JavaScript developer based in London. I love reading and writing about JavaScript, AWS and all things coding.

AVM Consulting Blog

AVM Consulting — Clear strategy for your cloud

Andrew Bestbier

Written by

Hi! I am a JavaScript developer based in London. I love reading and writing about JavaScript, AWS and all things coding.

AVM Consulting Blog

AVM Consulting — Clear strategy for your cloud

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store