ben willkommen 5a50e77e5a Updated docs
Changed docs so that name property of the matches the declared function name in serverless.yml examples, added comment indicating they must match. Fixed two typos.
2018-12-19 19:37:35 -06:00

2.8 KiB
Raw Blame History

Read this on the main serverless docs site

Cloudflare Workers - Functions

If you are using Cloudflare as a provider, all functions inside the service are Cloudflare Workers.

Configuration

All of the Cloudflare Workers in your serverless service can be found in serverless.yml under the functions property.

# serverless.yml
 
service:
    name: hello-world
    config:
      accountId: CLOUDFLARE_ACCOUNT_ID 
      zoneId: CLOUDFLARE_ZONE_ID 

provider:
  name: cloudflare

plugins:
  - serverless-cloudflare-workers

functions:
  helloWorld:
    # What the script will be called on Cloudflare (this property value must match the function name one line above)
    name: helloWorld
    # The name of the script on your machine, omitting the .js file extension
    script: helloWorld
    # Events are only relevant to the `serverless invoke` command and dont affect deployment in any way
    events:
      - http:
          url: example.com/hello/*
          method: GET
          headers:
            someKey: someValue

The script property points to the file containing your Cloudflare Worker.

// helloWorld.js
 
addEventListener('fetch', event => {
    event.respondWith(handleRequest(event.request))
  })
 
  async function handleRequest(request) {
    return new Response("Hello world")
  }

If you have an Enterprise Cloudflare account, you can add multiple Cloudflare Workers to your project.

# serverless.yml
 
service:
    name: hello-world
    config:
      accountId: CLOUDFLARE_ACCOUNT_ID 
      zoneId: CLOUDFLARE_ZONE_ID 

provider:
  name: cloudflare

plugins:
  - serverless-cloudflare-workers

functions:
  helloWorld:
    # What the script will be called on Cloudflare (this property value must match the function name one line above)
    name: helloWorld
    # The name of the script on your machine, omitting the .js file extension
    script: helloWorld
    # Events are only relevant to the `serverless invoke` command and dont affect deployment in any way
    events:
      - http:
          url: example.com/hello/*
          method: GET
          headers:
            someKey: someValue


  # Only Enterprise accounts would be allowed to add this second function and its corresponding route above
  foo:
    name: foo
    script: bar
    events:
      - http:
          url: example.com/foo/*
          method: GET

The script property is what the Cloudflare Worker will be called on Cloudflares data centers.