In this blog we'll create a simple serverless app using AWS lambda and API Gateway.
"Serverless" what is it? ๐ค
No servers obviously right? nope you got that wrong
Here's what it is: In serverless computing, infrastructure management tasks like capacity provisioning and patching are handled by third party (in our case AWS), so you can focus on only writing code that serves your customers.
How to Serverless?
AWS Lambda
AWS Lambda is a compute service that lets you run code without provisioning or managing servers - As per AWS
AWS API Gateway
Amazon API Gateway is a fully managed service that makes it easy for developers to create, publish, maintain, monitor, and secure APIs at any scale - As per AWS
In easy words Lambda is where you write your code and API Gateway is where you define routes to execute those Lambda functions, let's say you wrote a lambda function for login functionality and whenever someone hits xyz.com/login you want to execute the login Lambda you can do so by Connecting Lambda to API Gateway and configuring the route /login.
You know what it is, now let's see how it works ๐
- Login to your AWS account
- Open lambda console
- Then click on "Create function"
- Select "Author from scratch", type the function name, select an execution role ( learn how to create execution role )
- You'll see a screen like this here you can see the code editor, type your code and press deploy to deploy it you can run the function by clicking on "Test" button
Congratulations ๐ you have successfully created your first lambda function
Now, Let's connect to API Gateway
- Go to API Gateway console by searching "API Gateway" in services section
- Click on "Create API"
- Select Rest API
- Type in the name and press "Create API"
- Now let's add a path, Click on "Actions" and then "Create Resource"
- Type the path name and hit "Create Resource"
- You can now see a path is created as "/test"
- Click on "Actions" and "Create Method"
- Choose "Get"
- Type in the lambda function name you created earlier and click on "Save"
- And finally Click on "Actions" and then "Deploy API" ( learn how to create stages )
Wohoo!! you have have finally created a serverless app ๐ฅณ๏ธ You'll see the link to your serverless app by clicking on "Stages" and the stage you have created You can test the API by hitting the /test and you'll see the logs in lambda console
Thank you for reading ๐ค learn more about AWS serverless framework
Written by Syed Zubair Ahmed Twitter Instagram Website LinkedIn