You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The [VPC Service configurations](/workers-vpc/configuration/vpc-services/#vpc-service-configuration) will always be used to connect and route requests to your services in external networks, even if a different URL or host is present in the actual `fetch()` operation of the Worker code.
34
+
35
+
The information provided in the `fetch()` operation is not used to route requests, and instead only populates the `Host` field for a HTTP request that can be parsed by the server and used for Server Name Indication (SNI).
36
+
:::
37
+
32
38
### Parameters
33
39
34
40
-`resource` (string | URL | Request) - The URL to fetch. This must be an absolute URL including protocol, host, and path (for example, `http://internal-api:8080/api/users`)
Copy file name to clipboardExpand all lines: src/content/docs/workers-vpc/configuration/tunnel/index.mdx
+4Lines changed: 4 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -59,6 +59,10 @@ For platform-specific tunnel deployment instructions for production workloads:
59
59
60
60
Refer to the full Cloudflare Tunnel documentation on [how to setup Tunnels for high availability and failover with replicas](/cloudflare-one/connections/connect-networks/configure-tunnels/tunnel-availability/).
61
61
62
+
:::note
63
+
We do not recommend using `cloudflared` in autoscaling setups because downscaling (removing replicas) will break existing user connections to that replica. Additionally, `cloudflared` does not load balance across replicas; replicas are strictly for high availability and requests are routed to the nearest replica.
64
+
:::
65
+
62
66
## Next steps
63
67
64
68
- Configure [VPC Services](/workers-vpc/configuration/vpc-services/) to connect your tunnels to Workers
Copy file name to clipboardExpand all lines: src/content/docs/workers-vpc/configuration/vpc-services.mdx
+8-2Lines changed: 8 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -35,6 +35,12 @@ A VPC Service consists of:
35
35
-**Hostname or IPv4/IPv6 addresses**: The hostname, or IPv4 and/or IPv6 addresses to use to route to your service from the tunnel in your private network
36
36
-**Ports**: HTTP and/or HTTPS port configuration (optional, defaults to 80/443)
37
37
38
+
:::note
39
+
The [VPC Service configurations](/workers-vpc/configuration/vpc-services/#vpc-service-configuration) will always be used to connect and route requests to your services in external networks, even if a different URL or host is present in the actual `fetch()` operation of the Worker code.
40
+
41
+
The information provided in the `fetch()` operation is not used to route requests, and instead only populates the `Host` field for a HTTP request that can be parsed by the server and used for Server Name Indication (SNI).
42
+
:::
43
+
38
44
## Configuration example
39
45
40
46
The following is an example of a VPC Service for a service using custom HTTP and HTTPS ports, and both IPv4 and IPv6 addresses. These configurations represent the expected contract of the [REST API for creating a VPC Service](https://developers.cloudflare.com/api/resources/zero_trust/subresources/connectivity/subresources/directory/subresources/services/), a type of service within the broader connectivity directory.
Copy file name to clipboardExpand all lines: src/content/docs/workers-vpc/examples/private-api.mdx
+40-71Lines changed: 40 additions & 71 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,5 @@
1
1
---
2
-
title: Access private API
2
+
title: Access a private API or website
3
3
pcx_content_type: example
4
4
---
5
5
@@ -9,13 +9,29 @@ This example demonstrates how to access a private REST API that is not exposed t
9
9
10
10
## Prerequisites
11
11
12
-
- A private API running in your VPC/virtual network
13
-
-Cloudflare Tunnel configured and running (follow the [Get Started guide](/workers-vpc/get-started/)to set up)
12
+
- A virtual machine/EC2 instance running in your VPC/virtual network
13
+
-A private API or website running in your VPC/virtual network with security rules allowing access to the virtual machine that will be running `cloudflared`
14
14
- Workers account with Workers VPC access
15
15
16
-
## 1. Create the VPC Service
16
+
## 1. Set up Cloudflare Tunnel
17
17
18
-
First, create a service for your internal API:
18
+
A Cloudflare Tunnel creates a secure connection from your private network to Cloudflare. This tunnel will allow Workers to securely access your private resources.
19
+
20
+
1. Navigate to the [Workers VPC dashboard](https://dash.cloudflare.com/?to=/:account/workers/vpc/tunnels) and select the **Tunnels** tab.
21
+
22
+
2. Select **Create** to create a new tunnel.
23
+
24
+
3. Enter a name for your tunnel (for example, `private-api-tunnel`) and select **Save tunnel**.
25
+
26
+
4. Choose your operating system and architecture. The dashboard will provide specific installation instructions for your environment.
27
+
28
+
5. Follow the provided commands to download and install `cloudflared` on your VM, and execute the service installation command with your unique token.
29
+
30
+
The dashboard will confirm when your tunnel is successfully connected. Note the tunnel ID for the next step.
31
+
32
+
## 2. Create the Workers VPC Service
33
+
34
+
First, create a Workers VPC Service for your internal API:
// Use the response of the private API to perform more logic in Workers, before returning the final response
84
+
return response;
93
85
} catch (error) {
94
-
// Log error for debugging
95
-
console.error("Failed to reach internal API:", error);
96
-
97
-
// Return user-friendly error
98
-
return new Response(
99
-
JSON.stringify({
100
-
error: "Service temporarily unavailable",
101
-
timestamp: new Date().toISOString(),
102
-
}),
103
-
{
104
-
status: 503,
105
-
headers: {
106
-
"Content-Type": "application/json",
107
-
"Retry-After": "30",
108
-
},
109
-
},
110
-
);
86
+
return new Response("Service unavailable", { status: 503 });
111
87
}
112
88
},
113
89
};
114
90
````
115
91
116
92
This guide demonstrates how you could create a simple proxy in your Workers. However, you could use VPC Services to fetch APIs directly and manipulate the responses to enable you to build more full-stack and backend functionality on Workers.
117
93
118
-
## 4. Deploy and test
94
+
## 5. Deploy and test
119
95
120
96
Now, you can deploy and test your Worker that you have created:
This example demonstrates how to access a private S3 bucket that is not exposed to the public internet. In this guide, we will configure a Workers VPC Service for an internal S3-compatible storage service, create a Worker that makes requests to that bucket, and deploy the Worker to validate our changes.
9
+
10
+
## Prerequisites
11
+
12
+
- A private S3-compatible storage service running in your VPC/virtual network (such as AWS S3 VPC endpoint, MinIO, or similar)
13
+
- A virtual machine/EC2 instance running in the same VPC as your S3 VPC endpoint
14
+
- Workers account with Workers VPC access
15
+
16
+
## 1. Set up Cloudflare Tunnel
17
+
18
+
A Cloudflare Tunnel creates a secure connection from your private network to Cloudflare. This tunnel will allow Workers to securely access your private resources.
19
+
20
+
1. Navigate to the [Workers VPC dashboard](https://dash.cloudflare.com/?to=/:account/workers/vpc/tunnels) and select the **Tunnels** tab.
21
+
22
+
2. Select **Create** to create a new tunnel.
23
+
24
+
3. Enter a name for your tunnel (for example, `s3-tunnel`) and select **Save tunnel**.
25
+
26
+
4. Choose your operating system and architecture. The dashboard will provide specific installation instructions for your environment.
27
+
28
+
5. Follow the provided commands to download and install `cloudflared` on your VM, and execute the service installation command with your unique token.
29
+
30
+
The dashboard will confirm when your tunnel is successfully connected. Note the tunnel ID for the next step.
31
+
32
+
## 2. Create the Workers VPC Service
33
+
34
+
First, create a Workers VPC Service for your internal S3 storage:
35
+
36
+
```bash
37
+
npx wrangler vpc service create s3-storage \
38
+
--type http \
39
+
--tunnel-id <YOUR_TUNNEL_ID> \
40
+
--hostname s3.us-west-2.amazonaws.com
41
+
```
42
+
43
+
You can also create a Workers VPC Service using an IP address (for example, if using MinIO):
44
+
45
+
```bash
46
+
npx wrangler vpc service create s3-storage \
47
+
--type http \
48
+
--tunnel-id <YOUR_TUNNEL_ID> \
49
+
--ipv4 10.0.1.60 \
50
+
--http-port 9000
51
+
```
52
+
53
+
Note the service ID returned for the next step.
54
+
55
+
## 3. Configure S3 bucket policy
56
+
57
+
Configure your S3 bucket to allow anonymous access from your VPC endpoint. This works for unencrypted S3 objects:
58
+
59
+
```json
60
+
{
61
+
"Version": "2012-10-17",
62
+
"Statement": [
63
+
{
64
+
"Sid": "AllowAnonymousAccessFromVPCE",
65
+
"Effect": "Allow",
66
+
"Principal": "*",
67
+
"Action": ["s3:GetObject", "s3:ListBucket"],
68
+
"Resource": [
69
+
"arn:aws:s3:::your-bucket-name",
70
+
"arn:aws:s3:::your-bucket-name/*"
71
+
],
72
+
"Condition": {
73
+
"StringEquals": {
74
+
"aws:sourceVpce": "vpce-your-endpoint-id"
75
+
}
76
+
}
77
+
}
78
+
]
79
+
}
80
+
```
81
+
82
+
### Testing S3 access directly
83
+
84
+
You can test S3 access directly from the VM where your Cloudflare Tunnel is running to verify the bucket policy is working correctly. These commands should work without any AWS credentials:
// Use the response from S3 to perform more logic in Workers, before returning the final response
123
+
return response;
124
+
} catch (error) {
125
+
return new Response("Storage unavailable", { status: 503 });
126
+
}
127
+
},
128
+
};
129
+
````
130
+
131
+
This guide demonstrates how you could access private object storage from your Workers. You could use Workers VPC Services to fetch files directly and manipulate the responses to enable you to build more full-stack and backend functionality on Workers.
132
+
133
+
## 6. Deploy and test
134
+
135
+
Now, you can deploy and test your Worker that you have created:
136
+
137
+
```bash
138
+
npx wrangler deploy
139
+
```
140
+
141
+
```bash
142
+
# Test GET request
143
+
curl https://private-s3-gateway.workers.dev
144
+
```
145
+
146
+
## Next steps
147
+
148
+
- Add [authentication and authorization](/workers/examples/auth-with-headers/)
0 commit comments