31,19 €
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.
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Seitenzahl: 260
Veröffentlichungsjahr: 2018
Copyright © 2018 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
Commissioning Editor: Aaron LazarAcquisition Editor: Karan SadawanaContent Development Editor: Akshada IyerTechnical Editor: Adhithya HaridasCopy Editor: Safis EditingProject Coordinator: Prajakta NaikProofreader: Safis EditingIndexer: Rekha NairGraphics: Jisha ChirayilProduction Coordinator: Deepika Naik
First published: July 2018
Production reference: 1280718
Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK.
ISBN 978-1-78883-761-3
www.packtpub.com
Mapt is an online digital library that gives you full access to over 5,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website.
Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals
Improve your learning with Skill Plans built especially for you
Get a free eBook or video every month
Mapt is fully searchable
Copy and paste, print, and bookmark content
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details.
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks.
Abdulwahid Abdulhaque Barguzar is a software architect and an active contributor in the open source community. He has developed a deep understanding of architecting software product development through Python web frameworks and JavaScript frameworks. He is passionate about implementing and mastering new technologies. In his free time, he researches innovative approaches to rapidly developing and designing a software product and automation testing, aiming to become a master software architect.
Bhagvan Kommadi has around 18 years of experience in the software industry and has done master's in industrial systems engineering at Georgia Institute of Technology and bachelor's in aerospace engineering from IIT, Madras. He is currently working as Chief Technology Officer of Crystal Delta Solutions, based in Chennai. He has reviewed Beyond Software Architecture: Creating and Sustaining Winning Solutions by Luke Hohmann and Algorithms of the Intelligent Web by Dr. Haralambos Marmanis.
If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea.
Title Page
Copyright and Credits
Building Serverless Python Web Services with Zappa
Dedication
Packt Upsell
Why subscribe?
PacktPub.com
Contributors
About the author
About the reviewer
Packt is searching for authors like you
Preface
Who this book is for
What this book covers
To get the most out of this book
Download the example code files
Download the colour images
Conventions used
Get in touch
Reviews
Amazon Web Services for Serverless
Technical requirements
Transitioning from traditional server to serverless
Getting started with AWS Lambda
How AWS Lambda works
Configuration
Function code
Environment variables
Tags
Execution role
Basic settings
Network
Debugging and error handling
Monitoring
Executing the Lambda function
Creating Lambda triggers
Serverless RESTful API
AWS Lambda interaction with theAWS CLI 
Installing the AWS CLI
Configuring the AWS CLI
Configuring Lambda function with the AWS CLI
Creating a Lambda function
Invoking the function
Create-event-source-mapping
Summary
Questions
Getting Started with Zappa
Technical requirements
Hardware
Software
What is Zappa?
Installing and configuring Zappa
Building, testing, and deploying a Python web service using Zappa
Building the deployment package
What do you call this environment? (default dev)
What do you want to call your bucket? (default zappa-2o2zd8dg4)
What's your modular path to your app function? (default dev)
Would you like to deploy the application globally? (default n)
The zappa_settings.json file
Deploying and testing hello world
Basic uses
Initial deployments
Update
Status
Tailing logs
Rollback
Undeploy
Package
Summary
Questions
Building a Flask Application with Zappa
Technical requirements
What is Flask?
Installing Flask
A minimal Flask application
Configuring with Zappa
Building, testing, and deploying on AWS Lambda
A complete Flask Todo application
Prerequisite
Virtualenv
Flask extensions
Scaffolding
Configuration
Base model
Authentication
Blueprint
Models
Forms
Views
Templates
Todo
Blueprint
Model
Forms
Views
CREATE
RETRIEVE
UPDATE
DELETE
Templates
FLASK_APP
Deployment
Summary
Questions
Building a Flask-Based REST API with Zappa
Technical requirements
Installing and configuring Flask
Flask extensions
Flask-JWT
Learning about JWT
Flask-RESTful
Begining with REST
Designing the REST API 
Configuring the application settings
Implementing authentication
Implementing the todo API
Building, testing, and deploying the REST API using Zappa
Configuring Zappa
Initiating deployment using Zappa
Demonstrating the deployed application
Sign up API
Login API
Todo API
Todo API without authorization
Todo API with the authorization header
Summary
Questions
Building a Django Application with Zappa
Technical requirements
Installing and configuring Django
Setting up a virtual environment
Installing pipenv
Configuring and installing packages
Designing an image gallery application
Design overview
Initializing the project
Implementing models
Integrating with the admin panel
Application demonstration
Configuring the Amazon CloudFront CDN
Creating CloudFront distributions
Setting up static and media files
Standard configuration
django-storage
Building, testing, and deploying the Django application using Zappa
Configuring Zappa
Building and deploying
Django management command using Zappa
Summary
Questions
Building a Django REST API with Zappa
Technical requirements
Installing and configuring the Django REST Framework
Installing the required packages
Designing REST APIs
What is the Django Rest Framework?
Integrating the REST Framework
Implementing authentication and authorization
Configuring django-rest-framework-jwt
Implementing serializers
Implementing viewsets
Configuring the URL routes
Building, testing, and deploying Django app using Zappa
Executing in the local environment
API authentication
GET request on API "/albums/"
POST request on API "/albums/<album_id>/photos/"
Configuring Zappa
Building and deploying
Executing in the production environment
Authentication API
GET request on API /albums/
Summary
Questions
Building a Falcon Application with Zappa
Technical requirements
Installing and configuring Falcon
Setting up the virtual environment
Installing required packages
What is Falcon?
What is Peewee?
Designing Falcon APIs
Scaffolding the application
Designing the model class
Mashape API integration
Creating API resources
Building, testing, and deploying Falcon APIs using Zappa
Local execution using gunicorn
API for the daily quote
API of the random quote
Configuring Zappa
Zappa initialization
Zappa deployment
Executing in the production environment
Daily quote API execution
Random quote API execution
Enabling caching on the API Gateway
Event scheduling
Configuring events using Zappa
Scheduling an event to set the quote of the day
Summary
Questions
Custom Domain with SSL
Technical requirements
Configuring custom domain with AWS Route 53
What is AWS Route 53?
Changing nameservers to Route 53
Generating SSL certificates using AWS Certificate Manager
What is SSL?
What are SSL certificates?
Generating SSL certificate using Amazon Certificate Manager (ACM)
Integrating custom domain using Zappa
Deploying to a domain with ACM certificate
Application execution with the configured domain
Daily quote API
Random quote API
Summary
Questions
Asynchronous Task Execution on AWS Lambda
Technical requirements
Asynchronous execution
Understanding asynchronous execution
Asynchronous AWS Lambda execution using Boto3
AWS Lambda asynchronous invocation using Zappa
Asynchronous AWS Lambda invocation using the task decorator
Amazon SNS as the task source
Direct invocation
Remote invocation
Configuring the Quote API application with async features
SMS subscription with Amazon SNS for the daily quote
Configuring Amazon SNS using Boto3
Implementing the SMS subscription functionality
Deploying and executing the Quote API using Zappa
Setting up the virtual environment
Setting up environment variables
Adding a custom domain with SSL
Scheduling an event to publish an SMS
Deployment
Quote API execution
The daily quote API
Daily quote SMS subscription
Daily quote SMS unsubscription
Summary
Questions
Advanced Zappa Settings
Technical requirements
Keeping the server warm
Enabling CORS
Handling larger projects
Handling larger projects using the AWS Lambda console
Handling larger projects using Zappa
Enabling bash tab compilation
Catching unhandled exceptions
Summary
Questions
Securing Serverless Applications with Zappa
Technical requirements
Implementing a random quote API
Prerequisites
Developing the random quote API
Deploying with Zappa
Executing the API
Enabling secure endpoints on API Gateway
Enabling the API Key
Executing the API with the API key header
IAM policy
API Gateway Lambda authorizer
Tracing AWS Lambda failure with dead letter queues
Manual DLQ configuration
Automating DLQ configuration with Zappa
Raising exceptions in the asynchronous Lambda function
Analyzing the Zappa application with AWS X-Ray
X-Ray manual integration with AWS Lambda
Zappa configuration to enable AWS X-Ray support
Securing your Zappa application using AWS VPC
Manual configuration of VPC
VPC configuration using Zappa
Summary
Questions
Zappa with Docker
Technical requirements
Understanding Docker
Problem statement
Proposed solution
Case study for this solution
API development with a custom tool dependency
Prerequisites
Implementing the /doc-parser API
Executing in the local environment
Building, testing, and deploying with Docker
Building without Docker
Configuring Docker with Zappa
Executing the API on AWS Lambda
Summary
Questions
Assessments
Chapter 1, Amazon Web Services for Serverless
Chapter 2, Getting Started with Zappa
Chapter 3, Building a Flask Application with Zappa
Chapter 4, Building a Flask-Based REST API with Zappa
Chapter 5, Building a Django Application with Zappa
Chapter 6, Building a Django REST API with Zappa
Chapter 7, Building a Falcon Application with Zappa
Chapter 8, Custom Domain with SSL
Chapter 9, Asynchronous Task Execution on AWS Lambda
Chapter 10, Advanced Zappa Settings
Chapter 11, Securing Serverless Applications with Zappa
Chapter 12, Zappa with Docker
Other Books You May Enjoy
Leave a review - let other readers know what you think
This book is based on the modern way of developing a serverless Python-based web or microservices. Being serverless involves a serverless infrastructure provided by the cloud service provider. This book shows how to use Amazon Web Services to implement the serverless infrastructure. Also, it covers the deployment process using Zappa. Zappa eliminates manual intervention, gives you an automated way to proceed deployment, and helps you maintain multiple deployment stages.
This book is for beginner to experienced Python developers who want to understand the way to develop Python web services or microservices on the serverless infrastructure. Experienced Python developers can enhance their skills by learning about the serverless technology and understanding serverless deployment.
Chapter 1, Amazon Web Services for Serverless, covers understanding the basics of AWS Lambda and API Gateway services. Also, the manual process of creating a serverless service by interacting with the AWS console and CLI tool is covered.
Chapter 2, Getting Started with Zappa, explains the concept of the Zappa tool and elaborates the benefits of using Zappa over the manual process of AWS services.
Chapter 3, Building a Flask Application with Zappa, explores basic Flask application development and deployment using Zappa as a serverless application.
Chapter 4, Building a Flask-Based REST API with Zappa, presents the Flask-based RESTful API development and deployment process with Zappa.
Chapter 5, Building a Django Application with Zappa, discusses Django core application development and deploying the application as a serverless over AWS Lambda using Zappa.
Chapter 6, Building a Django REST API with Zappa, focuses on RESTful API implementation using the Django REST framework and the deployment process using Zappa.
Chapter 7, Building a Falcon Application with Zappa, takes you through developing a RESTful API as a microservice using the Falcon framework and the deployment process using Zappa.
Chapter 8, Custom Domain with SSL, explains how to configure a custom domain with Zappa and also covers SSL generation using AWS.
Chapter 9, Asynchronous Task Execution on AWS Lambda, showcases the implementation of asynchronous operations for the time-consuming task of using Zappa.
Chapter 10, Advanced Zappa Settings, familiarizes you with the additional settings of the Zappa tool to enhance the application deployment process.
Chapter 11, Securing Serverless Applications with Zappa, outlines the security aspect of a serverless application over AWS Lambda using Zappa.
Chapter 12, Zappa with Docker, presents application development with AWS Lambda context environment using Docker containerization.
Before starting, there are some prerequisites that readers require. Readers should have the following:
Good understanding of the virtual environment
Understanding of Python package installation
Knowledge of traditional deployment using Apache or NGINX
A basic knowledge of web services or microservices
You can download the example code files for this book from your account at www.packtpub.com. If you purchased this book elsewhere, you can visit www.packtpub.com/support and register to have the files emailed directly to you.
You can download the code files by following these steps:
Log in or register at
www.packtpub.com
.
Select the
SUPPORT
tab.
Click on
Code Downloads & Errata
.
Enter the name of the book in the
Search
box and follow the onscreen instructions.
Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:
WinRAR/7-Zip for Windows
Zipeg/iZip/UnRarX for Mac
7-Zip/PeaZip for Linux
The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Building-Serverless-Python-Web-Services-with-Zappa. In case there's an update to the code, it will be updated on the existing GitHub repository.
We also have other code bundles from our rich catalogue of books and videos available at https://github.com/PacktPublishing/. Check them out!
We also provide a PDF file that has colour images of the screenshots/diagrams used in this book. You can download it here: https://www.packtpub.com/sites/default/files/downloads/BuildingServerlessPythonWebServiceswithZappa_ColorImages.pdf.
Feedback from our readers is always welcome.
General feedback: Email [email protected] and mention the book title in the subject of your message. If you have questions about any aspect of this book, please email us at [email protected].
Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.
Piracy: If you come across any illegal copies of our works in any form on the Internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.
If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.
Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!
For more information about Packt, please visit packtpub.com.
In this chapter, we are going to learn about Amazon Web Services for managing a serverless infrastructure. We will be exploring the AWS workflow to create a serverless application. We will learn about the manual process for creating a basic serverless application and an automated process using the AWS CLI (command line interface).
Topics we will cover in this chapter include:
Transitioning from the traditional server to serverless
Getting started with AWS Lambda
How AWS Lambda works
Executing a Lambda function
Creating Lambda triggers
Creating a serverless RESTful API
AWS Lambda interaction by the AWS CLI
There are some technical prerequisites before moving ahead. We are going to demonstrate AWS through a web console and the AWS CLI. The following prerequisites should be considered:
All the demonstration has been tested on a Linux machine with Ubuntu 16.04. We have shared the links to each library used in this book. You can get the detailed information about the installation and configuration with a specific platform.
We are using open source libraries and software. Hence, for each library, we are going to share its official documentation links. You can refer to those links for detailed information about a specific library.
Web hosting has changed drastically since it started. Physical server machines were shared among many web applications, and it was a really big challenge when it came to scale. It proved to be very expensive for any individual or company to afford an entire server machine to host their web application.
But, thanks to virtualization, the need for a physical server for any web application has been eliminated. Virtualization provides the ability to create many virtual servers as opposed to a single physical server.
Now, the new era of serverless is making the developer's life easier, as we can focus our hard work on development instead of investing time and money on deployment.
Amazon introduced Amazon Elastic Compute Cloud (Amazon EC2) as a cloud computing solution. Amazon EC2 makes it possible to create an array of virtual servers or instances the Amazon Cloud without investing in hardware. You can scale it as per your requirements in terms of networking, computing, and storage.
The serverless approach is nothing but the process of eliminating the manual workload of setting up the hosting environment. Cloud service providers provide serverless services, and so you never actually own any server. Instead, the cloud service provider executes your code in a high-availability infrastructure.
Many cloud service providers introduced different services for the serverless infrastructure. Amazon introduced AWS Lambda as a compute service, where you just provide your code and AWS Lambda executes the code in a highly scalable infrastructure by itself. You don't need to worry about managing the services manually. You need to pay for the compute time of your code execution, and there are no charges when your code is not running.
AWS Lambda executes the code as needed in response to events such as data storage events on an S3 bucket, Amazon DynamoDB events, and HTTP request events via the API Gateway. AWS Lambda is able to execute the code based on scheduled time events via AWS CloudWatch Events. AWS Lambda supports Python, Node.js, C#, and Java.
You need to write a function, which will be executed by AWS Lambda on your behalf.
AWS Lambda is implemented on a container-based model that supports a runtime environment and executes the code as per the Lambda function configuration. When the Lambda function is invoked, it launches the container (an execution environment) based on the AWS Lambda configuration and enables the basic runtime environment, which is required to execute the code.
Let's start with some practical work:
To create a Lambda function, you must have an AWS account. If you don't have an AWS account, then you need to sign up on AWS (
https://aws.amazon.com/
) by providing some basic contact and payment information, as it's essential information required by Amazon.
Go to the Lambda home page (
https://console.aws.amazon.com/lambda/home
). Click on the
Create a function
button. This will redirect you to the
Create function
page, which is described in the next step. Take a look at the following screenshot:
AWS provides three different options to create a Lambda function, such as
Author from scratch
,
Blueprints
, and
Serverless Application Repository
. We will be using the
Blueprint
option, which has some built-in Lambda functions. We can choose these blueprints based on our requirements from the search bar, where you can filter by tag and attributes or search by keywords:
Let's choose a
hello-world-python
blueprint. Once we choose the blueprint, we need to set up the basic information about the Lambda function. This information includes the Lambda function's
Name
and
Role,
as shown in the following screenshot:
Here,
Name
will be a unique identification for your Lambda function and
Role
defines the permissions of your Lambda function.
There are three options available for creating a role:
Choose an existing role
Create new role from template(s)
Create a custom role
Let's look at them in more detail:
Choose an existing role
: This allows you to select the previously created role.
Create new role from template(s)
: Here, you need to define a role name. AWS Lambda provides ready-made built-in role policy templates that have pre-configured permissions. These are based on other AWS services-related permissions required by the AWS Lambda function. On any role selection, Lambda will automatically add the logging permission to CloudWatch (AWS logging service), as this is the basic permission required by Lambda.
Create a custom role
: AWS provides an additional privilege to create a customized role to access AWS Lambda. Here, you can define the role based on your requirement.
Let's create the
HelloWorld
Lambda function with some role. Here, I chose the S3 object read-only permission policy template.
The following screenshot describes the newly created
HelloWorld
Lambda function:
The Lambda function includes three sections:
Configuration
Triggers
Monitoring
Let's look at detailed information about the configuration and monitoring. We will have a separate section for triggers.
Lambda execution depends on the configuration setting. Configuring the Lambda function requires the following details:
Function code
Environment variables
Tags
Execution role
Basic settings
Network
Debugging and error handling
You can set the environment variables in key-value pairs, which can be utilized in your code.
You can use tags for grouping and filtering your Lambda functions. You may have multiple Lambda functions with different regions, so tags help make Lambda functions more manageable.
As we previously discussed the role and permission while creating the Lambda function, Lambda provides the capability to edit the existing role that you chose at the time of the Lambda function creation.
Under basic settings, you can configure the memory and execution timeout. Lambda supports memory from 128 MB to 1,536 MB. Timeout execution is in seconds; the default timeout execution Lambda supports is 300 seconds. This setting helps you to control the code execution performance and cost for your Lambda function.
In the network section, you can configure the network access to your Lambda function.
AWS provides a VPC (Virtual Private Cloud) service to create a virtual network, which allows access to AWS services. You can also configure the networking as per your requirements.
We will discuss the Lambda function with VPC in the upcoming chapters. As of now, we will choose No VPC in the network section.
AWS Lambda automatically retries the failed asynchronous invocation. But you can also configure the DLQ (Dead Letter Queue), such as the SQS queue or SNS topic. To configure the DLQ, the Lambda function must have permission to access DLQ resources.
Now that we understand the configuration, let's go ahead with the execution of the Lambda function.
Let's look at the Monitoring section, which describes the activity related to our Lambda function. It can be used to analyze the performance of our Lambda function execution.
AWS CloudWatch is a monitoring service for AWS resources and manages all activity logs. It creates metric data to generate statistical data. CloudWatch enables real-time monitoring of AWS resources. It also monitors hardware information related to AWS EC2 or RDS database instances and other resources.
Lambda monitoring sections display the last 24 hours' analytics data related to the Lambda function's activity and performance. The following screenshot shows the monitored analytics information about our hello world Lambda function:
Let's move on to the next section, where we are going to look at the Lambda function execution.
AWS Lambda supports several methods of execution. Let's start with the basic execution from its own web console interface. AWS Lambda provides the capability to test the function manually, where you can define the test event context. If you want to test against some other Amazon services, then there are built-in event templates available.
The following screenshot demonstrates the test event creation:
