Intro to AWS Lambda Functions and Serverless Framework
Are you looking for a tutorial about AWS Lambda Functions and Serverless Framework? đ€
This page is loaded with information regarding AWS Lambda Functions and Serverless Framework, it has also tutorials that are very helpful and easy to understand, so what are you waiting for? Open your favorite IDE and Start Coding! đ»
Overview
Learn about AWS Lambda
Lambda Functions
Architecture
Events
Learn about Serverless Framework
How to deploy Lambda Functions
How to iterate in development
How to clean your stack
Who commonly uses this technology?
Developers - Who wants to learn about the AWS Lambda functions and Serverless framework.
DevOps - Who wants to learn how to properly deploy AWS Lambda functions.
Solutions Architect - Who wants to understand how Serverless Apps are architected, built and deployed.
Pre-requisites:
Basic knowledge in NodeJS
Basic knowledge about AWS cloud
Basic knowledge on how to use terminals
Basic knowledge in Windows/Linux/Mac
Intro to AWS Lambda
Before we proceed we need to answer this questions first.
FAQâs:
What is AWS?
Cloud Provider.
They provide you with the servers and services that you can use on demand and scale easily.
AWS, Azure, and Google Cloud has revolutionized IT over time.
Back then IT companies uses their own physical servers that is kinda costly because of it's high maintainability.
Today most IT companies rents cloud servers like AWS because of the many advantages it provides.
Powers biggest websites like netflix and many more.
Recently introduced AWS Lambda Service.
What is EC2 Service?
Virtual servers in the cloud.
Limited RAM and CPU.
Continuously running.
Scaling means intervention to add or remove servers.
What is Lambda Service?
Virtual Functions.
No servers to manage.
Limited by time -> short executions.
Run on demand.
Scaling is automated.
You manage only the functions.
What are the benefits of Lambda Service?
Easy pricing
Pay per request and compute time.
Free tier of 1,000,000 Lambda request and 400,000 GBs of compute time.
Integrated with the whole AWS suite of services.
Example: (Commonly used stack)
API Gateway for REST API and invokes AWS Lambda Functions.
Kinesis for real time streaming of data and uses Lambda to do some data transformation on the fly.
DynamoDB for nosql DBMS and triggered Lambda functions when something happens in the database.
AWS S3 for simple storage service and triggered Lambda functions when a file is created inside the storage.
AWS IOT for internet of things.
CloudWatch Events for CRON-jobs.
CloudWatch Logs for streaming of logs.
AWS SNS for simple notification service.
AWS Cognito for simple user identity and data synchronization service
Integrated with many programming languages.
Node.js (JavaScripts)
Python
Java (Java 8 Compatible)
C# (.NET Core)
Golang
C# / Power Shell
Ruby
Custom Runtime API (Community Supported example: RUST)
Note: Docker is NOT AWS Lambda Function, Itâs for ECS or Fargate
Easy to get more resources per functions up to 3GB of RAM.
Increasing RAM will also improve CPU network.
What is the pricing of AWS Lambda? (As of this year 2021)
You can find overall pricing information here: https://aws.amazon.com/lambda/pricing/
Pay per calls:
First 1,000,000 request are free.
$0.20 per 1 million requests thereafter ($0.0000002 per request).
Pay per duration: (in increment of 100ms)
4000,000 GB-seconds of the compute time per month if FREE
\== 400,000 seconds if function is 1GB RAM
\== 3,200,000 seconds if function is 128 MB RAM
After that $1.00 for 600,000 GB-seconds
It is usually very cheap to run AWS Lambda so itâs very popular.
After answering all this question you probably have an Idea what is AWS Lambda functions purpose, now we will carry through the usage of it by following the tutorial below.
In this tutorial we will create a simple lambda function just to give you an idea how to create, run, and test it
Tutorial:
Sign in for aws account.
Select the most nearest region you are in.
Assuming you are in the Philippines you should select Singapore to reduce latency, minimize costs, or address regulatory requirements.
Go to Lambda services. (Type âLambdaâ keyword in search bar then select the top result.)
On the side menu select âFunctionsâ then click the button âCreate Function"
Tick the âUse a blueprintâ box
Search âhello-worldâ blueprint
Tick âhello-worldâ box
Click configure
Add Function name then click Create Function.
To test it, Under Code source click the âTestâ button then inside the modal add an âEvent Nameâ and click âCreateâ
- Logs are printed inside the Execution results tab.
Steps on how to run your Lambda Function in AWS CLI.
You must have AWS CLI v2 first on your local machine.
Guide on how to download AWS CLI v2 â https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html
Run this command to list your Lambda functions.
aws lambda list-functions --region <Region example: ap-southeast-1>
Run this command to invoke your function.
aws lambda invoke --function-name <Function name example: hello-world> --cli-binary-format raw-in-base64-out --payload <Payload example: '{"key1": "value1", "key2": "value2", "key3": "value3"}'> --region <Region example: ap-southeast-1> response.json
Disclaimer:
We are not responsible any data loss or any overridden lambda functions.
Use your personal AWS account to avoid overriding your intercompany lambda functions, unless you are sure what you are doing.
Congratulations you have now your very first AWS Lambda functions! đ
Intro to Serverless Framework
Serverless Framework (https://www.serverless.com/framework/docs/ ) aims to ease the pain of creating, deploying, managing, and debugging Lambda functions.
It integrates well with Continuous Integration/Continuous Delivery tools
It has CloudFormat support so your entire stack can be deployed using this framework.
Advantages:
It uses programming not manual clicks in the AWS console.
Can be integrated with continuous integration framework.
Can be integrated with continuous delivery framework.
Basics usage are to be tackled in this tutorial.
Make sure to install the dependencies before proceeding to tutorial.
Install NodeJS (https://nodejs.org/en/download/ ) and AWS CLI (https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html )
Install the serverless framework. (https://www.npmjs.com/package/serverless )
Set AWS Iam for the âserverless-adminâ user.
Download credentials on your machine.
Setup serverless to use these credentials.
Tutorial:
In this tutorial we will create hello-world lambda function using the serverless framework.
Open your IDE and follow along! Lezzz go! đȘ
Steps:
Install serverless in your terminal.
npm install -g serverless
Create user role for serverless application in your AWS Console.
Go to IAM service. (Type âiamâ keyword inside the search box and select the top result)
Select âUsersâ in the side menu then click the âAdd Usersâ button.
Add User name âserverless-adminâ (recommended) then tick the Access type checkbox.
Select the âAttach existing policies directlyâ and tick the âAdministratorAccessâ inside the table.
Skip adding tags.
Review your choices, click âCreate Userâ if you sure with your choices else go back to steps.
Download the .csv file to your local machine for using it later.
Setup serverless
sls config credentials --provider aws --key <Access key ID> --secret <Secret access key> --profile <User you created ex. serverless-admin>
Go to your desired directory and create your nodejs boiler-template for nodejs
sls create --template aws-nodejs --path hello-world-nodejs cd hello-world-nodejs
Edit serverless.yml file. (Add the profile and region key to provider property)
service: hello-world-nodejs frameworkVersion: '2' provider: name: aws runtime: nodejs12.x lambdaHashingVersion: 20201221 profile: serverless-admin region: ap-southeast-1
Deploy the function.
sls deploy -v
Invoking/Running the function.
sls invoke -f <Function name, in our tuts it is hello> -l
Updating the function only.
sls deploy -f <Function name, in our tuts it is hello>
Deleting the service.
sls remove
Fetching logs. (recommended: open another terminal window in case itâs not working)
sls logs -f hello -t
Congratulations you are now a certified Back-End Engineer! Well Done my dude! đ
Summary:
We learned how to create, deploy, run, stream logs, destroy lambda functions using the serverless framework!
I hope youâve learned a lot from this tutorials and apply it in real world. âș
Cheers! đ·
Related articles:
https://www.serverless.com/blog/node-rest-api-with-serverless-lambda-and-dynamodb/