diff --git a/CHANGELOG.md b/CHANGELOG.md index e69de29bb2d..1ea94ce4741 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -0,0 +1 @@ +- Set region for VPC connector to match function region if not specified. (#10469) \ No newline at end of file diff --git a/src/deploy/functions/build.ts b/src/deploy/functions/build.ts index df2d3950d6b..efbde154987 100644 --- a/src/deploy/functions/build.ts +++ b/src/deploy/functions/build.ts @@ -257,8 +257,8 @@ export type Endpoint = Triggered & { // Defaults to the compute service account when a function is first created as a GCF gen 2 function. serviceAccount?: ServiceAccount | Expression | null; - // defaults to ["us-central1"], overridable in firebase-tools with - // process.env.FIREBASE_FUNCTIONS_DEFAULT_REGION + // Defaults to REGION_TBD. The deployment region is resolved dynamically at deploy-time + // based on event trigger sources or matching existing functions, falling back to "us-central1". region?: ListField; // The Cloud project associated with this endpoint. diff --git a/src/deploy/functions/prepare.spec.ts b/src/deploy/functions/prepare.spec.ts index d99f1648f36..7b190fa2d03 100644 --- a/src/deploy/functions/prepare.spec.ts +++ b/src/deploy/functions/prepare.spec.ts @@ -472,6 +472,33 @@ describe("prepare", () => { expect(want.endpoints["us-central1"]?.["id"].region).to.equal("us-central1"); expect(want.endpoints[build.REGION_TBD]).to.not.exist; }); + + it("updates VPC connector region if it contains REGION_TBD placeholder when default region is resolved", async () => { + const wantE: backend.Endpoint = { + ...ENDPOINT_BASE, + id: "onArchive", + region: build.REGION_TBD, + eventTrigger: { + eventType: "google.cloud.storage.object.v1.archived", + eventFilters: { bucket: "my-bucket" }, + retry: false, + }, + vpc: { + connector: `projects/project/locations/${build.REGION_TBD}/connectors/my-connector`, + }, + }; + const want = backend.of(wantE); + const have = backend.empty(); + + getBucketStub.resolves({ location: "us-east1" }); + + await prepare.resolveDefaultRegions(want, have); + + expect(want.endpoints["us-east1"]?.["onArchive"]).to.exist; + expect(want.endpoints["us-east1"]?.["onArchive"].vpc?.connector).to.equal( + "projects/project/locations/us-east1/connectors/my-connector", + ); + }); }); describe("inferDetailsFromExisting", () => { diff --git a/src/deploy/functions/prepare.ts b/src/deploy/functions/prepare.ts index ad21e308c77..d56c87694b5 100644 --- a/src/deploy/functions/prepare.ts +++ b/src/deploy/functions/prepare.ts @@ -355,6 +355,15 @@ function moveEndpointToRegion( region: string, ) { endpoint.region = region; + + // Update VPC connector region if it was constructed using REGION_TBD + if (endpoint.vpc?.connector?.includes(`locations/${build.REGION_TBD}/`)) { + endpoint.vpc.connector = endpoint.vpc.connector.replace( + `locations/${build.REGION_TBD}/`, + `locations/${region}/`, + ); + } + backend.endpoints[region] = backend.endpoints[region] || {}; backend.endpoints[region][endpoint.id] = endpoint; delete backend.endpoints[build.REGION_TBD][endpoint.id];