Intro to AWS Lambda Functions and Serverless Framework

·

7 min read

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

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


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:

  1. Sign in for aws account.

  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.

  3. Go to Lambda services. (Type “Lambda” keyword in search bar then select the top result.)

  4. On the side menu select “Functions” then click the button “Create Function"

    1. Tick the “Use a blueprint” box

      1. Search “hello-world” blueprint

      2. Tick “hello-world” box

      3. Click configure

  5. Add Function name then click Create Function.

  6. To test it, Under Code source click the “Test” button then inside the modal add an “Event Name” and click “Create”

  1. Logs are printed inside the Execution results tab.

Steps on how to run your Lambda Function in AWS CLI.

  1. Run this command to list your Lambda functions.

     aws lambda list-functions --region <Region example: ap-southeast-1>
    
  2. 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! 🎉

Back to top


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.

  1. Install NodeJS (https://nodejs.org/en/download/ ) and AWS CLI (https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html )

  2. Install the serverless framework. (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.

     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)

    2. Select “Users” in the side menu then click the “Add Users” button.

    3. Add User name “serverless-admin” (recommended) then tick the Access type checkbox.

    4. Select the “Attach existing policies directly” and tick the “AdministratorAccess” inside the table.

    5. Skip adding tags.

    6. Review your choices, click “Create User” if you sure with your choices else go back to steps.

    7. Download the .csv file to your local machine for using it later.

  3. Setup serverless

     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

     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)

     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.

     sls deploy -v
    
  7. Invoking/Running the function.

     sls invoke -f <Function name, in our tuts it is hello> -l
    
  8. Updating the function only.

     sls deploy -f <Function name, in our tuts it is hello>
    
  9. Deleting the service.

     sls remove
    
  10. 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! 🎉

Back to 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


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

Back to top

Â