O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Building Serverless Python Web Services with Zappa

Book Description

Master serverless architectures in Python and their implementation, with Zappa on three different frameworks.

Key Features

  • Scalable serverless Python web services using Django, Flask, and Pyramid.
  • Learn Asynchronous task execution on AWS Lambda and scheduling using Zappa.
  • Implementing Zappa in a Docker container.

Book Description

Serverless applications are becoming very popular these days, not just because they save developers the trouble of managing the servers, but also because they provide several other benefits such as cutting heavy costs and improving the overall performance of the application.

This book will help you build serverless applications in a quick and efficient way. We begin with an introduction to AWS and the API gateway, the environment for serverless development, and Zappa. We then look at building, testing, and deploying apps in AWS with three different frameworks--Flask, Django, and Pyramid. Setting up a custom domain along with SSL certificates and configuring them with Zappa is also covered. A few advanced Zappa settings are also covered along with securing Zappa with AWS VPC.

By the end of the book you will have mastered using three frameworks to build robust and cost-efficient serverless apps in Python.

What you will learn

  • Build, test, and deploy a simple web service using AWS CLI
  • Integrate Flask-based Python applications, via AWS CLI configuration
  • Design Rest APIs integrated with Zappa for Flask and Django
  • Create a project in the Pyramid framework and configure it with Zappa
  • Generate SSL Certificates using Amazon Certificate Manager
  • Configure custom domains with AWS Route 53
  • Create a Docker container similar to AWS Lambda

Who this book is for

Python Developers who are interested in learning how to develop fast and highly scalable serverless applications in Python, will find this book useful

Downloading the example code for this book You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

Table of Contents

  1. Title Page
  2. Copyright and Credits
    1. Building Serverless Python Web Services with Zappa
  3. Dedication
  4. Packt Upsell
    1. Why subscribe?
    2. PacktPub.com
  5. Contributors
    1. About the author
    2. About the reviewer
    3. Packt is searching for authors like you
  6. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Download the colour images
      3. Conventions used
    4. Get in touch
      1. Reviews
  7. Amazon Web Services for Serverless
    1. Technical requirements
    2. Transitioning from traditional server to serverless
    3. Getting started with AWS Lambda
    4. How AWS Lambda works
      1. Configuration
        1. Function code
        2. Environment variables
        3. Tags
        4. Execution role
        5. Basic settings
        6. Network
        7. Debugging and error handling
      2. Monitoring
    5. Executing the Lambda function
    6. Creating Lambda triggers
    7. Serverless RESTful API
    8. AWS Lambda interaction with theAWS CLI 
      1. Installing the AWS CLI
      2. Configuring the AWS CLI
    9. Configuring Lambda function with the AWS CLI
      1. Creating a Lambda function
      2. Invoking the function
      3. Create-event-source-mapping
    10. Summary
    11. Questions
  8. Getting Started with Zappa
    1. Technical requirements
      1. Hardware
      2. Software
    2. What is Zappa?
    3. Installing and configuring Zappa
    4. Building, testing, and deploying a Python web service using Zappa
      1. Building the deployment package
        1. What do you call this environment? (default dev)
        2. What do you want to call your bucket? (default zappa-2o2zd8dg4)
        3. What's your modular path to your app function? (default dev)
        4. Would you like to deploy the application globally? (default n)
        5. The zappa_settings.json file
        6. Deploying and testing hello world
    5. Basic uses
      1. Initial deployments
      2. Update
      3. Status
      4. Tailing logs
      5. Rollback
      6. Undeploy
      7. Package
    6. Summary
    7. Questions
  9. Building a Flask Application with Zappa
    1. Technical requirements
    2. What is Flask?
      1. Installing Flask
    3. A minimal Flask application
    4. Configuring with Zappa
    5. Building, testing, and deploying on AWS Lambda
    6. A complete Flask Todo application
      1. Prerequisite
        1. Virtualenv
        2. Flask extensions
        3. Scaffolding
        4. Configuration
        5. Base model
      2. Authentication
        1. Blueprint
        2. Models
        3. Forms
        4. Views
        5. Templates
      3. Todo
        1. Blueprint
        2. Model
        3. Forms
        4. Views
          1. CREATE
          2. RETRIEVE
          3. UPDATE
          4. DELETE
        5. Templates
        6. FLASK_APP
      4. Deployment
    7. Summary
    8. Questions
  10. Building a Flask-Based REST API with Zappa
    1. Technical requirements
    2. Installing and configuring Flask
      1. Flask extensions
        1. Flask-JWT
          1. Learning about JWT
        2. Flask-RESTful
          1. Begining with REST
    3. Designing the REST API 
      1. Configuring the application settings
      2. Implementing authentication
      3. Implementing the todo API
    4. Building, testing, and deploying the REST API using Zappa
      1. Configuring Zappa
      2. Initiating deployment using Zappa
      3. Demonstrating the deployed application
        1. Sign up API
        2. Login API
        3. Todo API
          1. Todo API without authorization
          2. Todo API with the authorization header
    5. Summary
    6. Questions
  11. Building a Django Application with Zappa
    1. Technical requirements
    2. Installing and configuring Django
      1. Setting up a virtual environment
        1. Installing pipenv
        2. Configuring and installing packages
    3. Designing an image gallery application
      1. Design overview
      2. Initializing the project
      3. Implementing models
      4. Integrating with the admin panel
      5. Application demonstration
    4. Configuring the Amazon CloudFront CDN
      1. Creating CloudFront distributions
    5. Setting up static and media files
      1. Standard configuration
      2. django-storage
    6. Building, testing, and deploying the Django application using Zappa
      1. Configuring Zappa
      2. Building and deploying
      3. Django management command using Zappa
    7. Summary
    8. Questions
  12. Building a Django REST API with Zappa
    1. Technical requirements
    2. Installing and configuring the Django REST Framework
      1. Installing the required packages
    3. Designing REST APIs
      1. What is the Django Rest Framework?
      2. Integrating the REST Framework
      3. Implementing authentication and authorization
        1. Configuring django-rest-framework-jwt
      4. Implementing serializers
      5. Implementing viewsets
      6. Configuring the URL routes
    4. Building, testing, and deploying Django app using Zappa
      1. Executing in the local environment
        1. API authentication
        2. GET request on API "/albums/"
        3. POST request on API "/albums/<album_id>/photos/"
      2. Configuring Zappa
      3. Building and deploying
      4. Executing in the production environment
        1. Authentication API
        2. GET request on API /albums/
    5. Summary
    6. Questions
  13. Building a Falcon Application with Zappa
    1. Technical requirements
    2. Installing and configuring Falcon
      1. Setting up the virtual environment
        1. Installing required packages
      2. What is Falcon?
      3. What is Peewee?
    3. Designing Falcon APIs
      1. Scaffolding the application
      2. Designing the model class
      3. Mashape API integration
      4. Creating API resources
    4. Building, testing, and deploying Falcon APIs using Zappa
      1. Local execution using gunicorn
        1. API for the daily quote
        2. API of the random quote
      2. Configuring Zappa
        1. Zappa initialization
        2. Zappa deployment
      3. Executing in the production environment
        1. Daily quote API execution
        2. Random quote API execution
        3. Enabling caching on the API Gateway
      4. Event scheduling
        1. Configuring events using Zappa
        2. Scheduling an event to set the quote of the day
    5. Summary
    6. Questions
  14. Custom Domain with SSL
    1. Technical requirements
    2. Configuring custom domain with AWS Route 53
      1. What is AWS Route 53?
      2. Changing nameservers to Route 53
    3. Generating SSL certificates using AWS Certificate Manager
      1. What is SSL?
      2. What are SSL certificates?
      3. Generating SSL certificate using Amazon Certificate Manager (ACM)
    4. Integrating custom domain using Zappa
      1. Deploying to a domain with ACM certificate
      2. Application execution with the configured domain
        1. Daily quote API
        2. Random quote API
    5. Summary
    6. Questions
  15. Asynchronous Task Execution on AWS Lambda
    1. Technical requirements
    2. Asynchronous execution
      1. Understanding asynchronous execution
      2. Asynchronous AWS Lambda execution using Boto3
    3. AWS Lambda asynchronous invocation using Zappa
      1. Asynchronous AWS Lambda invocation using the task decorator
      2. Amazon SNS as the task source
      3. Direct invocation
      4. Remote invocation
    4. Configuring the Quote API application with async features
      1. SMS subscription with Amazon SNS for the daily quote
        1. Configuring Amazon SNS using Boto3
        2. Implementing the SMS subscription functionality
    5. Deploying and executing the Quote API using Zappa
      1. Setting up the virtual environment
      2. Setting up environment variables
      3. Adding a custom domain with SSL
      4. Scheduling an event to publish an SMS
      5. Deployment
      6. Quote API execution
        1. The daily quote API
        2. Daily quote SMS subscription
        3. Daily quote SMS unsubscription
    6. Summary
    7. Questions
  16. Advanced Zappa Settings
    1. Technical requirements
    2. Keeping the server warm
    3. Enabling CORS
    4. Handling larger projects
      1. Handling larger projects using the AWS Lambda console
      2. Handling larger projects using Zappa
    5. Enabling bash tab compilation
    6. Catching unhandled exceptions
    7. Summary
    8. Questions
  17. Securing Serverless Applications with Zappa
    1. Technical requirements
    2. Implementing a random quote API
      1. Prerequisites
      2. Developing the random quote API
      3. Deploying with Zappa
      4. Executing the API
    3. Enabling secure endpoints on API Gateway
      1. Enabling the API Key
        1. Executing the API with the API key header
      2. IAM policy
      3. API Gateway Lambda authorizer
    4. Tracing AWS Lambda failure with dead letter queues
      1. Manual DLQ configuration
      2. Automating DLQ configuration with Zappa
      3. Raising exceptions in the asynchronous Lambda function
    5. Analyzing the Zappa application with AWS X-Ray
      1. X-Ray manual integration with AWS Lambda
      2. Zappa configuration to enable AWS X-Ray support
    6. Securing your Zappa application using AWS VPC
      1. Manual configuration of VPC
      2. VPC configuration using Zappa
    7. Summary
    8. Questions
  18. Zappa with Docker
    1. Technical requirements
    2. Understanding Docker
    3. Problem statement
      1. Proposed solution
      2. Case study for this solution
    4. API development with a custom tool dependency
      1. Prerequisites
      2. Implementing the /doc-parser API
      3. Executing in the local environment
    5. Building, testing, and deploying with Docker
      1. Building without Docker
      2. Configuring Docker with Zappa
      3. Executing the API on AWS Lambda
    6. Summary
    7. Questions
  19. Assessments
    1. Chapter 1, Amazon Web Services for Serverless
    2. Chapter 2, Getting Started with Zappa
    3. Chapter 3, Building a Flask Application with Zappa
    4. Chapter 4, Building a Flask-Based REST API with Zappa
    5. Chapter 5, Building a Django Application with Zappa
    6. Chapter 6, Building a Django REST API with Zappa
    7. Chapter 7, Building a Falcon Application with Zappa
    8. Chapter 8, Custom Domain with SSL
    9. Chapter 9, Asynchronous Task Execution on AWS Lambda
    10. Chapter 10, Advanced Zappa Settings
    11. Chapter 11, Securing Serverless Applications with Zappa
    12. Chapter 12, Zappa with Docker
  20. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think