/docs/providers/cloudflare/guide/deploying.md

https://github.com/serverless/serverless · Markdown · 114 lines · 79 code · 35 blank · 0 comment · 0 complexity · 08189bcb02f2cefd0d1c7c3b283559f7 MD5 · raw file

  1. <!--
  2. title: Serverless Framework - Cloudflare Workers Guide - Deploying
  3. menuText: Deploying
  4. menuOrder: 7
  5. description: How to deploy your Cloudflare Workers functions and their required infrastructure
  6. layout: Doc
  7. -->
  8. <!-- DOCS-SITE-LINK:START automatically generated -->
  9. ### [Read this on the main serverless docs site](https://www.serverless.com/framework/docs/providers/cloudflare/guide/deploying)
  10. <!-- DOCS-SITE-LINK:END -->
  11. # Cloudflare Workers - Deploying
  12. The Serverless Framework was designed to provision your Cloudflare Workers Functions and Events. It does this via a couple of methods designed for different types of deployments.
  13. ## prerequisites
  14. In order to deploy your Cloudflare Worker, you need to set your Cloudflare email as an environmental variable called `CLOUDFLARE_AUTH_EMAIL`, and your Cloudflare Global API Key as an environmental variable called `CLOUDFLARE_AUTH_KEY`. 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](https://dash.cloudflare.com/) 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](https://dash.cloudflare.com/).
  15. Environmental variables are variables that live inside your terminal.
  16. For Mac and Linux users, you can set environmental variables like this:
  17. ```bash
  18. export CLOUDFLARE_AUTH_KEY=YOUR_API_KEY_HERE
  19. export CLOUDFLARE_AUTH_EMAIL=YOUR_CLOUDFLARE_EMAIL
  20. ```
  21. And for Windows (CMD) users, you can set environmental variables like this:
  22. ```bash
  23. set CLOUDFLARE_AUTH_KEY=YOUR_API_KEY_HERE
  24. set CLOUDFLARE_AUTH_EMAIL=YOUR_CLOUDFLARE_EMAIL
  25. ```
  26. Youll need to redefine your environmental variables after each time you close your terminal.
  27. ## Deploy All
  28. This is the main method for doing deployments with the Serverless Framework:
  29. ```bash
  30. serverless deploy
  31. ```
  32. Use this method when you have updated your Function, Event or Resource configuration in `serverless.yml` and you want to deploy that change (or multiple changes at the same time) to your Cloudflare Worker. If you've made changes to any of your routes since last deploying, the Serverless Framework will update them on the server for you.
  33. **Note:** You can specify a different configuration file name with the the `--config` option.
  34. ### How It Works
  35. The Serverless Framework reads in `serverless.yml` and uses it to provision your Functions.
  36. For each defined Function in your `serverless.yml` file, the Framework will create a Cloudflare Workers script. This means that only enterprise customers can declare more than one Function, but anyone can use [webpack](https://developers.cloudflare.com/workers/writing-workers/using-npm-modules/) to package their application into a single script.
  37. For example, let's take the following example `serverless.yml` file:
  38. ```yml
  39. # serverless.yml
  40. service:
  41. name: hello-world
  42. config:
  43. accountId: CLOUDFLARE_ACCOUNT_ID
  44. zoneId: CLOUDFLARE_ZONE_ID
  45. provider:
  46. name: cloudflare
  47. plugins:
  48. - serverless-cloudflare-workers
  49. functions:
  50. helloWorld:
  51. # What the script will be called on Cloudflare (this property value must match the function name one line above)
  52. name: helloWorld
  53. # The name of the script on your machine, omitting the .js file extension
  54. script: helloWorld
  55. # Events are optional to declare and only affect the `serverless invoke` command
  56. events:
  57. - http:
  58. url: example.com/hello/*
  59. method: GET
  60. headers:
  61. greeting: hi
  62. # Only Enterprise accounts would be allowed to add this second function
  63. foo:
  64. name: foo
  65. script: bar
  66. events:
  67. - http:
  68. url: example.com/foo/*
  69. method: GET
  70. ```
  71. After deploying that file, youll be able to hit the specified top-level routes of your zone, `example.com/hello/*` and `example.com/foo/*`, and any endpoints that resolve to these routes, like `example.com/hello/user` and `example.com/foo/bar`.
  72. ## Deploy Function
  73. This deployment method updates or deploys a single function. It performs the platform API call to deploy your package without the other resources. It is much faster than re-deploying your whole service each time.
  74. ```bash
  75. serverless deploy --function myFunction
  76. ```
  77. If you've made changes to the routes corresponding to this Function since last deploying, the Serverless Framework will update them on the server for you.
  78. ### Tips
  79. Check out the [deploy command docs](../cli-reference/deploy.md) for all details and options.