Introduction to Serverless Computing for Pythonistas

Piotr Grzesik

What I do ?

What I will be talking about ?

  • What is serverless computing?
  • What are the pros and cons of serverless computing?
  • What are the available platforms and frameworks?
  • How to create my first serverless Python app?
  • What is and how to use Serverless framework?

Serverless model

  • Small, stateless functions that do one thing (FaaS)
  • Cloud provider is responsible for managing infrastructure
  • Execution triggered by events
    • HTTP Request
    • Message from Queue
    • Timer
    • File upload
  • Per invocation billing
  • Integration with other services like DBs, Auth0 (BaaS)

 

Pros of serverless

  • No servers to provision and maintain manually
  • "Infinite" scalability
  • High availability
  • Quicker time to market, increased velocity
  • Smaller total cost of ownership
  • More focused on delivering business logic

Cons/challenges of serverless

  • Vendor lock-in
  • Ephemeral nature of runtime
  • Cold starts
  • Increased complexity with growing number of separate functions
  • Execution duration, limited CPU and RAM
  • More challenging testing, debugging and monitoring

Example uses for serverless

  • Multimedia processing (e.g. in response to image upload)
  • Reacting to database changes (e.g. auditing changes)
  • IoT sensors message processing (e.g. processing incoming temperature readings)
  • Stream processing (e.g. processing a stream of messages)
  • Scheduled or batch jobs (e.g. performing backups)
  • REST or GraphQL APIs (e.g. web applications)

 

Serverless is not a silver bullet

Providers/platforms

  • AWS Lambda
  • Azure Functions
  • Google Cloud Functions
  • Knative
  • OpenFaaS
  • IBM OpenWhisk
  • CloudFlare Workers
  • Auth0 Webtask
  • and many more...

 

AWS Lambda

  • Serverless computing platform, introduced in 2014 by Amazon Web Services
  • Out of the box support for Java, .NET Core, Go, Ruby, Python, Node.js. Virtually supports every runtime via custom runtimes introduced in 2018
  • Easy integration with S3, Kinesis, CloudWatch, API Gateway
  • Execution time limit - 15 minutes
  • Up to 3,008 MB of memory per invocation
  • Support for layers
  • Lambda@Edge - running code on Edge Locations

Creating our first serverless function!

Available frameworks

  • Serverless Framework
  • AWS SAM
  • Zappa
  • Chalice

Serverless framework

  • Written in Node.js
  • Provider agnostic, supports AWS, Azure, Google Cloud and many more
  • Supports multiple programming languages
  • Rich plugin ecosystem
  • Supports application lifecycle management
  • Configuration via single yaml file

Serverless framework - useful commands

Installation

Project initialization

Deployment

Function invocation

npm install -g serverless
serverless create --template aws-python3 --path hello-lambda
serverless deploy function -f hello
serverless invoke -f hello

Cleanup

serverless remove

Example serverless.yml

service: example

provider:
  name: aws
  runtime: python3.6
  region: us-east-1
  iamRoleStatements:
    - Effect: "Allow"
      Action:
        - "logs:CreateLogGroup"
        - "logs:CreateLogStream"
        - "logs:PutLogEvents"
      Resource:
        - "arn:aws:logs:*:*:*"
  environment:
    STAGE: ${self:custom.stage}

functions:
  example:
    handler: handler.example
    events:
      - schedule: rate(1 day)

plugins:
  - serverless-python-requirements

custom:
  pythonRequirements:
    dockerizePip: 'non-linux'
  stage: ${opt:stage, self:provider.stage}

Building app with Serverless Framework

  • Integrates with GitHub
  • When new PR is created, it checks if PR description contains a Trello card attached
  • Sends status to GitHub
  • Sends status information to subscribers (SNS integration)
  • Saves statuses to DynamoDB
  • Users can retrieve previous checks

Application diagram

Key points

  • No servers to manage
  • Per invocation billing
  • Scalability and availability
  • Quicker time to market
  • Serverless Framework is great

Recommended readings

  • https://github.com/cncf/wg-serverless/tree/master/whitepapers/serverless-overview
  • https://www2.eecs.berkeley.edu/Pubs/TechRpts/2019/EECS-2019-3.pdf
  • https://theburningmonk.com/

Thanks!

@p_grzesik

pj.grzesik@gmail.com

piotrgrzesik.pl

Introduction to Serverless Computing for Pythonistas

By progressive

Introduction to Serverless Computing for Pythonistas

  • 499