Book Image

Serverless Design Patterns and Best Practices

By : Brian Zambrano
Book Image

Serverless Design Patterns and Best Practices

By: Brian Zambrano

Overview of this book

Serverless applications handle many problems that developers face when running systems and servers. The serverless pay-per-invocation model can also result in drastic cost savings, contributing to its popularity. While it's simple to create a basic serverless application, it's critical to structure your software correctly to ensure it continues to succeed as it grows. Serverless Design Patterns and Best Practices presents patterns that can be adapted to run in a serverless environment. You will learn how to develop applications that are scalable, fault tolerant, and well-tested. The book begins with an introduction to the different design pattern categories available for serverless applications. You will learn thetrade-offs between GraphQL and REST and how they fare regarding overall application design in a serverless ecosystem. The book will also show you how to migrate an existing API to a serverless backend using AWS API Gateway. You will learn how to build event-driven applications using queuing and streaming systems, such as AWS Simple Queuing Service (SQS) and AWS Kinesis. Patterns for data-intensive serverless application are also explained, including the lambda architecture and MapReduce. This book will equip you with the knowledge and skills you need to develop scalable and resilient serverless applications confidently.
Table of Contents (18 chapters)
Title Page
Copyright and Credits
Dedication
Packt Upsell
Contributors
Preface
Index

Preface

Serverless architectures are changing the way software systems are being built and operated. When compared with systems that use physical servers or virtual machines, many tools, techniques, and patterns remain the same; however, there are several things that can or need to change drastically. To fully capitalize on the benefits of serverless systems, tools, patterns, and best practices should be thought through carefully before embarking on a serverless journey.

This book introduces and describes reusable patterns applicable to almost any type of serverless application, whether it be web systems, data processing, big data, or Internet of Things. You will learn, by example and explanation, about various patterns within a serverless context, such as RESTful APIs, GraphQL, proxy, fan-out, messaging, lambda architecture, and MapReduce, as well as when to use these patterns to make your applications scalable, performant, and fault tolerant. This book will take you through techniques for Continuous Integration and Continuous Deployment as well as designs for testing, securing, and scaling your serverless applications. Learning and applying these patterns will speed up your development lifecycle, while also improving the overall application architecture when building on top of your serverless platform of choice.

Who this book is for

This book is aimed at software engineers, architects, and anyone who is interested in building serverless applications using a cloud provider. Readers should be interested in learning popular patterns to improve agility, code quality, and performance, while also avoiding some of the pitfalls that new users may fall into when starting with serverless systems. Programming knowledge and basic serverless computing concepts are assumed.

What this book covers

Chapter 1, Introduction, covers the basics of serverless systems and discusses when serverless architectures may or may not be a good fit. Three categories of serverless patterns are introduced and briefly explained.

Chapter 2A Three-Tier Web Application Using REST, walks you through a full example of building a traditional web application using a REST API powered by AWS Lambda, along with serverless technologies for hosting HTML, CSS, and JavaScript for the frontend code.

Chapter 3A Three-Tier Web Application Pattern with GraphQL, introduces GraphQL and explains the changes needed to turn the previous REST API into a GraphQL API.

Chapter 4, Integrating Legacy APIs with the Proxy Pattern, demonstrates how it's possible to completely change an API contract while using a legacy API backend using nothing other than AWS API Gateway.

Chapter 5, Scaling Out with the Fan-Out Pattern, teaches you one of the most basic serverless patterns around, where a single event triggers multiple parallel serverless functions, resulting in quicker execution times over a serial implementation.

Chapter 6, Asynchronous Processing with the Messaging Pattern, explains different classes of messaging patterns and demonstrates how to put messages onto a queue using a serverless data producer, and process those messages downstream with a serverless data consumer.

Chapter 7, Data Processing Using the Lambda Pattern, explains how you can use multiple subpatterns to create two planes of computation, which provide views into historical aggregated data as well as real-time data.

Chapter 8, The MapReduce Pattern, explores an example implementation of aggregating large volumes of data in parallel, similar to the way systems such as Hadoop work.

Chapter 9Deployment and CI/CD Patterns, explain how to set up Continuous Integration and Continuous Delivery for serverless projects and what to keep in mind when doing so, in addition to showing examples of continuous deployment.

Chapter 10, Error Handling and Best Practices, reviews the tools and techniques for automatically tracking unexpected errors as well as several best practices and tips when creating serverless applications.

To get the most out of this book

  1. Almost all of the examples in this book use the Serverless Framework to manage AWS resources and Lambda functions. Installation instructions for the Serverless Framework can be found at https://serverless.com/framework/docs/getting-started/.

  2. In addition to the Serverless Framework, readers will need to have an AWS account to run the examples. For those new to AWS, you can create a new account, which comes with a year of usage in their Free Tier, at https://aws.amazon.com.

    During the course of this book, you will need the following tools:

    • AWS Lambda
    • AWS RDS
    • AWS API Gateway
    • AWS DynamoDB
    • AWS S3
    • AWS SQS
    • AWS Rekognition
    • AWS Kinesis
    • AWS SNS

    We will learn how to use these tools through the course of this book.

Download the example code files

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:

  1. Log in or register at www.packtpub.com.
  2. Select the SUPPORT tab.
  3. Click on Code Downloads & Errata.
  4. 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/Serverless-Design-Patterns-and-Best-Practices. 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 catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Conventions used

There are a number of text conventions used throughout this book.

CodeInText: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: "To test this, we need to set the timeout of the divide function to 4 seconds and put a time.sleep(3) in the middle of the application code."

A block of code is set as follows:

def divide(event, context):
    params = event.get('queryStringParameters') or {}
    numerator = int(params.get('numerator', 10))
    denominator = int(params.get('denominator', 2)) 
    body = { 
        "message": "Results of %s / %s = %s" % ( 
            numerator, 
            denominator,
            numerator // denominator,
        ) 
    } 

    response = { 
        "statusCode": 200,
        "body": json.dumps(body)
    } 

    return response

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

from raven_python_lambda import RavenLambdaWrapper

@RavenLambdaWrapper()
from raven_python_lambda import RavenLambdaWrapper

@RavenLambdaWrapper()
def divide(event, context):
    # Code

Any command-line input or output is written as follows:

$ curl "https://5gj9zthyv1.execute-api.us-west-2.amazonaws.com/dev?numerator=12&denominator=3"
{"message": "Results of 12 / 3 = 4"}

Bold: Indicates a new term, an important word, or words that you see onscreen. For example, words in menus or dialog boxes appear in the text like this. Here is an example: "The following screenshot showsInvocation errorsfrom the AWS Lambda monitoring page for the divide function:"

Note

Warnings or important notes appear like this.

Note

Tips and tricks appear like this.

Get in touch

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.

Reviews

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.