# Intro to AWS Lambda Functions and Serverless Framework

*Are you looking for a tutorial about AWS Lambda Functions and Serverless Framework?* 🤔

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686903556875/ac673953-8c90-4a4b-bf3a-91e2989c53c6.gif align="center")

*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
        
    * Docs → [https://aws.amazon.com/lambda/getting-started/](https://aws.amazon.com/lambda/getting-started/)
        
* **Learn about Serverless Framework**
    
    * How to deploy Lambda Functions
        
    * How to iterate in development
        
    * How to clean your stack
        
    * Docs → [https://www.serverless.com/framework/docs/](https://www.serverless.com/framework/docs/)
        

***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
    

[Back to top](#top)

---

### 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 -&gt; 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/](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:**

1. Sign in for aws account.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686904370623/a553c9b7-286d-4914-9199-1dfd3eeeedaf.png align="center")
    
2. 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.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686904392418/7643ad36-faff-4739-937f-733a2279e47a.png align="center")
    
3. Go to Lambda services. (Type “Lambda” keyword in search bar then select the top result.)
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686904413712/1525835e-61ab-4b05-ac73-53719ead38a5.png align="center")
    
4. On the side menu select “Functions” then click the button “Create Function"
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686904431473/7442f28d-0752-4601-9a8c-79d5318daa85.png align="center")
    
5. 1. Tick the “Use a blueprint” box
        
    2. Search “hello-world” blueprint
        
    3. Tick “hello-world” box
        
    4. Click configure
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686904454221/09127500-cee0-477d-8296-0eb663d6b4ef.png align="center")
        
6. Add Function name then click Create Function.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686904614315/52287074-cc0d-4517-95c0-c513026d53d2.png align="center")
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686904623661/9c8822a3-2520-4352-bdfe-4c9820849be6.png align="center")
    
7. To test it, Under **Code source** click the “Test” button then inside the modal add an “Event Name” and click “Create”
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686904548250/2e0e3aa4-85c8-4e4a-b9d5-d3afccc8bcb7.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686904564338/45874097-ff64-4f23-9b0c-cd7680b80f22.png align="center")

1. Logs are printed inside the Execution results tab.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686904513132/8afe0ceb-d671-4207-8d55-b761e4b0958c.png align="center")

**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](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html)
    

1. Run this command to list your Lambda functions.
    
    ```plaintext
    aws lambda list-functions --region <Region example: ap-southeast-1>
    ```
    
2. Run this command to invoke your function.
    
    ```plaintext
    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!* 🎉

[Back to top](#top)

---

### Intro to Serverless Framework

* Serverless Framework ([https://www.serverless.com/framework/docs/](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.

1. Install NodeJS ([https://nodejs.org/en/download/](https://nodejs.org/en/download/) ) and AWS CLI ([https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html) )
    
2. Install the serverless framework. ([https://www.npmjs.com/package/serverless](https://www.npmjs.com/package/serverless) )
    
3. Set AWS Iam for the ‘serverless-admin’ user.
    
4. Download credentials on your machine.
    
5. 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:**

1. Install serverless in your terminal.
    
    ```plaintext
    npm install -g serverless
    ```
    
2. Create user role for serverless application in your AWS Console.
    
    1. Go to IAM service. (Type “iam” keyword inside the search box and select the top result)
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686904109855/7bf40fb2-f165-4ec4-9078-5f743ef39875.png align="center")
        
    2. Select “Users” in the side menu then click the “Add Users” button.
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686904124731/a3d69b1b-54f1-4b9a-9114-92969f66b1eb.png align="center")
        
    3. Add User name “serverless-admin” (recommended) then tick the Access type checkbox.
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686904145913/8fea7f25-193f-4dd7-b64b-a8bdc8dc5ebb.png align="center")
        
    4. Select the “Attach existing policies directly” and tick the “AdministratorAccess” inside the table.
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686904183054/02800efd-21d6-4d0d-b16d-78d242d9587c.png align="center")
        
    5. Skip adding tags.
        
    6. Review your choices, click “Create User” if you sure with your choices else go back to steps.
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686904210938/a7c93475-7b84-49a9-83c2-975cc9e7b4a4.png align="center")
        
    7. Download the .csv file to your local machine for using it later.
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686904231113/23d4bee2-6ec1-48c7-8b50-7ae26273156a.png align="center")
        
3. Setup serverless
    
    ```plaintext
    sls config credentials --provider aws --key <Access key ID> --secret <Secret access key> --profile <User you created ex. serverless-admin>
    ```
    
4. Go to your desired directory and create your nodejs boiler-template for nodejs
    
    ```plaintext
    sls create --template aws-nodejs --path hello-world-nodejs
    cd hello-world-nodejs
    ```
    
5. Edit serverless.yml file. (Add the profile and region key to provider property)
    
    ```plaintext
    service: hello-world-nodejs
    
    frameworkVersion: '2'
    
    provider:
      name: aws
      runtime: nodejs12.x
      lambdaHashingVersion: 20201221
      profile: serverless-admin
      region: ap-southeast-1
    ```
    
6. Deploy the function.
    
    ```plaintext
    sls deploy -v
    ```
    
7. Invoking/Running the function.
    
    ```plaintext
    sls invoke -f <Function name, in our tuts it is hello> -l
    ```
    
8. Updating the function only.
    
    ```plaintext
    sls deploy -f <Function name, in our tuts it is hello>
    ```
    
9. Deleting the service.
    
    ```plaintext
    sls remove
    ```
    
10. Fetching logs. (recommended: open another terminal window in case it’s not working)
    
    ```plaintext
    sls logs -f hello -t
    ```
    

*Congratulations you are now a certified Back-End Engineer!* *Well Done my dude!* 🎉

[Back to top](#top)

---

### 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! 🍷

[Back to top](#top)

---

### Related articles:

[https://www.serverless.com/blog/node-rest-api-with-serverless-lambda-and-dynamodb/](https://www.serverless.com/blog/node-rest-api-with-serverless-lambda-and-dynamodb/)

[Back to top](#top)
