The best way to control Claude Code, Codex and more from any computer or phone

main
mdx 94 lines 2.64 KB
Raw
1 ---
2 title: VPC & private networking
3 description: Give Cloud Run services Direct VPC egress to reach private IPs like Memorystore, and create your own VPC networks and subnets.
4 ---
5
6 Most Google APIs, like Firestore, Pub/Sub, and Cloud Storage, are
7 reached over their public endpoints, so a Cloud Run service needs no
8 network setup. Some resources only have a private IP: Memorystore,
9 AlloyDB, Filestore, and VMs without a public address. To reach those,
10 give the service an interface on your VPC.
11
12 ## Direct VPC egress
13
14 Add `vpcAccess` to the service's template:
15
16 ```typescript
17 export default class Api extends GCP.Function<Api>()(
18 "Api",
19 {
20 main: import.meta.url,
21 template: {
22 vpcAccess: {
23 egress: "PRIVATE_RANGES_ONLY",
24 networkInterfaces: [{ network: "default", subnetwork: "default" }],
25 },
26 },
27 },
28 Effect.gen(function* () {
29 const redis = yield* GCP.Redis.ReadWriteRedis(Cache);
30 return {
31 fetch: redis.get("greeting").pipe(
32 Effect.map((value) => HttpServerResponse.text(value ?? "hello")),
33 Effect.orDie,
34 ),
35 };
36 }).pipe(Effect.provide(GCP.Redis.ReadWriteRedisHttp)),
37 ) {}
38 ```
39
40 `PRIVATE_RANGES_ONLY` sends traffic for private addresses through the
41 VPC. Everything else, including Google APIs, leaves the normal way.
42 Use `ALL_TRAFFIC` to send everything through the VPC, for example to
43 leave through a fixed NAT address.
44
45 ## Create your own network
46
47 The `default` network works for most projects. Create a dedicated one
48 when you need your own address ranges:
49
50 ```typescript
51 // src/network.ts
52 import * as GCP from "@alchemy.run/gcp";
53 import * as Effect from "effect/Effect";
54
55 export const Network = GCP.Compute.Network("Vpc", {});
56
57 export const Subnet = Effect.gen(function* () {
58 const network = yield* Network;
59 return yield* GCP.Compute.Subnetwork("Private", {
60 network: network.networkName,
61 ipCidrRange: "10.0.0.0/24",
62 privateIpGoogleAccess: true,
63 });
64 });
65 ```
66
67 New networks are in custom mode: you choose every subnet. Point the
68 service at them:
69
70 ```typescript
71 networkInterfaces: [
72 { network: network.networkName, subnetwork: subnet.subnetworkName },
73 ],
74 ```
75
76 ## Firewall rules
77
78 Allow traffic between your own ranges with a firewall rule:
79
80 ```typescript
81 yield* GCP.Compute.Firewall("AllowInternal", {
82 network: network.networkName,
83 sourceRanges: ["10.0.0.0/8"],
84 allowed: [{ protocol: "tcp" }],
85 });
86 ```
87
88 ## Where next
89
90 - [Memorystore](/gcp/data/memorystore) — Redis on a private IP.
91 - [Private services](/gcp/networking/private-services) — services that
92 only other services can call.
93 - [Compute reference](/providers/gcp/compute#network) — networks,
94 subnets, firewalls, and load balancers.