Learning Apache OpenWhisk

Book Description

Serverless computing greatly simplifies software development. Your team can focus solely on your application while the cloud provider manages the servers you need. This practical guide shows you step-by-step how to build and deploy complex applications in a flexible multicloud, multilanguage environment using Apache OpenWhisk. You’ll learn how this platform enables you to pursue a vendor-independent approach using preconfigured containers, microservices, and Kubernetes as your cloud operating system.

Michele Sciabarrà demonstrates how to build a serverless application using classical design patterns and the programming language or languages that best fit your task. You’ll start by building a simple serverless application hands-on before diving into the more complex aspects of the OpenWhisk platform.

  • Examine how OpenWhisk’s serverless architecture works, including the use of packages, actions, sequences, triggers, rules, and feeds
  • Learn how OpenWhisk compares to existing architectures, such as Java Enterprise Edition
  • Manipulate OpenWhisk features using the command-line interface or a JavaScript API
  • Design applications using common Gang of Four design patterns
  • Use architectural design patterns such as model-view-controller to combine several OpenWhisk actions
  • Learn how to test and debug your code in a serverless environment

Publisher Resources

View/Submit Errata

Table of Contents

  1. Foreword
  2. Preface
    1. Why Serverless?
    2. Conventions Used in This Book
    3. Using Code Examples
    4. O’Reilly Online Learning
    5. How to Contact Us
  3. I. Introducing OpenWhisk Development
  4. 1. Serverless and OpenWhisk Architecture
    1. OpenWhisk Architecture
      1. Functions and Events
      2. Architecture Overview
      3. Programming Languages for OpenWhisk
      4. Actions and Action Composition
      5. Action Chaining
    2. How OpenWhisk Works
      1. Nginx
      2. Controller
      3. Load Balancer
      4. Invoker
      5. Client
    3. Serverless Execution Constraints
      1. Actions Are Functional
      2. Actions Are Event-Driven
      3. Actions Do Not Have Local State
      4. Actions Are Time-Bound
      5. Actions Are Not Ordered
    4. From Java EE to Serverless
      1. Classic Java EE Architecture
      2. Serverless Equivalent of Java EE
    5. Summary
  5. 2. A Simple OpenWhisk Application
    1. Getting Started
      1. The Bash CLI
      2. The IBM Cloud
    2. Creating a Simple Contact Form
    3. Form Validation
      1. Address Validation
      2. Returning the Result
    4. Saving Form Data
      1. Invoking Actions
      2. Storing in the Database
    5. Sending an Email
      1. Configuring Mailgun
      2. Writing an Action to Send Email
      3. Creating an Action Sequence
    6. Summary
  6. 3. The OpenWhisk CLI and JavaScript API
    1. The wsk Command
      1. Configuring the wsk Command
      2. OpenWhisk Entity Names
    2. Defining Packages
      1. Package Binding
    3. Creating Actions
      1. Chaining Sequences of Actions
      2. Including Some Code of Your Own as a Library
    4. Inspecting Activations
    5. Managing Triggers and Rules
      1. Putting the Trigger to Work
    6. Using a Feed
    7. Generic JavaScript APIs
      1. Asynchronous Invocation
      2. Using Promises
      3. Creating a Promise
    8. Using the OpenWhisk API
      1. Invoking OpenWhisk Actions
      2. Firing Triggers
      3. Inspecting Activations
    9. Summary
  7. 4. Common Design Patterns in OpenWhisk
    1. Built-in Patterns
      1. Singleton
      2. Facade
      3. Prototype
      4. Decorator
    2. Patterns Commonly Implemented with Actions
      1. Strategy
      2. Chain of Responsibility
      3. Command
    3. Summary
  8. 5. Integration Design Patterns in OpenWhisk
    1. Integration Patterns
      1. Proxy
      2. Adapter
      3. Bridge
      4. Observer
    2. User Interaction Patterns
      1. Composite
      2. Visitor
      3. MVC
    3. Summary
  9. 6. Unit Testing OpenWhisk Applications
    1. Using the Jest Test Runner
      1. Using Jest
      2. Running Tests Locally
      3. Snapshot Testing
    2. Mocking
      1. What Is a Mock?
      2. Mocking an HTTPS Request
    3. Mocking the OpenWhisk API
      1. Using the Mocking Library to Invoke an Action
      2. Mocking Action Parameters
      3. Mocking a Sequence
    4. Summary
  10. II. Advanced OpenWhisk Development
  11. 7. Developing OpenWhisk Actions in Python
    1. The Python Runtime
      1. What’s in the Python Runtime?
      2. Libraries Available in the Runtime
    2. Using Third-Party Libraries
      1. Packaging a Python Application in a Zip File
      2. Using virtualenv
      3. How Virtualenv and Pip Work
      4. Automating the Virtual Environment
      5. Using the yattag Library
      6. Building the Virtualenv, Including a Library
    3. Using the OpenWhisk REST API
      1. Authentication
      2. Connecting to the API with curl
    4. Using the OpenWhisk REST API in Python
    5. Invocations, Activations, and Triggers in Python
      1. Blocking Action Invocation
      2. Nonblocking Trigger Invocation
      3. Retrieving the Result of an Invocation
    6. Testing Python Actions
      1. Recreating the Python Runtime Environment Locally
      2. Unit Test Examples
      3. Invoking the OpenWhisk API Locally
      4. Mocking Requests
    7. Summary
  12. 8. Using CouchDB with OpenWhisk
    1. How to Query CouchDB
    2. Exploring CouchDB on the Command Line
      1. How CouchDB works
      2. Creating Database
      3. Create
      4. Retrieve
      5. Update
      6. Delete
      7. Attachments
    3. Querying CouchDB
      1. Searching the Database
      2. Indexes
      3. Fields
      4. Pagination Support
      5. Bookmark Feature
      6. Selectors
      7. Operators
    4. CouchDB Design Documents
      1. Creating a Design Document
    5. View Functions
      1. Extracting Data with map Functions
      2. Implementing a Join with map Functions
      3. Joining with a Single Document
      4. Aggregations with reduce Functions
    6. Validation Functions
    7. Using the Cloudant Package
      1. CRUD Actions in the Cloudant Package
      2. Queries and Views with Packages
    8. Summary
  13. 9. An OpenWhisk Web Application in Python
    1. CRUD Application Architecture
      1. Deploying the Action
    2. Abstracting Database Access
      1. Implementing model.init()
      2. Implementing model.insert()
      3. Implementing model.find()
      4. Testing insert and find
      5. Implementing model.update() and model.delete()
      6. Testing update and delete
    3. The User Interface
      1. Testing
      2. Rendering the Table with view.table
      3. Rendering the Form with view.form
    4. The Controller
      1. Processing Operations
      2. Side Effects
    5. Advanced Web Actions
    6. Improving the CRUD Application
    7. Validation and Error Reporting
      1. Storing Error Messages
    8. Pagination
      1. Creating an Index
      2. Using Bookmarks and Limits
      3. Pagination
      4. Processing the Bookmark
    9. Uploading and Displaying Images
      1. File Upload Form
      2. Parsing the File Upload
      3. Saving Data in the Database
      4. Generating an <IMG> Tag
      5. Generating a URL to Retrieve an Image
      6. Rendering the Image with an HTTP Request
    10. Summary
  14. 10. Developing OpenWhisk Actions in Go
    1. Your First Golang Action
      1. From Echo to Hello
    2. Packaging Multiple Files
      1. Imports, GOPATH, and the vendor Folder
      2. Actions with Multiple Files in main
      3. Actions with Multiple Packages
    3. Actions Using Third-Party Libraries
      1. How Go Uses Third-Party Open Source Libraries
      2. Selecting a Given Version of a Library
      3. Action Precompilation
    4. Testing Go Actions
      1. Writing Tests
      2. Testing Using Examples
    5. Embedding Resources
      1. Using packr
    6. Serving Resources with Web Actions
    7. Accessing the OpenWhisk API in Go
      1. Utilities
      2. HTTP Requests
      3. Invoking an OpenWhisk Action
      4. Firing a Trigger
      5. Retrieving the Data Associated with the Activation ID
    8. Summary
  15. 11. Using Kafka with OpenWhisk
    1. Introducing Apache Kafka
      1. Kafka Brokers and Protocol
      2. Messages and Keys
      3. Topics and Partitions
      4. Offsets and Client Groups
    2. Creating a Kafka Instance in the IBM Cloud
      1. Creating an Instance
      2. Creating a Topic
      3. Get Credentials
    3. Using the messaging Package
      1. Creating a Binding and a Feed
      2. Receiving Messages with an Action
      3. Sending Messages Using kafkacat
      4. Testing the Kafka Broker
    4. A Kafka Producer in Go
      1. Creating a Producer
      2. Sending a Kafka Message
      3. Writing a Sender Action
      4. Deploying and Testing the Producer
    5. A Kafka Consumer in Go
      1. Creating a Consumer
      2. Receiving a Message
      3. Writing a Receiver Action
      4. Testing the Consumer
    6. Implementing the Web Chat Application
      1. Overview
      2. User Interface
      3. Initializing
      4. Joining
      5. Receiving
      6. Sending
    7. Summary
  16. 12. Deploying OpenWhisk with Kubernetes
    1. Installing Kubernetes
      1. Installation Types
    2. Installing kubectl and Helm
    3. Installing Kubernetes Locally
    4. Installing Kubernetes in the Cloud
      1. Architecture of a Kubernetes Cloud Deployment
      2. Generic Procedure for Installing Kubernetes with cloud-init
      3. Installing on Hetzner Cloud
      4. Installing on AWS Cloud
    5. Installing Kubernetes on a Bare Metal Server
      1. Collecting the Required Software
      2. Network Configuration
      3. Scripts for the Installation
      4. Creating the Cluster
    6. Installing OpenWhisk
      1. Configuring Kubectl
      2. Configuring Helm
      3. Installing in Docker Desktop
      4. Installing in the Kubernetes Cluster
    7. Configuring the OpenWhisk Command-Line Interface
      1. Configuring wsk Insecurely for Docker Desktop
      2. Creating a New Namespace
    8. Summary
    9. Conclusion
  17. Index

Product Information

  • Title: Learning Apache OpenWhisk
  • Author(s): Michele Sciabarrà
  • Release date: July 2019
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781492046165