In this tutorial, you will follow step-by-step instructions to deploy a Hello World application using Cloudflare Workers and Pulumi infrastructure as code (IaC) to familiarize yourself with the resource management lifecycle. In particular, you will create a Worker, a Route, and a DNS Record to access the application before cleaning up all the resources.
Before you begin
Ensure you have:
A Cloudflare account and API Token with permission to edit the resources in this tutorial. If you need to, sign up for a Cloudflare account ↗ before continuing. Your token must have the following:
Account-Workers Scripts-Edit
permission
Zone-Workers Route-Edit
permission
Zone-DNS-Edit
permission
A Pulumi Cloud account. You can sign up for an always-free individual tier ↗ .
The Pulumi CLI is installed on your machine.
A Pulumi-supported programming language ↗ configured. (TypeScript, JavaScript, Python, Go, .NET, Java, or use YAML)
A Cloudflare-managed domain. Complete the Add a site tutorial to bring your existing domain under Cloudflare.
1. Initialize your project
A Pulumi project is a collection of files in a dedicated folder that describes the infrastructure you want to create. The Pulumi project folder is identified by the required Pulumi.yaml
file. You will use the Pulumi CLI to create and configure a new project.
a. Create a directory
Use a new and empty directory for this tutorial.
mkdir serverless-cloudflare
b. Login to Pulumi Cloud
Pulumi Cloud ↗ is a hosted service that provides a secure and scalable platform for managing your infrastructure as code. You will use it to store your Pulumi backend configurations.
At the prompt, press Enter to log into your Pulumi Cloud account via the browser. Alternatively, you may provide a Pulumi Cloud access token ↗ .
c. Create a new program
A Pulumi program is code written in a supported programming language ↗ that defines infrastructure resources.
To create a program, select your language of choice and run the pulumi
command:
pulumi new javascript --name serverless-cloudflare --yes
# wait a few seconds while the project is initialized
pulumi new typescript --name serverless-cloudflare --yes
# wait a few seconds while the project is initialized
pulumi new python --name serverless-cloudflare --yes
# wait a few seconds while the project is initialized
pulumi new go --name serverless-cloudflare --yes
# wait a few seconds while the project is initialized
pulumi new java --name serverless-cloudflare --yes
# wait a few seconds while the project is initialized
pulumi new csharp --name serverless-cloudflare --yes
# wait a few seconds while the project is initialized
pulumi new yaml --name serverless-cloudflare --yes
d. Create a stack
A Pulumi stack ↗ is an instance of a Pulumi program. Stacks are independently configurable and may represent different environments (development, staging, production) or feature branches. For this tutorial, you’ll use the dev
stack.
To instantiate your dev
stack, run:
# wait a few seconds for the stack to be instantiated.
You have not defined any resources at this point, so you’ll have an empty stack.
e. Save your application settings
In this step, you will store your application settings in a Pulumi ESC Environment ↗ , a YAML file containing configurations and secrets. These can be accessed in several ways, including a Pulumi program. All ESC Environments securely reside in your Pulumi Cloud account and can be fully managed via the Pulumi CLI. For this tutorial, you will store the following values:
Your Cloudflare account ID .
A valid Cloudflare API token .
A domain. For instance, example.com
.
# Give your new ESC Environment a name
# Initialize the new ESC Environment
pulumi config env init --env $E --yes
Creating environment hello-world/dev-env for stack dev...
# Replace abc123 with your Cloudflare account ID
pulumi env set $E --plaintext pulumiConfig.accountId abc123
# Replace API_TOKEN with your Cloudflare API token
pulumi env set $E --secret pulumiConfig.cloudflare:apiToken API_TOKEN
# Replace example.com with your domain
pulumi env set $E --plaintext pulumiConfig.domain example.com
f. Install the Cloudflare package
You need to install the Cloudflare package for your language of choice in order to define Cloudflare resources in your Pulumi program.
Install the Cloudflare package by running the following command:
npm install @pulumi/cloudflare
npm install @pulumi/cloudflare
echo "pulumi_cloudflare>=5.38,<6.0.0" >> requirements.txt
pip install -r requirements.txt
.. .Collecting pulumi-cloudflare...
go get github.com/pulumi/pulumi-cloudflare/sdk/v3/go/cloudflare
go: downloading github.com/pulumi/pulumi-cloudflare ...
Below are Apache Maven instructions. For other Java project managers such as Gradle, see the official Maven repository ↗
Open your pom.xml
file.
Add the Pulumi Cloudflare dependency inside the <dependencies>
section.
< groupId > com.pulumi </ groupId >
< artifactId > cloudflare </ artifactId >
< version > 5.38.0 </ version >
Run:
... [INFO] BUILD SUCCESS...
dotnet add package Pulumi.Cloudflare
info : Adding PackageReference for package 'Pulumi.Cloudflare' into project
There are no dependencies to download for YAML. Skip ahead.
2. Define Cloudflare resources in code
With the Cloudflare package installed, you can now define any supported Cloudflare resource ↗ in your Pulumi program. Next, define a Worker, a Route, and a DNS Record.
a. Add a Workers script
The Workers Script resource ↗ represents a Cloudflare Worker that can be deployed to the Cloudflare network.
Replace the contents of your entrypoint file with the following:
Filename: index.js
const pulumi = require ( "@pulumi/pulumi" ) ;
const cloudflare = require ( "@pulumi/cloudflare" ) ;
const config = new pulumi . Config () ;
const accountId = config . require ( "accountId" ) ;
const domain = config . require ( "domain" ) ;
const content = `export default {
const options = { headers: { 'content-type': 'text/plain' } };
return new Response("Hello World!", options);
const worker = new cloudflare . WorkersScript ( "hello-world-worker" , {
name : "hello-world-worker" ,
module : true , // ES6 module
Filename: index.ts
import * as pulumi from "@pulumi/pulumi" ;
import * as cloudflare from "@pulumi/cloudflare" ;
const config = new pulumi . Config () ;
const accountId = config . require ( "accountId" ) ;
const domain = config . require ( "domain" ) ;
const content = `export default {
const options = { headers: { 'content-type': 'text/plain' } };
return new Response("Hello World!", options);
const worker = new cloudflare . WorkersScript ( "hello-world-worker" , {
name : "hello-world-worker" ,
module : true , // ES6 module
Filename: __main__.py
import pulumi_cloudflare as cloudflare
ACCOUNT_ID = CONFIG . get ( "accountId" )
DOMAIN = CONFIG . require ( "domain" )
const options = { headers: { 'content-type': 'text/plain' } };
return new Response("Hello World!", options);
worker = cloudflare . WorkersScript ( "hello-world-worker" ,
name = "hello-world-worker" ,
Filename: main.go
" github.com/pulumi/pulumi-cloudflare/sdk/v5/go/cloudflare "
" github.com/pulumi/pulumi/sdk/v3/go/pulumi "
" github.com/pulumi/pulumi/sdk/v3/go/pulumi/config "
pulumi . Run ( func ( ctx * pulumi . Context ) error {
conf := config . New ( ctx , "" )
accountID := conf . Get ( "accountId" )
domain := conf . Get ( "domain" )
const options = { headers: { 'content-type': 'text/plain' } };
return new Response("Hello World!", options);
worker , err := cloudflare . NewWorkersScript ( ctx , "hello-world-worker" , & cloudflare . WorkersScriptArgs {
AccountId : pulumi . String ( accountID ),
Name : pulumi . String ( "hello-world-worker" ),
Content : pulumi . String ( content ),
Module : pulumi . Bool ( true ), // ES6 module
Filename: src/main/java/myproject/App.java
import com . pulumi . Pulumi ;
import com . pulumi . cloudflare . WorkersScript ;
import com . pulumi . cloudflare . WorkersScriptArgs ;
import com . pulumi . core . Output ;
public static void main ( String [] args ) {
const options = { headers: { 'content-type': 'text/plain' } };
return new Response("Hello World!", options);
var accountId = ctx . config (). require ( "accountId" );
var domain = ctx . config (). require ( "domain" );
var worker = new WorkersScript ( "hello-world-worker" , WorkersScriptArgs . builder ()
. name ( "hello-world-worker" )
Filename: Program.cs
using Cloudflare = Pulumi . Cloudflare ;
return await Deployment . RunAsync (() =>
var config = new Config ();
var accountId = config . Require ( "accountId" );
var domain = config . Require ( "domain" );
const options = { headers: { 'content-type': 'text/plain' } };
return new Response( "" Hello World! "" , options);
var worker = new Cloudflare . WorkersScript ( "hello-world-worker" , new ()
Name = "hello-world-worker" ,
Filename: Pulumi.yaml
name : serverless-cloudflare
type : cloudflare:WorkersScript
accountId : "${accountId}"
name : "hello-world-worker"
const options = { headers: { 'content-type': 'text/plain' } };
return new Response("Hello World!", options);
b. Add a Route
You will now add a Workers Route resource ↗ to your Pulumi program so the Workers script can have an endpoint and be active. To properly configure the Route, you will also look up the zone ID for your domain.
Add the following code snippet to your entrypoint file after the Worker script resource:
Filename: index.js
const zone = cloudflare . getZone ( {
const zoneId = zone . then ( z => z . zoneId ) ;
const route = new cloudflare . WorkersRoute ( "hello-world-route" , {
pattern : "hello-world." + domain ,
Filename: index.ts
const zone = cloudflare . getZone ( {
const zoneId = zone . then ( z => z . zoneId ) ;
const route = new cloudflare . WorkersRoute ( "hello-world-route" , {
pattern : "hello-world." + domain ,
Filename: __main__.py
zone = cloudflare . get_zone ( account_id = ACCOUNT_ID , name = DOMAIN )
route = cloudflare . WorkersRoute ( "hello-world-route" ,
pattern = "hello-world." + DOMAIN ,
Filename: main.go
zone , err := cloudflare . LookupZone ( ctx , & cloudflare . LookupZoneArgs {
route , err := cloudflare . NewWorkersRoute ( ctx , "hello-world-route" , & cloudflare . WorkersRouteArgs {
ZoneId : pulumi . String ( zone . Id ),
Pattern : pulumi . String ( "hello-world." + domain ),
Filename: src/main/java/myproject/App.java
final var zone = CloudflareFunctions . getZone ( GetZoneArgs . builder ()
var route = new WorkersRoute ( "hello-world-route" , WorkersRouteArgs . builder ()
. zoneId ( zone . applyValue ( getZoneResult -> getZoneResult . id ()))
. pattern ( "hello-world." + domain )
. scriptName ( worker . name ())
Filename: Program.cs
var zone = Output . Create ( Cloudflare . GetZone . InvokeAsync ( new ()
var route = new Cloudflare . WorkersRoute ( "hello-world-route" , new ()
ZoneId = zone . Apply ( z => z . Id ),
Pattern = "hello-world." + domain ,
ScriptName = worker . Name ,
Filename: Pulumi.yaml
Below the runtime
key, add the following code:
function : cloudflare:getZone
Below the worker
resource, add the following code:
type : cloudflare:WorkersRoute
pattern : "hello-world.${domain}"
scriptName : ${worker.name}
c. Add a DNS Record
You will now add a DNS Record resource ↗ to resolve the previously configured Route. In the next step, you’ll also output the Route URL so it can be easily accessed.
Add the following code snippet to your entrypoint file after the Route resource:
Filename: index.js
const record = new cloudflare . Record ( "hello-world-record" , {
exports . url = pulumi . interpolate `https:// ${ record . hostname } `
Filename: index.ts
const record = new cloudflare . Record ( "hello-world-record" , {
export const url = pulumi . interpolate `https:// ${ record . hostname } ` ;
Filename: __main__.py
record = cloudflare . Record ( "hello-world-record" ,
url = pulumi . Output . concat ( "https://" , record . hostname )
pulumi . export ( 'url' , url )
Filename: main.go
record , err := cloudflare . NewRecord ( ctx , "hello-world-record" , & cloudflare . RecordArgs {
Type : pulumi . String ( "A" ),
Content : pulumi . String ( "192.0.2.1" ),
ZoneId : pulumi . String ( zone . Id ),
Proxied : pulumi . Bool ( true ),
ctx . Export ( "url" , pulumi . Sprintf ( "https:// %s " , record . Hostname ))
Filename: src/main/java/myproject/App.java
var record = new Record ( "hello-world-record" , RecordArgs . builder ()
. zoneId ( zone . applyValue ( getZoneResult -> getZoneResult . id ()))
ctx . export ( "url" , Output . format ( "https://%s" , record . hostname ()));
Filename: Program.cs
Notice the updated ’ return ’ statement because you’re now exporting a value. Ensure that you also include using System.Collections.Generic;
in your imports.
var record = new Cloudflare . Record ( "hello-world-record" , new ()
ZoneId = zone . Apply ( z => z . Id ),
return new Dictionary < string , object ?>
[ "url" ] = Output . Format ( $"https:// { record . Hostname } " )
Notice the new top-level outputs
section.
url : "https://${record.hostname}"
d. (Optional) Verify your code
Confirm all your changes match the full solution below:
Filename: index.js
const pulumi = require ( "@pulumi/pulumi" ) ;
const cloudflare = require ( "@pulumi/cloudflare" ) ;
const config = new pulumi . Config () ;
const accountId = config . require ( "accountId" ) ;
const domain = config . require ( "domain" ) ;
const content = `export default {
const options = { headers: { 'content-type': 'text/plain' } };
return new Response("Hello World!", options);
const worker = new cloudflare . WorkersScript ( "hello-world-worker" , {
name : "hello-world-worker" ,
module : true , // ES6 module
const zone = cloudflare . getZone ( {
const zoneId = zone . then ( z => z . zoneId ) ;
const route = new cloudflare . WorkersRoute ( "hello-world-route" , {
pattern : "hello-world." + domain ,
const record = new cloudflare . Record ( "hello-world-record" , {
exports . url = pulumi . interpolate `https:// ${ record . hostname } `
Filename: index.ts
import * as pulumi from "@pulumi/pulumi" ;
import * as cloudflare from "@pulumi/cloudflare" ;
const config = new pulumi . Config () ;
const accountId = config . require ( "accountId" ) ;
const domain = config . require ( "domain" ) ;
const content = `export default {
const options = { headers: { 'content-type': 'text/plain' } };
return new Response("Hello World!", options);
const worker = new cloudflare . WorkersScript ( "hello-world-worker" , {
name : "hello-world-worker" ,
module : true , // ES6 module
const zone = cloudflare . getZone ( {
const zoneId = zone . then ( z => z . zoneId ) ;
const route = new cloudflare . WorkersRoute ( "hello-world-route" , {
pattern : "hello-world." + domain ,
const record = new cloudflare . Record ( "hello-world-record" , {
export const url = pulumi . interpolate `https:// ${ record . hostname } ` ;
Filename: __main__.py
import pulumi_cloudflare as cloudflare
ACCOUNT_ID = CONFIG . get ( "accountId" )
DOMAIN = CONFIG . require ( "domain" )
const options = { headers: { 'content-type': 'text/plain' } };
return new Response("Hello World!", options);
worker = cloudflare . WorkersScript ( "hello-world-worker" ,
name = "hello-world-worker" ,
zone = cloudflare . get_zone ( account_id = ACCOUNT_ID , name = DOMAIN )
route = cloudflare . WorkersRoute ( "hello-world-route" ,
pattern = "hello-world." + DOMAIN ,
record = cloudflare . Record ( "hello-world-record" ,
url = pulumi . Output . concat ( "https://" , record . hostname )
pulumi . export ( 'url' , url )
Filename: main.go
" github.com/pulumi/pulumi-cloudflare/sdk/v5/go/cloudflare "
" github.com/pulumi/pulumi/sdk/v3/go/pulumi "
" github.com/pulumi/pulumi/sdk/v3/go/pulumi/config "
pulumi . Run ( func ( ctx * pulumi . Context ) error {
conf := config . New ( ctx , "" )
accountID := conf . Get ( "accountId" )
domain := conf . Get ( "domain" )
const options = { headers: { 'content-type': 'text/plain' } };
return new Response("Hello World!", options);
worker , err := cloudflare . NewWorkersScript ( ctx , "hello-world-worker" , & cloudflare . WorkersScriptArgs {
AccountId : pulumi . String ( accountID ),
Name : pulumi . String ( "hello-world-worker" ),
Content : pulumi . String ( content ),
Module : pulumi . Bool ( true ), // ES6 module
zone , err := cloudflare . LookupZone ( ctx , & cloudflare . LookupZoneArgs {
route , err := cloudflare . NewWorkersRoute ( ctx , "hello-world-route" , & cloudflare . WorkersRouteArgs {
ZoneId : pulumi . String ( zone . Id ),
Pattern : pulumi . String ( "hello-world." + domain ),
record , err := cloudflare . NewRecord ( ctx , "hello-world-record" , & cloudflare . RecordArgs {
Type : pulumi . String ( "A" ),
Content : pulumi . String ( "192.0.2.1" ),
ZoneId : pulumi . String ( zone . Id ),
Proxied : pulumi . Bool ( true ),
ctx . Export ( "url" , pulumi . Sprintf ( "https:// %s " , record . Hostname ))
Filename: src/main/java/myproject/App.java
import com . pulumi . Pulumi ;
import com . pulumi . core . Output ;
import com . pulumi . cloudflare . WorkersScript ;
import com . pulumi . cloudflare . WorkersScriptArgs ;
import com . pulumi . cloudflare . CloudflareFunctions ;
import com . pulumi . cloudflare . inputs . GetZoneArgs ;
import com . pulumi . cloudflare . WorkersRoute ;
import com . pulumi . cloudflare . WorkersRouteArgs ;
import com . pulumi . cloudflare . Record ;
import com . pulumi . cloudflare . RecordArgs ;
public static void main ( String [] args ) {
const options = { headers: { 'content-type': 'text/plain' } };
return new Response("Hello World!", options);
var accountId = ctx . config (). require ( "accountId" );
var domain = ctx . config (). require ( "domain" );
var worker = new WorkersScript ( "hello-world-worker" , WorkersScriptArgs . builder ()
. name ( "hello-world-worker" )
final var zone = CloudflareFunctions . getZone ( GetZoneArgs . builder ()
var route = new WorkersRoute ( "hello-world-route" , WorkersRouteArgs . builder ()
. zoneId ( zone . applyValue ( getZoneResult -> getZoneResult . id ()))
. pattern ( "hello-world." + domain )
. scriptName ( worker . name ())
var record = new Record ( "hello-world-record" , RecordArgs . builder ()
. zoneId ( zone . applyValue ( getZoneResult -> getZoneResult . id ()))
ctx . export ( "url" , Output . format ( "https://%s" , record . hostname ()));
Filename: Program.cs
using System . Collections . Generic ;
using Cloudflare = Pulumi . Cloudflare ;
return await Deployment . RunAsync (() =>
var config = new Config ();
var accountId = config . Require ( "accountId" );
var domain = config . Require ( "domain" );
const options = { headers: { 'content-type': 'text/plain' } };
return new Response( "" Hello World! "" , options);
var worker = new Cloudflare . WorkersScript ( "hello-world-worker" , new ()
Name = "hello-world-worker" ,
var zone = Output . Create ( Cloudflare . GetZone . InvokeAsync ( new ()
var route = new Cloudflare . WorkersRoute ( "hello-world-route" , new ()
ZoneId = zone . Apply ( z => z . Id ),
Pattern = "hello-world." + domain ,
ScriptName = worker . Name ,
var record = new Cloudflare . Record ( "hello-world-record" , new ()
ZoneId = zone . Apply ( z => z . Id ),
return new Dictionary < string , object ?>
[ "url" ] = Output . Format ( $"https:// { record . Hostname } " )
Filename: Pulumi.yaml
name : serverless-cloudflare
function : cloudflare:getZone
type : cloudflare:WorkersScript
accountId : "${accountId}"
name : "hello-world-worker"
const options = { headers: { 'content-type': 'text/plain' } };
return new Response("Hello World!", options);
type : cloudflare:WorkersRoute
pattern : "hello-world.${domain}"
scriptName : ${worker.name}
url : "https://${record.hostname}"
3. Deploy your application
Now that you have defined all the Cloudflare resources, you can deploy the Hello World application to your Cloudflare account using the Pulumi CLI.
To deploy the changes, run:
wait for the dev stack to become ready
4. Test the Worker
You incrementally added Cloudflare resources to run and access your Hello World application. You can test your application by curling the url
output from the Pulumi stack.
curl $( pulumi stack output url )
5. Clean up
In this last step, you will clean up the resources and stack used throughout the tutorial.
a. Delete the Cloudflare resources
b. Remove the Pulumi stack
Next steps
Visit the Cloudflare package documentation ↗ to explore other resources you can define with Pulumi and Cloudflare.