Day 7 of AWS
Today I am going to set up a project using Serverless Framework!
Before that we have get to know about serverless-framework!
Is that new to you????
No worries, I will make it easy.
With that said, let’s get started!
What is this Buzz-word “Serverless” is all about?
Let's deep-drive
If you remember, when we were in college, we were taught all about Servers, Storage, DBs, and whatnot!? We all understood that in order to host an application, somewhere we need to maintain a Server, and not just that, as and when our application grows, the Servers also need to be upgraded and they have to be maintained, and also security is the biggest aspect!
So, in today's rapidly evolving tech landscape, efficiency and scalability are crucial. This is where serverless computing in the cloud steps in, transforming the way businesses deploy and manage applications.
In simple terms, "serverless" refers to a cloud computing model where you don't have to worry about managing servers. Instead of dealing with server infrastructure, you can focus on writing code for your applications, and the cloud provider takes care of everything behind the scenes. Serverless cloud uses a combination of aws sdk, cli and cloud dashboard to easily building, deploy and monitor the application.
You need to have a separate team altogether to handle all these things right?
Well, Now! Nothing of that is required! AWS provides you an out-of-the-box Service called AWS Lambda, which lets you focus on the actual code, rather than having the Servers to be maintained, and that’s what is Serverless, having no Servers at all!
What is AWS Lambda?
The biggest confusion that a newbie gets is, how can we focus on just code and not the servers? how can we not have a server and make our application run? Well, the simple thing to understand here is, that rather than you hosting the entire application, you will now host the code!
Next confusion, Wait what? how do you host just the code?
Lambda stores your code in an internal S3 bucket that is private to your AWS account, and that’s how the code is hosted on the AWS console, and with the help of API Gateway you can associate a web API to a lambda, which routes the application request to the specified lambda function, and that’s how the building blocks of your application are created.
You can find more information on AWS Lambda here.
What is Serverless Framework?
Serverless Framework is a well-known framework & open-source framework for building and deploying serverless applications and to develop, deploy and maintain your serverless resources on AWS. Serverless framework provides you with IaC (Infrastructure as Code) which is a way of creating, deploying and maintaining your services or resources on the cloud with machine-readable configuration files.
With Serverless Framework, developers can write and deploy serverless applications without having to worry about the underlying infrastructure. The framework allows developers to define their application as a set of functions, events, and resources in a YAML or JSON file, which can then be deployed to the cloud provider of their choice.
The Serverless Framework supports multiple programming languages, including Node.js, Python, Java, and Go. It also provides a plugin architecture, allowing developers to extend its functionality and integrate it with other services.
Overall, the Serverless Framework is a powerful tool for simplifying the development and deployment of serverless applications.
Go to the serverless framework and create an account by signing up.
How does Serverless work?
As explained above Serverless is nothing but a tool that internally integrates several cloud services of defined cloud providers(AWS in our case) and automates the infrastructure formation. Going forward in our project this Serverless tool will work with several AWS services such as CloudFormation,aws lambda, S3, API gate way etc and provide the resources for our deployment.
Serverless Framework Installation
Serverless Framework is made on NodeJS hence, before installing Serverless Framework we need to have NodeJS installed on our system to support it.
Update the APT
sudo apt-get update
Install Node.js 14 on Ubuntu 22.04|20.04|18.04
Use this site for installing Node Installation
curl -sL https://deb.nodesource.com/setup_14.x | sudo bash -
Once the repository is added, you can begin the installation of Node.js 14 on Ubuntu Linux
sudo apt -y install nodejs
Verify the version of Node.js and NPM installed.
You can install repected versions using nvm.
# Use node 14 or above
node -v
npm -v
Install Serverless module via NPM
sudo npm install -g serverless
The above command will install the Serverless Framework globally on your computer. To check if it is properly installed, you can just check the version in the command prompt.
serverless --version
Serverless pair-up with aws Lamba as they are technology partners. To access the AWS console we need to install aws cli and configure it
Installing AWS CLI
Refer the Link
sudo apt install awscli
aws --version
That’s it! we are now done, but hold on, make sure your AWS Credentials are configured on your computer, for which you need to install the AWS CLI on your computer. Refer the Link
This is important not only for Serverless Framework but also for interacting with AWS Services. So go ahead! Do it if you haven’t configured it, it’s a one-time thing!
Now that we are all set, let’s get started on Deploying our Lambda function, it’s a quick and small process, follow the below steps!
Create a folder and open it in VSCode.
Type serverless
in your terminal and press enter, you will be shown a prompt as below.
These are the options for templates, here you can select anyone by using the up and down arrow keys on your keyboard, I have selected AWS-Python-Starter
Once you select it, you will be asked to give a name for your project. Specify any name of your choice and press enter. Since we are building a serverless password manager I have given the name is sayHello
Once it is done, a python template will be downloaded into your folder.
Next, you will be asked what org do you want to add this service? Now, this is with respect to your serverless dashboard, you should have a serverless account to use the dashboard, kritikashaw
is my username, if you don’t have an account, you can choose to skip this part.
We are almost done! be careful in this next step, it asks Do you want to deploy now? If you have multiple AWS accounts configured in your computer, this option will take the default account and deploy your lambda wherever the default account’s credentials point to! I chose No because I want to deploy it later.
That’s it! our initial project setup is done. All we have to do now is test it locally and see if it works fine! The below files will be generated once we have run all the above commands.
Below is the minimal code that is provided in the template.
To test it out locally I would suggest you create a dictionary called event
inside the function because we are going to simulate the request that comes to this lambda function.
event = {“greeting”: “Hello World”}
You can add this as the first line inside the function.
The serverless.yml
is the most important file, it contains information regarding our AWS resources, and we have to specify the resource configurations in this file itself.
This is the serverless configuration provided by the template, most of the things are self-explanatory, the service
is our project name, and it has described the serverless framework version as well. The main thing to focus on is the functions
. Every lambda function that you create as a part of this project must be registered in this place, the hello
is the function name, and handler
is the path to the function. If you noticed, the filename is handler.py
and the function name is hello
.
That’s all! Now to test this, all you have to do is type the following command.
sls invoke local -f <func_name> -p <input_file_path> --region <aws_region> --aws-profile <profile_name>
sls invoke local -f <func_name>
Let's define this command, -f
stands for function name that you have registered in the serverless.yml
file, note that function names specified inside the .py
files can be different and the ones that you register in the serverless.yml
file can be different.
-p
is for the input file path, since we already created a dictionary called as event
in the function itself, we didn’t use this. However, if you want to pass that dictionary as a request, create a JSON file and create an object inside it, and provide the relative path of that file to this parameter.
--region
This is the AWS region, if you don’t provide this, the default one will be picked.
--aws-profile
As I said, there can be multiple accounts configured on your computer, you can use this parameter to specify in which account this function exists.
Alright, we have done everything, the only thing that is pending is the deployment of our lambda function.
All you need to do is type serverless deploy
or sls deploy
you can use --aws-profile
along with this command to specify where to host the lambda function. Once you run this command, a CloudFormation stack will run, you can open your AWS console and see the progress by going to AWS CloudFormation as it is getting deployed! Make sure you are in the same region that you have specified on your computer.
You can use sls
or serverless
for commands, both resemble the same.
And this is how your code looks in the console.
You can also test it by clicking on the Test button given on Lambda Console.
Alright with that said, This tutorial ends here! I hope you it was worth your time. For more information, check out these links to docs: Serverless Framework and AWS Lambda.
Thank you so much for taking the time to read till the end! Hope you found this blog informative and helpful.
Feel free to explore more of my content, and don't hesitate to reach out if need any assistance from me or in case of you have any questions.
Happy Learning!
~kritika :)
Connect with me: LinkedIn