Subscribe to receive notifications of new posts:

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

2018-09-11

2 min read

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.

Cloudflare's connectivity cloud protects entire corporate networks, helps customers build Internet-scale applications efficiently, accelerates any website or Internet application, wards off DDoS attacks, keeps hackers at bay, and can help you on your journey to Zero Trust.

Visit 1.1.1.1 from any device to get started with our free app that makes your Internet faster and safer.

To learn more about our mission to help build a better Internet, start here. If you're looking for a new career direction, check out our open positions.
ServerlessCloudflare WorkersCloudflare NetworkData CenterDevelopersDeveloper Platform

Follow on X

Cloudflare|@cloudflare

Related posts

October 31, 2024 1:00 PM

Moving Baselime from AWS to Cloudflare: simpler architecture, improved performance, over 80% lower cloud costs

Post-acquisition, we migrated Baselime from AWS to the Cloudflare Developer Platform and in the process, we improved query times, simplified data ingestion, and now handle far more events, all while cutting costs. Here’s how we built a modern, high-performing observability platform on Cloudflare’s network. ...