Serverless Programming Cookbook

Book description

Build, secure, and deploy real-world serverless applications in AWS and peek into the serverless cloud offerings from Azure, Google Cloud, and IBM Cloud

Key Features

  • Build serverless applications with AWS Lambda, AWS CloudFormation and AWS CloudWatch
  • Perform data analytics and natural language processing(NLP)on the AWS serverless platform
  • Explore various design patterns and best practices involved in serverless computing

Book Description

Managing physical servers will be a thing of the past once you're able to harness the power of serverless computing. If you're already prepped with the basics of serverless computing, Serverless Programming Cookbook will help you take the next step ahead. This recipe-based guide provides solutions to problems you might face while building serverless applications.

You'll begin by setting up Amazon Web Services (AWS), the primary cloud provider used for most recipes. The next set of recipes will cover various components to build a Serverless application including REST APIs, database, user management, authentication, web hosting, domain registration, DNS management, CDN, messaging, notifications and monitoring. The book also introduces you to the latest technology trends such as Data Streams, Machine Learning and NLP. You will also see patterns and practices for using various services in a real world application. Finally, to broaden your understanding of Serverless computing, you'll also cover getting started guides for other cloud providers such as Azure, Google Cloud Platform and IBM cloud.

By the end of this book, you'll have acquired the skills you need to build serverless applications efficiently using various cloud offerings.

What you will learn

  • Serverless computing in AWS and explore services with other clouds
  • Develop full-stack apps with API Gateway, Cognito, Lambda and DynamoDB
  • Web hosting with S3, CloudFront, Route 53 and AWS Certificate Manager
  • SQS and SNS for effective communication between microservices
  • Monitoring and troubleshooting with CloudWatch logs and metrics
  • Explore Kinesis Streams, Amazon ML models and Alexa Skills Kit

Who this book is for

For developers looking for practical solutions to common problems while building a serverless application, this book provides helpful recipes. To get started with this intermediate-level book, knowledge of basic programming is a must.

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. Serverless Programming Cookbook
  3. Dedication
  4. About Packt
    1. Why subscribe?
    2. Packt.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. Conventions used
    4. Get in touch
      1. Reviews
  7. Getting Started with Serverless Computing on AWS
    1. Introduction
    2. Getting started with the AWS platform
      1. Getting ready
      2. How to do it...
      3. How it works...
        1. AWS Identity and Access Management (IAM)
        2. Amazon CloudWatch
        3. Multi-Factor Authentication (MFA)
      4. There's more...
      5. See also
    3. Your first AWS Lambda
      1. Getting ready
        1. Code repository usage guidelines
      2. How to do it...
      3. How it works...
        1. About the parent POM
        2. Lambda roles
        3. Lambda runtimes
        4. Extra dependencies
      4. There's more...
        1. Other ways to create Lambda functions from the management console
        2. Other ways to deploy code in a Lambda function
        3. Passing JSON to and from Lambda handler
      5. See also
    4. Your first Lambda with AWS CLI
      1. Getting ready
        1. Configuring AWS CLI
        2. Creating S3 bucket
        3. Note for Windows users
      2. How to do it...
      3. How it works...
        1. Creating a role and attaching a policy
        2. Lambda memory-size and timeout
        3. S3 Bucket and Key
        4. Cleaning up
      4. There's more...
      5. See also
    5. Your first Lambda with Amazon CloudFormation
      1. Getting ready
        1. Set up the project and S3 bucket
        2. Understanding YAML and JSON
      2. How to do it...
        1. Cleaning up roles, policy, and Lambda
      3. How it works...
      4. There's more...
        1. CloudFormation Template Components
        2. Resource component
        3. Intrinsic functions
        4. CloudFormation Designer
        5. Additional benefits of CloudFormation
        6. Cloud Formation alternatives
      5. See also
    6. Using AWS SDK, Amazon CloudFormation, and AWS CLI with Lambda
      1. Getting ready
      2. How to do it...
        1. Creating the POJOs for requests and response.
      3. How it works...
      4. There's more...
        1. Pseudo-parameters
      5. See also
    7. Dev Practices – dependency injection and unit testing
      1. Getting ready
        1. Code refactoring
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    8. Your first Lambda with serverless framework
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
  8. Building Serverless REST APIs with API Gateway
    1. Introduction
    2. Building your first API using the AWS CLI
      1. Getting ready
      2. How to do it...
      3. How it works...
        1. HTTP essentials
        2. REST essentials
        3. Parts of a URL
      4. There's more...
      5. See also
    3. Building your first API using Amazon CloudFormation
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    4. Building your first API with Lambda integration
      1. Getting ready
      2. How to do it...
        1. Creating the Lambda
        2. Creating a REST API with AWS CLI commands
        3. Creating a REST API with a CloudFormation template
      3. How it works...
        1. CLI commands
        2. CloudFormation template components
      4. There's more...
      5. See also
    5. Building and testing your first POST API method
      1. Getting ready
      2. How to do it...
        1. Creating the API with CLI commands
        2. Creating the API with a CloudFormation template
        3. Testing with Postman
      3. How it works...
        1. Passthrough behavior
      4. There's more...
      5. See also
    6. Mapping requests and responses with mapping templates
      1. Getting ready
      2. How to do it...
        1. Mapping requests
        2. Mapping responses
        3. Creating the API using CLI commands
        4. Creating the API with a CloudFormation template
        5. Testing the API
      3. How it works...
        1. Mapping templates and variables
        2. Using #set
      4. There's more...
        1. The Apache Velocity Language
        2. JSONPath expressions
      5. See also
    7. Validating request payloads with models
      1. Getting ready
      2. How to do it...
        1. The request data format
        2. Creating the API with CLI commands
        3. The CloudFormation template
          1. Testing the API
      3. How it works...
      4. There's more...
      5. See also
    8. Lambda and API with proxy integration
      1. Getting ready
      2. How to do it...
      3. Creating a Lambda function for proxy integration
        1. Deploying the Lambda
        2. Creating the Proxy API with CLI commands
        3. Creating the API using CloudFormation templates
      4. How it works...
        1. The greedy path, the ANY HTTP method, and proxy integration
        2. RequestStreamHandler versus RequestHandler
        3. The input and output format of a Lambda function for proxy integration
      5. There's more...
      6. See also
  9. Data Storage with Amazon DynamoDB
    1. Introduction
    2. Your first DynamoDB table
      1. Getting ready
      2. How to do it...
        1. Creating a table using CLI commands
          1. Creating a table using a CloudFormation template
      3. How it works...
        1. DynamoDB data model
        2. Data model limits
        3. DynamoDB keys and partitions
        4. Read and write capacity units
        5. Waiting for asynchronous operations
        6. Other ways to create tables
      4. There's more...
        1. DynamoDB features
        2. DynamoDB general limitations
        3. Local and global secondary indexes
      5. See also
    3. Throughput provisioning examples
      1. Getting ready
      2. How to do it...
        1. Scenario 1
        2. Scenario 2
        3. Scenario 3
      3. How it works...
        1. Strongly consistent reads versus eventually consistent reads
        2. Limits on throughput updates within a day
      4. There's more...
      5. See also
    4. Working with data from the CLI
      1. Getting ready
      2. How to do it...
        1. Adding items
        2. Reading items
        3. Updating items
        4. Deleting items
      3. How it works...
        1. Add, update, and delete operations
        2. Reading data from DynamoDB
        3. return-consumed-capacity
      4. There's more...
      5. See also
    5. Using the DynamoDB SDK from Lambda
      1. Getting ready
      2. How to do it...
      3. How it works...
        1. AmazonDynamoDB client versus DynamoDB wrapper client
        2. IAM policy and actions
        3. Single Lambda versus multiple Lambdas
      4. There's more...
      5. See also
    6. Creating tables from Lambda
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    7. Adding data items from Lambda
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    8. Reading data from Lambda
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
  10. Application Security with Amazon Cognito
    1. Introduction
    2. Creating a Cognito user pool
      1. Getting ready
      2. How to do it...
        1. Creating Cognito user pool with AWS CLI
        2. Creating Cognito user pool with CloudFormation template
      3. How it works...
        1. Generating and using JSON templates with CLI commands
        2. AliasAttributes versus UsernameAttributes
      4. There's more...
      5. See also
    3. Server-side authentication flow
      1. Getting ready
      2. How to do it...
        1. Creating Cognito user pool client
          1. Creating a Cognito user pool client with AWS CLI
          2. Creating a Cognito user pool client with CloudFormation template
        2. Server-side authentication flow
      3. How it works...
        1. Server-side, client-side, and custom authentication flows
        2. Secure Remote Password protocol
        3. The access token, refresh token, and ID token
        4. ADMIN_NO_SRP_AUTH versus USER_PASSWORD_AUTH
      4. There's more...
      5. See also
    4. Client-side authentication flow
      1. Getting ready
      2. How to do it...
        1. Creating a Cognito user pool client
          1. Creating a Cognito user pool client with AWS CLI
          2. Creating a Cognito user pool client with CloudFormation template
        2. Client-side authentication flow
      3. How it works...
      4. There's more...
      5. See also
    5. User sign-up flow with Cognito
      1. Getting ready
      2. How to do it...
        1. User sign-up with self-confirmation
        2. User sign-up with admin confirmation
      3. How it works...
      4. There's more...
      5. See also
    6. Working with groups
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    7. Integrating Cognito with the API gateway
      1. Getting ready
      2. How to do it...
        1. The AWS CLI
        2. The CloudFormation template
      3. How it works...
        1. Claim-based identity
      4. There's more...
      5. See also
    8. User sign-up with SMS verification and MFA
      1. Getting ready
      2. How to do it...
        1. Creating the user pool
          1. Creating a Cognito user pool client with AWS CLI
          2. Creating a Cognito user pool client with CloudFormation template
        2. User sign-up with SMS and MFA verification
      3. How it works...
      4. There's more...
      5. See also
  11. Web Hosting with S3, Route53, and CloudFront
    1. Introduction
    2. Setting up an S3 static website
      1. Getting ready
      2. How to do it...
        1. AWS CLI commands
        2. The CloudFormation template
      3. How it works...
      4. There's more...
      5. See also
    3. Setting up CloudFront for an S3 website
      1. Getting ready
      2. How to do it...
        1. AWS CLI commands
          1. Understanding the DistributionConfig defaults in the response
        2. The CloudFormation template
      3. How it works...
      4. There's more...
      5. See also
    4. Registering a domain name with Route 53
      1. Getting ready
      2. How to do it...
        1. Registering a domain with AWS CLI commands
      3. How it works...
        1. Hosted zones
      4. There's more...
        1. Registering a domain from AWS Management Console
      5. See also
    5. Using domains registered with other registrars
      1. Getting ready
      2. How to do it...
        1. AWS CLI commands
        2. The CloudFormation template
      3. How it works...
        1. DNS propagation
      4. There's more...
        1. Steps for configuring DNS from the AWS Management Console
      5. See also
    6. Using custom domains with S3 static websites
      1. Getting ready
      2. How to do it...
        1. AWS CLI commands
        2. The CloudFormation template
      3. How it works...
      4. There's more...
      5. See also
    7. Using custom domain names with CloudFront
      1. Getting ready
      2. How to do it...
        1. AWS CLI commands
        2. The CloudFormation template
          1. The CloudFront distribution stack
          2. The RecordSet stack
      3. How it works...
      4. There's more...
        1. Using HTTPS to secure your domain URL
        2. Disabling and deleting a CloudFront distribution
      5. See also
    8. Using HTTPS with a CloudFront domain
      1. Getting ready
      2. How to do it...
        1. Associating a certificate with a domain using AWS CLI commands
        2. Associating a certificate with a domain using CloudFormation template
      3. How it works...
        1. Server Name Identification (SNI)
      4. There's more...
      5. See also
  12. Messaging and Notifications with SQS and SNS
    1. Introduction
    2. Your first SQS queue (AWS CLI + CloudFormation)
      1. Getting ready
      2. How to do it...
        1. Creating an SQS queue
          1. AWS CLI commands
          2. The CloudFormation template
        2. Sending and receiving data (AWS CLI)
      3. How it works...
      4. There's more...
      5. See also
    3. Creating an SQS queue and sending messages with SDK (Java)
      1. Getting ready
      2. How to do it...
        1. Lambda project code (Java)
        2. Provisioning and testing the Lambda (AWS CLI)
      3. How it works...
      4. There's more...
      5. See also
    4. Receiving and sending SQS messages in batches with SDK (Java)
      1. Getting ready
      2. How to do it...
        1. Lambda project code (Java)
        2. Setting up queues and data
        3. Provisioning and testing the Lambda (AWS CLI)
      3. How it works...
      4. There's more...
      5. See also
    5. Invoking the Lambda with an SQS event (Java)
      1. Getting ready
      2. How to do it...
        1. Lambda project code (Java)
        2. Setting up queues and data
        3. Provisioning the Lambda (AWS CLI)
        4. Testing the Lambda (AWS CLI)
      3. How it works...
      4. There's more...
      5. See also
    6. Your first SNS topic for email and SMS (AWS CLI + CloudFormation)
      1. Getting ready
      2. How to do it...
        1. Creating an SNS topic
          1. AWS CLI
          2. The CloudFormation template
        2. Creating email and SMS subscriptions (AWS CLI)
          1. SMS subscription
          2. Email subscription
          3. Publishing a message
      3. How it works...
      4. There's more...
      5. See also
    7. Publishing to an SNS topic with SDK (Java)
      1. Getting ready
      2. How to do it...
        1. Lambda project code (Java)
        2. Provisioning and testing the Lambda (AWS CLI)
      3. How it works...
      4. There's more...
      5. See also
    8. Invoking a Lambda with SNS events (Java)
      1. Getting ready
      2. How to do it...
        1. Lambda project code (Java)
        2. Provisioning the Lambda (AWS CLI)
        3. Testing the Lambda (AWS CLI)
      3. How it works...
      4. There's more...
      5. See also
  13. Redshift, Amazon ML, and Alexa Skills
    1. Introduction
    2. Your first Kinesis data stream (AWS CLI)
      1. Getting ready
      2. How to do it...
        1. Step 1 - Creating a Kinesis data stream
          1. Using AWS CLI
          2. Using the CloudFormation template
        2. Step 2 - Adding and retrieving data
      3. How it works...
        1. Kinesis shard iterator types
      4. There's more...
      5. See also
    3. Writing data into Kinesis Stream with SDK (Java)
      1. Getting ready
      2. How to do it...
        1. Step 1 - Creating the Lambda project (Java)
        2. Step 2 - Provisioning and testing Lambda (AWS CLI)
      3. How it works...
      4. There's more...
      5. See also
    4. Invoking Lambda with Kinesis events (Java)
      1. Getting ready
      2. How to do it...
        1. Step 1 - Creating a Lambda project (Java)
        2. Step 2 - Provisioning and testing Lambda (AWS CLI)
      3. How it works...
      4. There's more...
      5. See also
    5. Using Amazon ML for binary classification (AWS CLI)
      1. Getting ready
      2. How to do it...
      3. How it works...
        1. Types of models
        2. DataSource object
        3. Receiver Operating Characteristic and Area Under the ROC
      4. There's more...
      5. See also
    6. Building and testing an Alexa skill (Java for Lambda, CLI for Alexa skill)
      1. Getting ready
        1. Installing and configuring the ASK CLI
          1. Configuring ask-cli for the first time
      2. How to do it...
        1. Step 1 - Creating the Lambda project (Java)
        2. Step 2 - Provisioning Lambda (AWS CLI)
        3. Step 3 - Building an Alexa skill with ASK CLI
      3. How it works...
        1. Alexa skill invocation basics
        2. Explaining the Lambda project (Java)
        3. Explaining the ASK CLI steps
      4. There's more...
        1. Voice app platforms
      5. See also
  14. Monitoring and Alerting with Amazon CloudWatch
    1. Introduction
    2. Checking logs, insights, and metrics (Console)
      1. Getting ready
      2. How to do it...
        1. Step 1: Logging in to the CloudWatch dashboard
        2. Logs
        3. Log insights
        4. Metrics
      3. How it works...
        1. Log groups
        2. Log streams
        3. Log insights
        4. Metrics
      4. There's more...
      5. See also
    3. Your first custom metric (AWS CLI)
      1. Getting ready
      2. How to do it...
        1. Step 1–Create a simple metric without dimensions
        2. Step 2—Add dimensions to metric data
      3. How it works...
      4. There's more...
      5. See also
    4. Setting up CloudWatch alarms (AWS CLI)
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    5. CloudWatch alarms with dimensions (AWS CLI)
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    6. Using CloudWatch metric log filters
      1. Getting ready
      2. How to do it...
        1. Creating metric filters from AWS CLI
        2. Creating metric filters from the console
      3. How it works...
      4. There's more...
      5. See also
  15. Serverless Programming Practices and Patterns
    1. Introduction
    2. Enabling CORS for the API and testing with CodePen
      1. Getting ready
        1. Getting familiar with CodePen
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    3. Implementing and testing Cognito operations with the JavaScript SDK
      1. Getting ready
        1. Download the amazon-cognito-identity.min.js file
        2. Create an S3 bucket and upload the amazon-cognito-identity.min.js file
        3. Creating a Cognito user pool and client
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    4. Federated identity with Amazon Cognito
      1. Getting ready
      2. How to do it...
        1. Step 1 - Configuring Google Plus
        2. Step 2 - Creating and Configuring an Identity Pool
        3. Step 3 - Preparing and uploading code files
          1. Preparing the index.html file
          2. Deploying and testing the index.html file
      3. How it works...
      4. There's more...
      5. See also
    5. Creating SSL/TLS certificate with ACM
      1. Getting ready
      2. How to do it...
        1. AWS CLI Commands
        2. CloudFormation Template
      3. How it works...
      4. There's more...
      5. See also
    6. Fan-in and fan-out architectural patterns with AWS
      1. Getting ready
      2. How to do it...
        1. Scenario 1 - Pub-Sub with multiple producers and consumers
        2. Scenario 2 - Parallel processing without duplicates
        3. Scenario 3 - Streaming real-time data for processing
        4. Scenario 4 - Streaming real-time data for storing
      3. How it works...
        1. Real-world example scenarios
          1. Scenario 1 - Pub-Sub scenario with multiple producers and consumers
          2. Scenario 2 - Parallel processing
          3. Scenario 3 - Streaming real-time data for processing
          4. Scenario 4 - Streaming real-time data for storage
      4. There's more...
        1. Building a full stack web application on AWS
      5. See also
  16. Other Cloud Providers
    1. Introduction
    2. Your first Serverless application in Azure
      1. Getting ready
      2. How to do it...
        1. Deploying the function to Azure
        2. Getting function info from UI and verifying the deployment
      3. How It works...
      4. There's more...
      5. See also
    3. Your first serverless application on Google Cloud Platform
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    4. Your first Serverless application on IBM cloud
      1. Getting ready
      2. How to do it...
      3. How It works...
      4. There's more...
      5. See also
  17. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Serverless Programming Cookbook
  • Author(s): Heartin Kanikathottu
  • Release date: January 2019
  • Publisher(s): Packt Publishing
  • ISBN: 9781788623797