Edit on github

#Cloudflare Workers - Quickstart

#Pre-requisites

Node.js v6.5.0 or later. Serverless CLI v1.31.0 or later. You can run npm install -g serverless to install it.

#Create a new service

Create a new service using either the cloudflare-workers or cloudflare-workers-enterprise template, depending on your Cloudflare domain’s plan level, and specifying a unique name and an optional path for your service.

# Create a new Serverless Service/Project
$ serverless create --template cloudflare-workers --path new-project
# Change into the newly created directory
$ cd new-project
# Install npm dependencies
$ npm install

Note: there are two templates for Cloudflare Workers: cloudflare-workers and cloudflare-workers-enterprise. The enterprise template sets up a project that can natively deploy multiple scripts, each with their own routes. It requires an enterprise Cloudflare account to use.

The cloudflare-workers template still supports conditional routing and multiple scripts if you use a tool like webpack.

#Deploy, test and diagnose your service

You will need to set your Global API key from Cloudflare as an environmental variable named CLOUDFLARE_AUTH_KEY, and your Cloudflare account email as an environmental variable named CLOUDFLARE_AUTH_EMAIL. You can get your Global API key from your Cloudflare profile page. You will also need to set accountId and zoneId in serverless.yml under service.config. The first part of the path when you open Cloudflare dashboard as a logged in user is your accountId, e.g. dash.cloudflare.com/{accountId}. And the zoneId can be found from the overview tab after selecting the desired zone from the Cloudflare dashboard.

Environmental variables are variables that live inside your terminal.

For Mac and Linux users, you can set environmental variables like this:

export CLOUDFLARE_AUTH_KEY=YOUR_API_KEY_HERE
export CLOUDFLARE_AUTH_EMAIL=YOUR_CLOUDFLARE_EMAIL

And for Windows (CMD) users, you can set environmental variables like this:

set CLOUDFLARE_AUTH_KEY=YOUR_API_KEY_HERE
set CLOUDFLARE_AUTH_EMAIL=YOUR_CLOUDFLARE_EMAIL

You’ll need to redefine your environmental variables after each time you close your terminal.

  1. Deploy the Service

Use this when you have made changes to your Functions, Events or Resources in serverless.yml or you simply want to deploy all changes within your Service at the same time. If you've made changes to your routes since last deploying, the Serverless Framework will update them on the server for you.

serverless deploy
  1. Deploy the Function

Use this to quickly upload and overwrite your function code, allowing you to develop faster.

serverless deploy -f hello
  1. Invoke the Function

Invokes the Function and returns results.

serverless invoke --function helloWorld

Hello world

Note that for invoke, your Function must have the events field populated in order for the serverless tool to know exactly which route to request. Defining the headers field is optional.

# serverless.yml
...
foo:
    worker: foo_script
    script: bar
    events:
      - http:
          url: example.com/foo/bar
          # Defines the method used by serverless when the `invoke` command is used. Cloudflare Workers only support GET requests for now
          method: GET
          headers:
            someKey: someValue

#Cleanup

If at any point, you no longer need your service, you can run the following command to remove the Functions, Events and Resources that were created.

serverless remove

Made with love in San Francisco + Atlanta, Austria, Germany, Pakistan, Poland, Nebraska & Thailand

Serverless, Inc. © 2018

Join our newsletter and get the latest news about Serverless products and happenings. #noSpamWePromise