Serverless Apps on Cloudflare
Build Solutions, Not Infrastructure
by Ashley Peacock
Use serverless technologies to build applications that scale, more
quickly and easily, and without worrying about deployment. Whether
you’re writing an API, a full-stack app, or real-time code, harness the
power of serverless on Cloudflare’s platform so you can focus on what
you do best: delivering solutions. With hands-on instruction and code
samples throughout, you’ll go from building a simple API to analyzing
images with AI. And, when it’s time to launch, you’ll learn how to
deploy your applications and websites automatically, and how to optimize
their performance for production.
For decades, applications have been built and deployed in a similar way:
you write code, provision a server, and upload your code. Over the
years, you’ve graduated from FTP or SSH with dedicated servers to Docker
and Kubernetes in the cloud, but fundamentally you’ve just switched from
owning to renting; you’re paying 24/7 for the server hosting your app.
Serverless technologies change that model.
With serverless, you focus on writing code, and not how it’s deployed.
You only pay for when your application code is executed, rather than
paying for idle servers. In this book we’ll use Cloudflare, which was
built from the ground up to be a serverless platform, but the same
concepts apply to every serverless provider.
With this book, you’ll learn a better way to build applications, and
you’ll see improved productivity and ease of deployment. From secrets to
dependencies, such as databases and caches, Cloudflare has
revolutionized how to build applications, and has made building
applications easier and more fun. As you go through the book, you’ll
learn in detail what serverless is, how it functions behind the scenes,
and how to build serverless applications on Cloudflare. When you finish
this book, you’ll understand how to think in terms of serverless
functions, and you’ll have the experience of deploying and extending
these apps as you work through the examples.
What You Need
- Mac or PC
- GitHub account (free)
- Cloudflare account (free)
Resources
Releases:
- P1.0 2024/11/11
- B7.0 2024/10/29
- B6.0 2024/10/03
- B5.0 2024/09/25
- Acknowledgments
- Preface
- Who Should Read This Book?
- What’s in This Book?
- How to Read This Book
- Online Resources
- Introduction
- What is Serverless?
- Pay for What You Use
- Scalability
- High Availability
- No Server Maintenance
- Ready to Go Serverless?
- Deploy Your First Cloudflare Worker
excerpt
- Create a New Worker
- Configure a Worker
- Implement Logic in Your Workers
- Run Scheduled Tasks
- Deploy Your First Worker
- What You’ve Learned
- Build a Serverless API
- Route HTTP Requests
- Locally Test Workers
- Define HTTP Handlers
- Create Resources Using HTTP POST
- Define Routes with Parameters
- What You’ve Learned
- Run Tests Against a Worker
- Define the Test Setup
- Write a Test for 404s
- Write Tests for GET /images
- Test POST /images
- What You’ve Learned
- Persist Data with D1
- Create a D1 Database
- Connect a Database to a Worker
- Create a Migration
- Create Tables
- Run a Migration
- Retrieve Multiple Rows
- Retrieve a Single Row
- Insert Data
- What You’ve Learned
- Worker-to-Worker Communication
excerpt
- Create the Authentication Worker
- Make a Worker Private
- Add Authentication Logic
- Store Application Secrets
- Test the Authentication Worker
- Add a Service Binding
- Test a Service Binding
- What You’ve Learned
- Build a Static Website with Pages
- Create a Pages Project
- Understand the Project Structure
- Install Bootstrap
- Create the Home Page
- Run the Static Website Locally
- Deploy a Pages Application
- What You’ve Learned
- Add a Back End to the Front End
- Create a Pages Function
- Create a Serverless Next.js Route
- Test the Server-Side Endpoint
- Call the Server-Side Route from the Front End
- Run the Weather App Locally
- Deploy a Pages Application
- What You’ve Learned
- Cache Data with KV
- Create a New KV Namespace
- Bind KV Namespaces
- Cache Data in KV
- Test the Caching Locally
- Deploy to Cloudflare
- What You’ve Learned
- Upload and Store Files on R2
- Create a New Project
- Create an R2 Bucket
- Bind an R2 Bucket
- Upload Files to R2
- Create the Front End
- Test R2 Uploads Locally
- Deploy to Cloudflare
- What You’ve Learned
- Analyze Images Using Workers AI
- Add an AI Binding
- Execute AI
Models
- Render Image Classification Results
- Test Applications that Use AI
- What You’ve Learned
- Produce Messages to a Queue
- Add a Queue
- Publish to a Queue
- View Messages on the Dashboard
- What You’ve Learned
- Consume Messages from a Queue
- Poll for Analysis
- Test the Consumer
- What You’ve Learned
- WebSockets with Durable Objects
- Create a Worker with Durable Objects
- Serve HTML from a Worker
- Create Durable Objects
- Connect to WebSockets with Durable Objects
- Handle Server-Side WebSocket Messages
- Persist Data with Durable Objects
- Connect to WebSockets with JavaScript
- What You’ve Learned
- Automate Workers & Pages Deployments
- Deploy with Git Integration
- Deploy Pages with GitHub Actions
- Configure Wrangler per Environment
- Deploy a Worker with GitHub Actions
- Add Secrets
- Run a GitHub Action
- Apply Database Migrations
- What You’ve Learned
- Deploy to Production
- Register and Transfer Domains
- Add a Custom Domain to a Project
- Manage Different Environments
- View and Query Logs
- Define Advanced Routes for Workers
- Deploy Changes Gradually
- Configure Access to Your Applications
- Handle Emails
- Analytics, Security, and Speed
- What You’ve Learned
Author
Ashley Peacock is the author of the bestselling Creating Software
with Modern Diagramming
Techniques.
He has experience across the tech stack, and as a staff engineer and
architect working in the UK tech industry for over a decade, he has
built a number of serverless applications on Cloudflare. He couldn’t
believe how impressive and effortless the process was, which led him to
share that expertise in this book.