Shirubasoft.Aspire.Cloudflared
1.1.1
Shirubasoft.Aspire.CloudflareTunnels
Additional DetailsThis package has been replaced by Shirubasoft.Aspire.CloudflareTunnels
dotnet add package Shirubasoft.Aspire.Cloudflared --version 1.1.1
NuGet\Install-Package Shirubasoft.Aspire.Cloudflared -Version 1.1.1
<PackageReference Include="Shirubasoft.Aspire.Cloudflared" Version="1.1.1" />
<PackageVersion Include="Shirubasoft.Aspire.Cloudflared" Version="1.1.1" />
<PackageReference Include="Shirubasoft.Aspire.Cloudflared" />
paket add Shirubasoft.Aspire.Cloudflared --version 1.1.1
#r "nuget: Shirubasoft.Aspire.Cloudflared, 1.1.1"
#:package Shirubasoft.Aspire.Cloudflared@1.1.1
#addin nuget:?package=Shirubasoft.Aspire.Cloudflared&version=1.1.1
#tool nuget:?package=Shirubasoft.Aspire.Cloudflared&version=1.1.1
Aspire.Cloudflared
An Aspire integration for Cloudflare Tunnels, enabling you to expose local services to the internet through Cloudflare's network during development and deployment.
Features
- Automatic Tunnel Creation: Tunnels are automatically created via the Cloudflare API during local development
- DNS Record Management: DNS CNAME records are automatically created for your hostnames
- Ingress Configuration: Tunnel routes are automatically configured to point to your services
- Aspire Dashboard Integration: Monitor tunnel status directly in the Aspire dashboard
- Aspire Pipeline Integration: Configure deployment-specific routes as a standard
aspire deploypipeline step
Installation
Install the NuGet package in your AppHost project:
dotnet add package Shirubasoft.Aspire.Cloudflared
Or add it directly to your AppHost .csproj file:
<ItemGroup>
<PackageReference Include="Shirubasoft.Aspire.Cloudflared" Version="*" />
</ItemGroup>
Quick Start
var builder = DistributedApplication.CreateBuilder(args);
// Add your service (e.g., a simple nginx container)
var webApp = builder.AddContainer("web-app", "docker.io/library/nginx", "alpine")
.WithHttpEndpoint(targetPort: 80, name: "http");
// Create a Cloudflare tunnel
var tunnel = builder.AddCloudflareTunnel("my-tunnel");
// Expose your service through the tunnel
webApp.WithCloudflareTunnel(tunnel, hostname: "myapp.example.com");
builder.Build().Run();
When you run the application, Aspire will prompt you for:
- Account ID: Your Cloudflare account ID
- API Token: A Cloudflare API token with the required permissions
Getting Your Cloudflare Account ID
- Log in to the Cloudflare Dashboard
- Select any domain in your account
- On the Overview page, scroll down to find Account ID in the right sidebar
- Copy the Account ID value
Creating a Cloudflare API Token
The integration requires a Cloudflare API token with specific permissions to create tunnels and DNS records.
Step-by-Step Guide
- Go to the Cloudflare Dashboard
- Navigate to My Profile → API Tokens (or for account tokens: Manage Account → API Tokens)
- Click Create Token
- Select Create Custom Token
- Configure the token with the following settings:
Required Permissions
| Permission | Access Level | Scope | Description |
|---|---|---|---|
| Account → Cloudflare Tunnel | Edit | Your Account | Create, read, and manage tunnels |
| Zone → Zone | Read | All zones (or specific zones) | Look up zone IDs for DNS records |
| Zone → DNS | Edit | All zones (or specific zones) | Create CNAME records for tunnel routes |
Token Configuration Screenshot Reference
Your custom token configuration should look like this:
Token name: Aspire Cloudflared Development
Permissions:
├── Account
│ └── Cloudflare Tunnel: Edit
└── Zone
├── Zone: Read
└── DNS: Edit
Account Resources: Include → Your Account
Zone Resources: Include → All zones (or specific zones you'll use)
- Click Continue to summary
- Review the permissions and click Create Token
- Copy the token immediately — it will only be shown once!
Verifying Your Token
You can verify your token works by running:
curl "https://api.cloudflare.com/client/v4/user/tokens/verify" \
--header "Authorization: Bearer YOUR_API_TOKEN"
A successful response will show:
{
"success": true,
"result": {
"status": "active"
}
}
API Reference
AddCloudflareTunnel
Creates a Cloudflare tunnel resource that can be used to expose services.
IResourceBuilder<CloudflareTunnelResource> AddCloudflareTunnel(
this IDistributedApplicationBuilder builder,
string name,
int? metricsPort = null)
Parameters:
name: The name of the tunnel (used in Cloudflare and as the resource name in Aspire)metricsPort: Optional port for the cloudflared metrics endpoint
Required Parameters (prompted at runtime):
{name}-account-id: Your Cloudflare account ID{name}-api-token: Your Cloudflare API token with tunnel permissions
WithCloudflareTunnel
Exposes a resource's endpoint through a Cloudflare tunnel.
IResourceBuilder<T> WithCloudflareTunnel<T>(
this IResourceBuilder<T> builder,
IResourceBuilder<CloudflareTunnelResource> tunnel,
string hostname,
string endpointName = "http")
where T : IResourceWithEndpoints
Parameters:
tunnel: The Cloudflare tunnel resource to route throughhostname: The public hostname for this route (e.g.,"api.example.com")endpointName: The name of the endpoint to expose (defaults to"http")
Complete Example
var builder = DistributedApplication.CreateBuilder(args);
// Create a Cloudflare tunnel
var tunnel = builder.AddCloudflareTunnel("my-cloudflare-tunnel");
// Example 1: Expose an nginx container
var nginx = builder.AddContainer("hello-world", "docker.io/library/nginx", "alpine")
.WithHttpEndpoint(targetPort: 80, name: "http");
nginx.WithCloudflareTunnel(tunnel, hostname: "hello.example.com");
// Example 2: Expose a .NET project
var api = builder.AddProject<Projects.MyApi>("api")
.WithHttpEndpoint(port: 5000, name: "http");
api.WithCloudflareTunnel(tunnel, hostname: "api.example.com");
// Example 3: Expose multiple services through the same tunnel
var frontend = builder.AddContainer("frontend", "my-frontend", "latest")
.WithHttpEndpoint(targetPort: 3000, name: "http");
frontend.WithCloudflareTunnel(tunnel, hostname: "app.example.com");
builder.Build().Run();
Deployment Considerations
Production Deployment
During local development (run mode), the integration automatically:
- Creates the tunnel if it doesn't exist
- Retrieves the tunnel token
- Configures DNS records and ingress routes
For deployment, pre-provision the tunnel and provide these Aspire parameters:
| Parameter | Purpose |
|---|---|
{tunnel-name}-account-id |
Finds the existing tunnel and its DNS zones |
{tunnel-name}-api-token |
Configures Cloudflare DNS and ingress routes |
{tunnel-name}-tunnel-token |
Authenticates the deployed cloudflared connector |
Create the tunnel if it does not already exist:
cloudflared tunnel create my-tunnel-nameGet the tunnel token:
cloudflared tunnel token my-tunnel-nameOr retrieve it from the Cloudflare Zero Trust Dashboard under Networks → Tunnels.
Supply the parameters through your pipeline's secret store. Aspire maps dashes in parameter names to underscores in environment-variable names:
export Parameters__my_cloudflare_tunnel_account_id="..." export Parameters__my_cloudflare_tunnel_api_token="..." export Parameters__my_cloudflare_tunnel_tunnel_token="..." aspire deploy --non-interactive
aspire deploy contributes a configure-{tunnel-name}-cloudflare-routes step to the AppHost deployment pipeline. The step:
- runs after Aspire publishes the deployment artifacts
- uses the selected compute environment to resolve each service's deployment URL
- finds the pre-provisioned Cloudflare tunnel
- creates the DNS records and updates the tunnel ingress configuration
- is required by Aspire's standard
deploystep
You can inspect or run the step directly:
aspire deploy --list-steps
aspire do configure-my-cloudflare-tunnel-cloudflare-routes --non-interactive
aspire publish remains side-effect free. It emits the cloudflared resource and tunnel-token parameter, but it does not call the Cloudflare API.
Why Manual Token for Deployment?
Automatic tunnel creation is intentionally limited to local run mode. In a deployment pipeline:
- The Cloudflare API token stays in the pipeline environment and is not passed to the deployed cloudflared container
- The deployed container receives only the tunnel-scoped token
- Existing infrastructure remains under explicit lifecycle control
- This follows the principle of least privilege
How It Works
Run Mode (Local Development)
- Tunnel Installer creates/finds the tunnel via Cloudflare API
- Tunnel Token is retrieved and passed to the cloudflared container
- Route Installer creates DNS CNAME records pointing to
{tunnel-id}.cfargotunnel.com - Ingress Configuration is updated with routes from hostname to your services
- Cloudflared Container starts and connects to Cloudflare's network
Publish and Deploy Mode
- Publish emits the cloudflared container and a secret tunnel-token parameter without external side effects
- Deploy Pipeline resolves target URLs from Aspire's selected compute environment
- Cloudflare Pipeline Step configures DNS and ingress for the existing tunnel
- Cloudflared Container starts with the provided tunnel token
Troubleshooting
"Zone Not Found" Error
This means the API token doesn't have access to the domain's zone. Ensure:
- The domain is registered in your Cloudflare account
- The API token has Zone: Read permission for that zone
- The zone is active (not pending)
"Tunnel Token Not Available" Error
The tunnel installer hasn't completed yet. This usually resolves automatically. If it persists:
- Check the Aspire dashboard for installer status
- Verify your API token has Cloudflare Tunnel: Edit permission
- Check the logs for API errors
DNS Record Already Exists
This is normal if you've run the application before. The integration will skip creating duplicate records.
Tunnel Not Connecting
- Verify your API token is valid and not expired
- Check that the tunnel token is correct
- Retrieve the token again if the tunnel was recreated or its credentials were rotated
- Ensure no firewall is blocking outbound connections to Cloudflare
Integration Tests
The live integration test uses the sample AppHost to exercise both aspire deploy and local run mode:
export CLOUDFLARE_ACCOUNT_ID="..."
export CLOUDFLARE_API_TOKEN="..."
export CLOUDFLARE_TUNNEL_TOKEN="..."
./scripts/run-integration-tests.sh
The script deploys the Docker Compose environment, verifies the public nginx route, destroys the test deployment, starts the local AppHost, and verifies the route again. GitHub Actions calls the same script with repository secrets.
Security Best Practices
- Never commit API tokens to source control
- Use User Secrets for local development:
dotnet user-secrets set "Parameters:my-tunnel-api-token" "your-token-here" dotnet user-secrets set "Parameters:my-tunnel-account-id" "your-account-id" - Rotate tokens if they may have been exposed
- Limit token scope to only the zones you need
- Use account-owned tokens for shared development environments
License
See LICENSE for details.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- Aspire.Hosting (>= 13.4.6)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.