Skip to content

Commit 33d8c33

Browse files
committed
updated tunnels example
1 parent 65f0341 commit 33d8c33

2 files changed

Lines changed: 12 additions & 11 deletions

File tree

EXAMPLES.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,13 @@ uv run pytest -m smoketest tests/smoketests/examples/
109109
<a id="devbox-tunnel"></a>
110110
## Devbox Tunnel (HTTP Server Access)
111111

112-
**Use case:** Create a devbox, start an HTTP server, enable a tunnel, and access the server from the local machine through the tunnel. Uses the async SDK.
112+
**Use case:** Create a devbox with a tunnel, start an HTTP server, and access the server from the local machine through the tunnel. Uses the async SDK.
113113

114114
**Tags:** `devbox`, `tunnel`, `networking`, `http`, `async`
115115

116116
### Workflow
117-
- Create a devbox
117+
- Create a devbox with tunnel configuration
118118
- Start an HTTP server inside the devbox
119-
- Enable a tunnel for external access
120119
- Make an HTTP request from the local machine through the tunnel
121120
- Validate the response
122121
- Shutdown the devbox

examples/devbox_tunnel.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
---
44
title: Devbox Tunnel (HTTP Server Access)
55
slug: devbox-tunnel
6-
use_case: Create a devbox, start an HTTP server, enable a tunnel, and access the server from the local machine through the tunnel. Uses the async SDK.
6+
use_case: Create a devbox with a tunnel, start an HTTP server, and access the server from the local machine through the tunnel. Uses the async SDK.
77
workflow:
8-
- Create a devbox
8+
- Create a devbox with tunnel configuration
99
- Start an HTTP server inside the devbox
10-
- Enable a tunnel for external access
1110
- Make an HTTP request from the local machine through the tunnel
1211
- Validate the response
1312
- Shutdown the devbox
@@ -40,7 +39,7 @@
4039

4140

4241
async def recipe(ctx: RecipeContext) -> RecipeOutput:
43-
"""Create a devbox, start an HTTP server, enable a tunnel, and access it from the local machine."""
42+
"""Create a devbox with a tunnel, start an HTTP server, and access it from the local machine."""
4443
cleanup = ctx.cleanup
4544

4645
sdk = AsyncRunloopSDK()
@@ -50,6 +49,7 @@ async def recipe(ctx: RecipeContext) -> RecipeOutput:
5049
launch_parameters={
5150
"resource_size_request": "X_SMALL",
5251
},
52+
tunnel={"auth_mode": "open"},
5353
)
5454
cleanup.add(f"devbox:{devbox.id}", devbox.shutdown)
5555

@@ -60,15 +60,17 @@ async def recipe(ctx: RecipeContext) -> RecipeOutput:
6060
# Give the server a moment to start
6161
await asyncio.sleep(SERVER_STARTUP_DELAY_S)
6262

63-
# Enable a tunnel to expose the HTTP server
64-
# For authenticated tunnels, use auth_mode="authenticated" and include the auth_token
63+
# The tunnel was created with the devbox. For authenticated tunnels, set
64+
# tunnel={"auth_mode": "authenticated"} on create and include the auth_token
6565
# in your requests via the Authorization header: `Authorization: Bearer {tunnel.auth_token}`
66-
tunnel = await devbox.net.enable_tunnel(auth_mode="open")
66+
tunnel = await devbox.get_tunnel()
67+
if tunnel is None:
68+
raise RuntimeError("Failed to create tunnel at devbox launch time")
6769

6870
# Get the tunnel URL for the server port
6971
tunnel_url = await devbox.get_tunnel_url(HTTP_SERVER_PORT)
7072
if tunnel_url is None:
71-
raise RuntimeError("Failed to get tunnel URL after enabling tunnel")
73+
raise RuntimeError("Failed to get tunnel URL after creating tunnel")
7274

7375
# Make an HTTP request from the LOCAL MACHINE through the tunnel to the devbox
7476
# This demonstrates that the tunnel allows external access to the devbox service

0 commit comments

Comments
 (0)