There has been a lot of talk about serverless and AWS lambda these days. More and more companies and teams are switching towards the serverless architecture for their applications.
In this article, I will be explaining serverless and we will be deploying our first hello world lambda function!
What is Serverless?
Serverless simply means that you don’t have to manage the servers on which your application runs.
You don’t have to take care of patching the system, installing antivirus software or configuring firewalls. Also, you don’t have to worry about scaling your application as the load increases, it is handled automatically!
Hence, it allows you to focus more on the functionality of your application.
Why is it better?
Normally, when you are creating a dynamic web application you usually create a server in PHP, Nodejs, Ruby or Python which interact with a database to fetch information and send it to the Frontend.
Then you host this backend server on hosting providers like GoDaddy, AWS or Azure. The problem with this approach is that your server is running 24 x 7. It is running even when no one is interacting with your application.
Because of this you end paying even for the idle time. Along with this, most of the time you are also responsible for patching the system for bugs, keeping the antivirus software updated and setting up autoscaling.
What is AWS Lambda?
This is AWS’s offering of building serverless applications. In this, you can create lambda functions that are executed only when some action is performed.
The action performed can be someone visiting your website so your functions fetch some data from the database and return the response. You can create these functions in a number of languages like Python, Nodejs, Java, Go, Ruby, C#, .NET etc.
The best part is that you only pay for the execution duration of the function. When your function is not running, you don’t have to pay anything.
Similarly to lambda, other cloud providers have their own serverless offerings like Microsoft Azure offers Azure functions and Google Cloud Platform offers Cloud Functions.
Features of Lambda
- Pay per execution.
- Create an event-driven architecture. Eg —Trigger your lambda function once an image is uploaded to Amazon S3 to add a watermark to the image or change its format.
- Built-in Fault Tolerance.
- Automatic Scaling.
- Integrated security model (Industry compliances).
Now enough with the introduction, let’s create your first lambda function.
Creating a lambda function
1. Log in to your AWS console and select Lambda under Compute tab. You will be taken to the lambda dashboard. You will see either of the two below images.
2. Now, let’s click on Create a function button. It will open up a form where you have to enter some basic details for your lambda function.
3. You have three ways of creating a lambda function –
a. Creating from scratch and writing all the code from starting.
b. Using a blueprint of a lambda function from common use cases
c. Choose a sample function from AWS Serverless Repository.
If you are building something that is commonly used by others then you may find a similar function in the last two options. Using them may reduce your development time.
Here I will be creating a function from scratch. Let’s fill in some details.
4. Give your function a name of your choice. Then you can select the language in which you would like to run this function. I am selecting the Node.js 8.10 here.
Now you specify an IAM role that defines what permissions your lambda function has. Eg — If your function will be interacting with S3, just give S3 access to its assigned role. Because of this your function is limited to access to S3 only and cannot interact with any other AWS service (#Security!).
For now select create a new role option and give your role a name.
Once done, click on the Create function button.
5. Your lambda function will be created now. By default, a lambda function has access to CloudWatch logs where it logs all the executions of the function along with any STDOUT based events like console.log for Nodejs and print for python.
If you scroll down, you can see your function code. By default, it just outputs “Hello from Lambda!”.
If you scroll even further, you can add environment variables, description and allocated memory to your function along with tags that help you with billing and filtering.
The timeout of your function signifies that after this much time your function will be automatically stopped if it keeps on running.
Our function is created now but to run it we need to add some trigger to it.
6. Scroll to the top and select API Gateway from the left panel. You will see a form coming up at the bottom. Select Create a new API in the API field and Open in the Security field. By open we mean that anyone will be able to access this API with the generated URL.
Once done, click on Add button and then click on the Save button at the right top of the page to save your function.
Your API has been created and will be accessible through the given link. If you click on the link it will open a new tab where you will see “Hello from Lambda!”.
Try changing this text to something else, save your function and reload the API page. You will see the updated text.
Congratulations! You have created your first lambda function and API using API Gateway.
I will be writing more articles on lambda and serverless so stay tuned!
Thanks for reading this article. If you liked it, please give a few claps so it reaches more people who would love it!
What is AWS Lambda or Serverless? was originally published in Hacker Noon on Medium, where people are continuing the conversation by highlighting and responding to this story.