Skip to content

Instantly share code, notes, and snippets.

@nikkaroraa
Created September 26, 2020 11:56
Show Gist options
  • Save nikkaroraa/387a9835a89d79e679585eeb8c4de605 to your computer and use it in GitHub Desktop.
Save nikkaroraa/387a9835a89d79e679585eeb8c4de605 to your computer and use it in GitHub Desktop.
serverless

Serverless

What's serverless?

  • Serverless is a new paradigm in which the developers don't have to manage servers anymore...
  • Just deploy code
  • Or just deploy functions
  • Initially, serverless === FaaS (Functions as a Service)
  • Serverless was pioneered by AWS Lambda but now it also includes anything that's managed: "databases, messaging, storage, etc"
  • Serverless doesn't mean there are no servers. It means you just don't manage / provision / see them

Serverless in AWS

  • AWS Lambda
  • DynamoDB
  • AWS Cognito
  • AWS API Gateway
  • Amazon S3
  • AWS SNS & SQS
  • AWS Kinesis Data Firehose
  • Aurora Serverless
  • Step Functions
  • Fargate

Why AWS Lambda

  • Amazon EC2

    • Virtual servers in the cloud
    • Limited by RAM and CPU
    • Continuously running
    • Scaling means intervention to add / remove servers
  • Amazon Lambda

    • Virtual functions - no servers to manage
    • Limited by time - short executions
    • Run on-demand
    • Scaling is automated

Benefits of AWS Lambda

  • Easy Pricing
    • Pay per request and compute time
    • Free tier of 1,000,000 AWS Lambda requests and 400,000 GBs of compute time
  • Integrated with the whole AWS suite of services
  • Integrated with many programming languages
  • Easy monitoring through AWS CloudWatch
  • Easy to get more resources per functions (up to 3GB of RAM)
  • Increasing RAM will also improve CPU and network

AWS Lambda Pricing

  • Pay per calls
    • First 1,000,000 requests are free
    • $0.20 per 1 million requests thereafter ($0.0000002 per request)
  • Pay per duration (in increment of 100ms)
    • 400,000 GB-seconds of compute time per month is 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

Lambda Synchronous Invocations

  • Results is returned right away
  • Error handling must happen client side (retries, exponential backoff, etc)
  • Services
    • User Invoked
      • Elastic Load Balancing (Application Load Balancer)
      • Amazon API Gateway
      • Amazon CloudFront (Lambda@Edge)
      • Amazon S3 batch
    • Service Invoked
      • Amazon Cognito
      • AWS Step Functions
    • Other Services
      • Amazon Lex
      • Amazon Alexa
      • Amazon Kinesis Data Firehose

Lambda Integration with ALB (Application Load Balancer)

  • To expose a Lambda function as an HTTP(s) endpoint...
  • You can use the Application Load Balancer (or an API Gateway)
  • The Lambda function must be registered in a target group
  • ALB converts HTTP(s) request to JSON so as to invoke the Lambda function (and vice versa)
  • ALB "multi-header values" option converts HTTP headers and query string parameters that are sent with multiple values are shown as arrays within the AWS Lambda event and response objects.

Lambda@Edge

  • You have deployed a CDN using CloudFront

  • What if you wanted to run a global AWS Lambda alongside?

  • Or how to implement request filtering before reaching your application

  • You can use Lambda to change CloudFront requests and responses

    • After CloudFront receives a request from a viewer (viewer request)
    • Before CloudFront forwards the request to the origin (origin request)
    • After CloudFront receives the response from the origin (origin response)
    • Before CloudFront forwards the response to the viewer (viewer response)
    • You can also generate responses to viewers without every sending the request to the origin

Lambda - Asynchronous Invocations

  • S3, SNS, CloudWatch events
  • The events are placed in an Event Queue
  • Lambda attempts to retry on errors
    • 3 tries total
    • 1 minute wait after 1st, then 2 minutes wait
  • Make sure the processing is idempotent (in case of retries)
  • If the function is retried, you will see duplicate log entries in CloudWatch logs
  • Can define a DLQ (dead-letter queue) - SNS or SQS - for failed processing (need correct IAM permissions)
  • Services
    • Amazon Simple Storage Service (S3)
    • Amazon Simple Notification Service (SNS)
    • Amazon CloudWatch Events / EventBridge
    • AWS CodeCommit (CodeCommit Trigger: new branch, new tag, new push)
    • AWS CodePipeline (invoke a Lambda function during the pipeline, Lambda must callback)
    • Amazon CloudWatch logs (log processing)
    • Amazon Simple Email Service
    • AWS CloudFormation
    • AWS Config
    • AWS IoT
    • AWS IoT Events

Lambda - Event Source Mapping

  • Kinesis Data Streams

  • SQS and SQS FIFO queue

  • DynamoDB streams

  • Common denominator

    • records need to be polled from the source

SNS & SQS

  • Add messages to the queue every 1 min for page 1-20
  • That is trigger 20 different lambdas every 1 min
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment