ScanSkill
Sign up for daily dose of tech articles at your inbox.
Loading

Building An API Using AWS Lambda and API Gateway

Building An API Using AWS Lambda and API Gateway
Building An API Using AWS Lambda and API Gateway

In this article, we will learn about building an API using AWS lambda and API gateway. AWS  Lambda is an Amazon Web Service compute service that lets you run code without managing or provisioning servers. Lambda runs your code on a high-availability compute infrastructure with all computing resources managed, including server and operating system maintenance, capacity provisioning, automatic scaling, and logging.

API Gateway is an AWS service that provides the ability to create, publish, secure, maintain, monitor, and maintain APIs at any scale. As an API developer, you can make APIs available to third-party or use them in your own client applications. API gateways act as reverse proxy servers, accepting API requests, aggregating the various services required to fulfill them, and returning appropriate responses.

Prerequisites

  1. AWS account
  2. Basic AWS Lambda and API Gateway concept

Building an API using AWS Lambda and API Gateway

Create a Lambda Function

  • Go to AWS Console and Log In
  • In the search bar, type lambda
  • Select the Lambda options displayed under Services section
lambda service
lambda service
  • Then AWS Lambda page will appear and click the Create function button on the right side of the page.
  • Then let’s create a function named apiWithLambda . Then click Create Function.
create lambda function
create lambda function
  • After some time a lambda function will be created and you will be redirected to the following page.
  • Now, let’s test the lambda function by clicking the Test under Code Source and then, setting up a test event.
lambda function
lambda function
  •  Now, click on create new event and specify an Event name as testEvent, any name you like, before clicking save.
create test event
create test event
  •  After that, click Test to verify whether the Lambda function was executed or not. We can view the execution logs in the output. The text Hello from Lambda! should appear in the log output along with a 200 response code.
lambda execution result
lambda execution result

Build API Gateway

  • In the search bar, type api gateway
  • Select the API Gateway options displayed under Services section
api gateway service
api gateway service
  • Now, go to the dashboard of API Gateway and click Create API button.
  • Then, Under the REST API section click Build.
rest api
  • After completing above steps, now click on the New API option under the Create new API section and also enter the desired API name.
api creation
api creation
  • Now, On the page for configuration. Go to Resource> Actions > create Method. Create a GET method and select the Lambda Function option as the integration type. Then select the name of the Lambda function previously created and click Save.
  • Then, after the completion of lambda integration following page will appear.
  • Now, let’s deploy the API so that it can be accessed through browsers.
  • For that, Go to Resource> Actions > Deploy API.
  • After that, select the [New Stage] option for Deployment stage.
  • Then, enter your desired name for a Stage name and click Deploy.
  •  After that, the API will be deployed. And, you should now see the API’s URL.
  • Now, copy the invoke URL and paste it into your browser.
  • You can see the following output in the browser.
lambda browser response
lambda browser response

Conclusion

In this article, we’ve learned about building an API using AWS lambda and API gateway.

Sign up for daily dose of tech articles at your inbox.
Loading