OneCompiler

Introduction to Serverless - Part 1

698

What is Serverless?

Serverless is a framework to help develop and deploy AWS lambda functions along with lambda event resources. It manages code and also infrastructure.

Pre-requisites

  1. Node.js
  2. AWS account (or) any cloud account

Functions

  1. A function is a lambda function, it may be Nodejs function or Java methods like that.
  2. It is independent deployment and execution like microservice.
  3. This function will be deployed in the cloud.
  4. Examples of functions
  • Saving users to the database
  • Upload file to s3
  • Add job to Queue
  • Process schedulers

Events

  1. Events is an AWS resource it triggers lambda function.
  2. Examples of events
  • AWS API Gateway HTTP endpoint request
  • AWS S3 bucket upload
  • AWS DynamoDB table updates

Resources

  1. Resources are AWS resources used by the lambda function.
  2. Examples of resources
  • AWS DynamoDB Table
  • AWS S3 bucket
  • AWS SNS Topic

Services

  1. A service is a collection of lambda functions along with resources.
  2. Service is like a project file, it contains all lambda function names and resource configurations.
  3. The service file name should be serverless.yml or serverless.json or serverless.js.
  4. Sample service file
service: first-app
provider:
  name: aws
  runtime: nodejs10.x

  stage: dev
  region: us-east-1

functions:
  hello:
    handler: handler.hello
    events:
      - http:
          path: getData
          method: GET
      - http:
          path: postData
          method: POST


# you can add CloudFormation resource templates here
resources:
  Resources:
    UploadFiles:
      Type: AWS::S3::Bucket
      Properties:
        BucketName: 3nath-bucket

    TableCreation:
      Type: AWS::DynamoDB::Table
      Properties:
        TableName: ${self:service}-users-table
        AttributeDefinitions: 
          - AttributeName: userId
            AttributeType: S
          - AttributeName: userName
            AttributeType: S
        KeySchema:
          - AttributeName: userId
            KeyType: HASH
          - AttributeName: userName
            KeyType: RANGE
        ProvisionedThroughput:
          ReadCapacityUnits: 1
          WriteCapacityUnits: 1