Blog What we do Support Community
Login Sign up

Build and Deploy Functions to Cloudflare’s 152+ Data Centers with Serverless

by Stephen Pinkerton.

We’re very excited to announce that Cloudflare Workers are now integrated into the Serverless framework as a serverless cloud provider! Serverless’ open source framework has become a must-have for many developers, and we want to make it as simple as possible to harness the power of Cloudflare’s distributed computing network.

Workers have become essential to the way people build applications on the web. The expressiveness of modern JavaScript combined with sub-30 second deploys to Cloudflare’s network of 152+ datacenters means that you can truly build your application into our global compute network. Up until this point, deploying Workers required doing all of your editing through our browser-based IDE or developing one’s own custom tooling on top of our API. But many developers have their own environments and are already familiar with the Serverless framework, so it seemed natural that we build first-class support for Workers into Serverless!

You can now define the entire structure and routing behavior of your Workers scripts in code and deploy them with ease using serverless deploy from your own development environment. Store configuration files in version control alongside your application code. And feel more confident testing your application with serverless invoke, a new way to quickly send requests to endpoints of interest with specific arguments and headers.

What is Serverless?

Serverless is a paradigm for building applications without concern for the underlying infrastructure. It lets developers focus on writing code and lets them deploy quickly and cheaply to a cloud provider which manages servers, networking, and configuration.

The Serverless framework takes this a step further. It empowers developers to write applications with consistent tooling and project structure, which speeds up development and simplifies deployment to common cloud providers.

Our new plugin lets you use serverless command line tool, which you may already be accustomed to, but with Cloudflare Workers! Define the structure of your Worker in serverless.yml and serverless deploy it to our 152+ data centers around the world.

How simple is it? Here’s a complete serverless.yml for a Slackbot running on Workers. Serverless uses the idea of functions (as in functions-as-service) to separate concerns within an application. For Workers, a function is your application, and the script field refers to the name of your Worker script locally on disk.

service:
    name: slack-bot
    config:
      accountId: CLOUDFLARE_ACCOUNT_ID 
      zoneId: CLOUDFLARE_ZONE_ID 
      workers:
        slackbot:
          routes:
            - slackbot.example.com

provider:
  name: cloudflare

plugins:
  - serverless-cloudflare-workers

functions:
  SlackBot:
    worker: slackbot
    script: bot

Learn how retrieve your Cloudflare account and zone IDs

To deploy your new Worker, simply run serverless deploy or serverless deploy -f SlackBot.

Getting Started

$ npm install -g serverless
$ serverless create --template cloudflare-workers --path new-project
$ cd new-project
$ npm install
$ export CLOUDFLARE_AUTH_EMAIL=you@example.com
$ export CLOUDFLARE_AUTH_KEY=YOUR_API_KEY
$ serverless deploy

Enterprise Features

Use the cloudflare-workers-enterprise template if you're on an enterprise plan in order to deploy multiple scripts to Workers.

Learn More

Interested? Get started by reading the docs.

Looking to build even more powerful Workers? Check out the recipes on the docs about including NPM modules in your project and performing additional routing within your functions.

Are you building something cool with Workers? Let us know!


If you are in the San Francisco Bay Area, and are interested in hearing from Bay Area serverless experts and explorers, you're invited to come by tonight's Serverless Meetup. RSVP here! Doors open at 6.

comments powered by Disqus