Building Serverless Web Applications

Book description

Build scalable, efficient, and highly available web apps using AWS

About This Book

  • Get an in-depth understanding of the serverless model
  • Build a complete serverless web application end to end
  • Learn how to use the Serverless Framework to improve your productivity

Who This Book Is For

If you're looking to learn more about scalable and cost-efficient architectures, this book is for you. Basic knowledge of Node.js skills or familiarity with cloud services is required. For other topics, we cover the basics.

What You Will Learn

  • Get a grasp of the pros and cons of going serverless and its use cases
  • Discover how you can use the building blocks of AWS to your advantage
  • Set up the environment and create a basic app with the Serverless Framework
  • Host static files on S3 and CloudFront with HTTPS support
  • Build a sample application with a frontend using React as an SPA
  • Develop the Node.js backend to handle requests and connect to a SimpleDB database
  • Secure your applications with authentication and authorization
  • Implement the publish-subscribe pattern to handle notifications in a serverless application
  • Create tests, define the workflow for deployment, and monitor your app

In Detail

This book will equip you with the knowledge needed to build your own serverless apps by showing you how to set up different services while making your application scalable, highly available, and efficient.

We begin by giving you an idea of what it means to go serverless, exploring the pros and cons of the serverless model and its use cases. Next, you will be introduced to the AWS services that will be used throughout the book, how to estimate costs, and how to set up and use the Serverless Framework.

From here, you will start to build an entire serverless project of an online store, beginning with a React SPA frontend hosted on AWS followed by a serverless backend with API Gateway and Lambda functions. You will also learn to access data from a SimpleDB database, secure the application with authentication and authorization, and implement serverless notifications for browsers using AWS IoT. This book will describe how to monitor the performance, efficiency, and errors of your apps and conclude by teaching you how to test and deploy your applications.

Style and approach

This book takes a step-by-step approach on how to use the Serverless Framework and AWS services to build Serverless Applications. It will give you a hands-on feeling, allowing you to practice while reading. It provides a brief introduction of concepts while keeping the focus on the practical skills required to develop applications.

Table of contents

  1. Preface
    1. What this book covers
    2. What you need for this book
    3. Who this book is for
    4. Conventions
    5. Reader feedback
    6. Customer support
      1. Downloading the example code
      2. Downloading the color images of this book
      3. Errata
      4. Piracy
      5. Questions
  2. Understanding the Serverless Model
    1. Introducing serverless
      1. Thinking serverless
      2. Serverless and PaaS
      3. IaaS and On-Premises
      4. The main goals of serverless
        1. Scalability
        2. Availability
        3. Cost efficiency
    2. The pros and cons of serverless
      1. Pros
        1. Reduced operational costs
        2. Focus on business
        3. System security
        4. Continuous delivery
        5. Microservices friendly
        6. Cost model
      2. Cons
        1. Higher latency
        2. Constraints
        3. Hidden inefficiencies
        4. Vendor dependency
        5. Debugging difficulties
        6. Atomic deploys
        7. Uncertainties
    3. Use cases
      1. Static websites
      2. Lean websites
      3. Small e-commerce websites
      4. Temporary websites
      5. Triggered processing
      6. Chatbots
      7. IoT backends
      8. Scheduled events
      9. Big Data
      10. What you should avoid
    4. Summary
  3. Getting Started with AWS
    1. Amazon Web Services
      1. Handling user accounts and security
        1. AWS IAM
          1. Creating users and groups
          2. Sign in with a non-root user account
        2. Amazon Cognito
      2. Managing AWS resources
        1. AWS Management Console
        2. AWS SDKs
        3. AWS CLI
        4. AWS CloudFormation
        5. Amazon CloudWatch
      3. Frontend services
        1. Amazon S3
          1. Using S3 with the CLI
        2. Amazon Route 53
        3. Amazon CloudFront
        4. AWS Certificate Manager
      4. Messaging and notifications
        1. Amazon SNS
        2. AWS IoT
      5. Backend services
        1. AWS Lambda
          1. Creating a Lambda function
        2. Amazon API Gateway
          1. Expose your Lambda function using API Gateway
      6. Database services
        1. Amazon SimpleDB
        2. Amazon DynamoDB
    2. The serverless architecture of our online store
      1. Estimating costs
        1. Assumptions
        2. Route 53 pricing
        3. S3 pricing
        4. CloudFront pricing
        5. Certificate Manager pricing
        6. IAM pricing
        7. Cognito pricing
        8. IoT pricing
        9. SNS pricing
        10. CloudWatch pricing
        11. API Gateway pricing
        12. Lambda pricing
        13. SimpleDB pricing
        14. DynamoDB
        15. Total pricing
    3. Summary
  4. Using the Serverless Framework
    1. Serverless Framework
      1. Understanding the Serverless Framework
        1. Other frameworks
      2. Installing the framework
      3. Configuring the framework
      4. Managing permissions in a team
      5. Creating a new project
        1. The handler.js file
        2. The serverless.yml file
      6. Configuring the Lambda limits
      7. Deploying a service
      8. Invoking a function
      9. Retrieving logs
      10. Adding endpoints
      11. Cross-Origin Resource Sharing
      12. Removing a service
    2. Going beyond the basics
      1. Using npm packages
      2. Accessing other AWS resources
      3. Events
        1. The S3 event
        2. Schedule events
      4. Serving HTML pages with Lambda
      5. Using configuration variables
      6. Plugins
      7. Showing deployment information
      8. Scaffolding
    3. Summary
  5. Hosting the Website
    1. Serving static files with Amazon S3
      1. Creating a bucket
      2. Enabling web hosting
      3. Handling errors
        1. Using redirection rules
        2. Using an error document
        3. Delegating to CloudFront
        4. Prerendering pages
      4. Supporting www anchors
      5. Uploading static files
      6. Automating website publishing
      7. Serving the gzip files
    2. Setting up Route 53
      1. Creating a hosted zone
      2. Creating record sets
    3. Setting up CloudFront
      1. Creating a distribution
      2. Handling CloudFront caching
        1. Invalidating server-side cache
        2. Invalidating client-side cache
      3. Updating Route 53 to use CloudFront
    4. Supporting HTTPS
      1. Creating a mail exchange record
      2. Requesting free certificates with AWS Certificate Manager
      3. Configuring CloudFront to use support HTTPS connections
    5. Summary
  6. Building the Frontend
    1. Getting started with React
      1. React principles
        1. The Flux pattern
      2. React hello-world
      3. Building a shopping cart
        1. Preparing the development environment
        2. Organizing the solution
        3. Composing components
        4. Implementing a component
        5. The App component
        6. The Product List component
        7. The Product component
        8. The Shopping Cart component
        9. The Cart Item component
        10. Publishing our demo
      4. Making Ajax requests
        1. Retrieving data from the server
        2. Sending data to the server
      5. Handling the page routes
        1. Linking pages
        2. Using the query parameters
    2. Single-Page Applications
      1. Pros and cons of SPA
        1. Pros
        2. Cons
        3. Considerations
      2. Prerendering pages
        1. Using PhantomJS
        2. Using the Lambda functions for prerendering
        3. Rendering on the fly
    3. Building an online store
      1. The Navbar component
      2. The Home page
      3. The product details page
      4. The Shopping Cart page
      5. The Login and Signup pages
      6. The error pages
    4. Summary
  7. Developing the Backend
    1. Defining the project architecture
      1. Monolith versus Microservices
      2. Nanoservices
      3. Microservices
      4. Monolith
      5. Graph
      6. Naming differences
    2. Developing the backend
      1. Defining the functionalities
      2. Organizing the code
        1. Referencing Lambda functions in other folders
        2. Using multiple services
      3. Setting the endpoints
        1. RESTful APIs
      4. Routing the HTTP verbs
        1. Handling HTTP responses
      5. Implementing the Lambda functions
        1. Retrieving all products
        2. Saving the Shopping Cart
        3. Checking out
    3. Summary
  8. Managing a Serverless Database
    1. Amazon SimpleDB
      1. Modeling the database
        1. Handling one-to-many relations
        2. Handling numbers
        3. Handling Booleans
        4. Handling dates
        5. Creating a domain
        6. Limits
      2. Inserting and querying data
        1. Performance and concurrency
      3. Managing the database
        1. Backing up and restoring data
        2. Controlling user access
    2. DynamoDB
      1. Modeling the database
        1. Creating a table
        2. Limits
      2. Inserting and querying data
        1. Inserting data
          1. The Document Client API
        2. Querying data
          1. The scan method
          2. The query method
        3. Performance and concurrency
      3. Managing the database
        1. Provisioned throughput
        2. Auto scaling
        3. Backing up and restoring data
        4. Controlling user access
      4. Improving the serverless store
        1. Retrieving all products
        2. Retrieving the user's shopping cart
        3. Saving the user's shopping cart
        4. Processing the checkout
    3. Amazon S3 (for media files)
      1. Uploading and downloading files
        1. Enabling CORS
      2. Backing up and restoring data
        1. Using S3 versioning
    4. Summary
  9. Securing the Serverless Application
    1. Security basics
      1. Information security
        1. Authentication
        2. Authorization
        3. Confidentiality
        4. Integrity
        5. Non-repudiation
        6. Availability
      2. Security on AWS
        1. Shared responsibility model
        2. The Trusted Advisor tool
        3. Pen testing
        4. AWS CloudTrail
        5. MFA
    2. Handling authentication and authorization
      1. Amazon Cognito
        1. Creating a user pool
        2. Creating an identity pool
      2. Using Cognito in our serverless store
        1. The Signup page
        2. The Login page
        3. Persisting the user token
        4. Logging out
      3. Handling authentication in Lambda functions
    3. Summary
  10. Handling Serverless Notifications
    1. AWS IoT
      1. Protocols
      2. Finding the IoT endpoint
      3. AWS IoT SDK
    2. Implementing serverless notifications
      1. Public notifications
        1. Adding a comment box
        2. Updating the list of comments
        3. Creating a new instance of the IoT SDK
        4. Sending and receiving new comments
        5. Triggering a Lambda function with IoT
      2. Private notifications
        1. Using credentials of an authenticated user
        2. Creating an IoT policy
        3. Attaching an IoT policy and connecting
        4. Updating the logout feature
        5. Sending IoT messages with Lambda
    3. Summary
  11. Testing, Deploying, and Monitoring
    1. Testing your solution
      1. Unit testing Lambda functions
      2. Mocking external services
        1. Using Sinon.JS for mocking
      3. Testing the frontend
      4. Simulating AWS services locally
    2. Deploying your application
      1. Development workflow
        1. Blue-green deployment
        2. Deploying new versions with a different database model
          1. Creating a new table or column
          2. Renaming a table or column
          3. Dropping a table or column
      2. Rollback a deployment
      3. Creating staging environments
        1. Being careful with the production environment
        2. Test data
      4. Keeping your functions warm
    3. Monitoring the operation
      1. Monitoring costs
      2. Monitoring errors
        1. Retrieving metrics with the Serverless Framework
        2. Streaming Lambda logs
        3. Handling errors
      3. Monitoring performance
      4. Monitoring efficiency
    4. Summary

Product information

  • Title: Building Serverless Web Applications
  • Author(s): Diego Zanon
  • Release date: July 2017
  • Publisher(s): Packt Publishing
  • ISBN: 9781787126473