VPC Services は、Cloudflare Terraform プロバイダー の cloudflare_connectivity_directory_service ↗ リソースとして、インフラストラクチャとして管理できます。
これは connectivity directory に直接対応します。ダッシュボードと Wrangler CLI も、同じ API で VPC Services を作成・管理します。サービスの作り方に関係なく、同じ VPC Service の設定フィールド(type、host、ports、tunnel ID)が適用されます。
cloudflare_connectivity_directory_service リソースは、connectivity directory に VPC Service を作成します。各リソースは、Worker がバインドできる VPC Service エントリ 1 件に対応します。
ホスト名を使う場合は、resolver_network ブロック付きで host.hostname を指定します。ホスト名ベースの JSON 設定例 と同じ考え方です。
resource "cloudflare_connectivity_directory_service" "my_private_api" {
account_id = var.account_id
name = "my-private-api"
type = "http"
http_port = 80
https_port = 443
host = {
hostname = "internal-api.example.com"
resolver_network = {
tunnel_id = var.tunnel_id
}
}
}プライベートネットワーク内のカスタム DNS リゾルバーを使うには、resolver_ips を追加します。
resource "cloudflare_connectivity_directory_service" "my_private_api" {
account_id = var.account_id
name = "my-private-api"
type = "http"
host = {
hostname = "internal-api.example.com"
resolver_network = {
tunnel_id = var.tunnel_id
resolver_ips = ["10.0.0.53"]
}
}
}IP アドレスを使う場合は、network ブロック付きで host.ipv4 または host.ipv6(あるいは両方)を指定します。IP ベースの JSON 設定例 と同じ考え方です。
resource "cloudflare_connectivity_directory_service" "my_private_api" {
account_id = var.account_id
name = "my-private-api"
type = "http"
http_port = 8080
https_port = 8443
host = {
ipv4 = "10.0.1.50"
ipv6 = "fe80::1"
network = {
tunnel_id = var.tunnel_id
}
}
}TCP サービス(例: データベース)では type = "tcp" を設定し、tcp_port を指定します。任意で app_protocol に postgresql または mysql を指定できます。
resource "cloudflare_connectivity_directory_service" "my_database" {
account_id = var.account_id
name = "my-postgres-db"
type = "tcp"
tcp_port = 5432
app_protocol = "postgresql"
host = {
ipv4 = "10.0.0.5"
network = {
tunnel_id = var.tunnel_id
}
}
}オリジンへの接続の TLS 証明書検証モードを設定するには、tls_settings ブロックを追加します。
resource "cloudflare_connectivity_directory_service" "my_database" {
account_id = var.account_id
name = "my-postgres-db"
type = "tcp"
tcp_port = 5432
app_protocol = "postgresql"
host = {
ipv4 = "10.0.0.5"
network = {
tunnel_id = var.tunnel_id
}
}
tls_settings = {
cert_verification_mode = "verify_ca"
}
}cert_verification_mode の有効な値は次のとおりです。
verify_full(デフォルト)verify_cadisabled
詳細は TLS 証明書の検証モード を参照してください。
HTTP サービスではポートは任意で、デフォルトは 80(HTTP)と 443(HTTPS)です。一方のスキームだけを強制するには、http_port または https_port のどちらかだけを指定します。スキームの強制とポートの挙動は VPC Service の設定 を参照してください。
TCP サービスでは tcp_port が必須です。
VPC Service を作成したら、cloudflare_worker_version リソースの bindings 配列で、バインディングタイプ vpc_service を使って Worker にバインドします。Wrangler 設定の vpc_services 配列 と同じです。
resource "cloudflare_worker_version" "my_worker_version" {
account_id = var.account_id
worker_id = cloudflare_worker.my_worker.id
compatibility_date = "2025-02-21" # Set this to today's date
main_module = "worker.js"
modules = [{
name = "worker.js"
content_type = "application/javascript+module"
content_file = "build/worker.js"
}]
bindings = [{
type = "vpc_service"
name = "PRIVATE_API"
service_id = cloudflare_connectivity_directory_service.my_private_api.service_id
}]
}同じ Worker に複数の VPC Service バインディングを追加できます。
bindings = [
{
type = "vpc_service"
name = "PRIVATE_API"
service_id = cloudflare_connectivity_directory_service.api.service_id
},
{
type = "vpc_service"
name = "PRIVATE_DATABASE"
service_id = cloudflare_connectivity_directory_service.database.service_id
}
]Worker のコードは、Workers Binding API のとおり、env.PRIVATE_API.fetch() と env.PRIVATE_DATABASE.fetch() で各バインディングにアクセスします。
Terraform での Workers とバインディングの管理について、詳細は Workers の Infrastructure as Code を参照してください。
Terraform プロバイダーには、ライフサイクルを管理せずに既存の VPC Services を読むデータソースがあります。
data "cloudflare_connectivity_directory_service" "existing" {
account_id = var.account_id
service_id = "182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e"
}Terraform 設定の外で管理している VPC Service(ダッシュボードや Wrangler CLI で作成したものなど)にバインドするときに便利です。
data "cloudflare_connectivity_directory_services" "all_http" {
account_id = var.account_id
type = "http"
}
data "cloudflare_connectivity_directory_services" "all_tcp" {
account_id = var.account_id
type = "tcp"
}resource "cloudflare_connectivity_directory_service" "example" {
# Required
account_id = "your-account-id" # Account identifier
name = "my-private-api" # Human-readable name
type = "http" # Service type: "http" or "tcp"
# HTTP-specific (optional, defaults to 80/443)
http_port = 80 # HTTP port
https_port = 443 # HTTPS port
# TCP-specific (tcp_port is required when type = "tcp")
# tcp_port = 5432 # TCP port
# app_protocol = "postgresql" # Optional: "postgresql" or "mysql"
host = {
# Use hostname OR ipv4/ipv6, not both
# Option A: Hostname-based
hostname = "internal-api.example.com"
resolver_network = {
tunnel_id = "tunnel-uuid" # Required — Cloudflare Tunnel ID
resolver_ips = ["10.0.0.53"] # Optional — custom DNS resolver IPs
}
# Option B: IP-based
# ipv4 = "10.0.1.50" # IPv4 address
# ipv6 = "fe80::1" # IPv6 address
# network = {
# tunnel_id = "tunnel-uuid" # Required — Cloudflare Tunnel ID
# }
}
# Optional TLS settings
# tls_settings = {
# cert_verification_mode = "verify_full" # "verify_full", "verify_ca", or "disabled"
# }
# Read-only (computed by the API)
# id — Terraform resource ID
# service_id — VPC Service ID (use this for Worker bindings)
# created_at — Creation timestamp
# updated_at — Last update timestamp
}完全なスキーマは Terraform registry のドキュメント ↗ を参照してください。