---
description:
  Google Cloud Platform (GCP) is a suite of cloud computing services powered by Google. It is easy
  to use Hive Gateway with GCP.
---

import { Callout } from '@theguild/components'

# Deploying Hive Gateway to Google Cloud Platform

Google Cloud Platform (GCP) is a suite of cloud computing services powered by Google. It is easy to
use Hive Gateway with GCP.

## Prerequisites

You will first need to install the GCP command-line tool: `gcloud`.
[You can find instructions here](https://cloud.google.com/sdk/docs/install).

If you already have `gcloud` installed, make sure it is up to date with `gcloud components update`.

[Create a new project](https://cloud.google.com/resource-manager/docs/creating-managing-projects)
and make sure
[billing is enabled](https://cloud.google.com/billing/docs/how-to/verify-billing-enabled).

<Callout type="warning">
  Running these examples requires you to have billing enabled on your GCP account. It should not
  cost more than a few cents, but don't forget to clean up your project after you are done to avoid
  unexpected charges.
</Callout>

## Cloud Functions

Cloud Functions is a serverless execution environment for building and connecting cloud services.
With Cloud Functions, you write simple, single-purpose functions that are attached to events, such
as an HTTP request.

It is probably the most straight forward way to deploy a Hive Gateway to GCP.

<Callout>
  Before you start, make sure you read the [Serverless / On the
  Edge](/docs/gateway/deployment/serverless) page.
</Callout>

### Installation

```sh npm2yarn
npm i @google-cloud/functions-framework @graphql-hive/gateway-runtime graphql
```

<Callout type="info">
  Don't forget to add the `main` field to your `package.json`. Google Cloud Functions rely on it to
  know which file to run.
</Callout>

<Callout type="info">
  This example uses ESM syntax, so you should set `"type": "module"` in your `package.json`.
</Callout>

### Usage

#### Gateway runtime

You can create a runnable `index.js` script that will be executed by Google Cloud Run Function to
serve requests.

See [Bundling Problems](/docs/gateway/deployment/serverless#bundling-problem) for more details about
how to load the supergraph and `transports` option.

```js filename=index.js
import { createGatewayRuntime } from '@graphql-hive/gateway-runtime'
import http from '@graphql-mesh/transport-http'
import supergraph from './supergraph.js'

export const graphql = createGatewayRuntime({
  supergraph,
  transports: {
    http // For example, http transport is required for subgraphs using standard GraphQL over HTTP.
  }
})
```

You can now deploy your function with `gcloud` CLI:

```bash
gcloud functions deploy graphql --runtime nodejs18 --trigger-http --allow-unauthenticated
```

You can now test your function by using the URL found in the `httpsTrigger.url` property returned by
the previous command or by using the `gcloud` CLI:

```bash
gcloud functions describe graphql
```

## Cloud Run

Cloud Run is the Platform as a Service by Google. It is straightforward to use Hive Gateway with it.

### Installation

Create a new Node project and add Hive Gateway to its dependencies.

```sh npm2yarn
npm i @graphql-hive/gateway graphql
```

<Callout type="info">
  This example uses ESM syntax, so you should set `"type": "module"` in your `package.json`.
</Callout>

Add a `start` script to your `package.json`. Cloud Run needs to know how to start your application.

<Callout>You can use Hive Gateway CLI as usual with `gateway.config.ts`</Callout>

```json
{
  "name": "hive-gateway-cloud-run-guide",
  "version": "1.0.0",
  "type": "module",
  "scripts": {
    "start": "hive-gateway supergraph"
  },
  "dependencies": {
    "graphql": "latest",
    "@graphql-hive/gateway": "latest"
  }
}
```

You can now deploy to Cloud Run. You can use all default values, except the last one, which allows
unauthenticated access to your service.

```bash
gcloud run deploy --source .
```

<Callout type="info">
  If this is your first time using Cloud Run, enabling the service can take up to a few minutes to
  be fully effective. If you encounter any `403 Forbidden` errors, please wait for 2 minutes and try
  again.
</Callout>

You can now access your API using the URL provided by `gcloud`. The default GraphQL endpoint is
`/graphql`.

If you need to use TypeScript or any other tool that requires a build phase, such as code
generation, add a Dockerfile to the root of your project so that Cloud Run can build a custom image
for you.
