Deploying GraphOS Router on GCP
Deploy router with Google Cloud Run
Learn how to deploy the router for development on Google Cloud Platform (GCP) with Google Cloud Run.
You will:
Build a router image using a Dockerfile and a router configuration file.
Set up a container registry and push your router image to it.
Create a Cloud Run service and configure it to deploy your router.
Prerequisites
Before you start:
Set up a GraphQL API in GraphOS.
Save your
APOLLO_KEY
andAPOLLO_GRAPH_REF
in your GCP Secret Manager. You'll need them when deploying the router.
Install Docker locally.
Create a GCP account or use an existing account.
Create a GCP project. Choose a project name (for example,
my-project
) and save it to use later when deploying the router.Install the gcloud CLI and log in to your GCP account.
Choose a version of the router to deploy (for example,
v1.61.0
). You'll need it when specifying the router image to deploy.
Build router image
To deploy your own router, start by customizing and building a router image, using a Dockerfile and a router configuration file:
In a local directory, create a
router.yaml
file and copy-paste the following configuration into the file:YAMLrouter.yaml1supergraph: 2 listen: 0.0.0.0:4000 3health_check: 4 listen: 0.0.0.0:8088
The router's default HTTP and health check endpoint addresses are localhost, so they wouldn't be reachable when deployed. This configuration enables the router to listen to all addresses.
Create a
Dockerfile
file and copy-paste the following into the file:Text# Use the official Apollo Router Core image as the base. # Set the image tag to the desired router version (e.g. v1.61.0) FROM ghcr.io/apollographql/router:v1.61.0 # Replace the default router config with the local, customized router.yaml COPY router.yaml /dist/config/router.yaml
The Dockerfile sources the base router image from the GitHub container registry, using the version of router you specify. It then copies your customized
router.yaml
configuration file to overwrite the default router configuration file.From the same local directory, use
docker buildx
CLI command to build a new router image. Choose a name and tag for the image, for examplerouter-gcp:v1.61.0
.Bashdocker buildx build --platform linux/amd64 -t router-gcp:v1.61.0 --load .
Because Cloud Run only supports AMD64-based images, the
docker buildx build --platform linux/amd64
command ensures the image is built for AMD64 and is compatible.The
--load
option loads the built image todocker images
.
Use
docker images
to validate that your router image is successfully built and loaded.
Push router image to container registry
With a router image built, set up GCP Artifact Registry, then tag and push your image to it.
Set up container registry
In GCP, enable Artifact Registry in your project.
Create a repository and choose a repository name (for example,
my-repo
). Keep this name handy, as you'll need it later to build and deploy a router image.
Tag and push router image
Use
docker tag
to tag the image before pushing it to Artifact Registry. Make sure your tag conforms with Artifact Registry's naming convention (for example,us-west2-docker.pkg.dev/my-project/my-repo/router-gcp:v1.61.0
).Bashdocker tag router-gcp:v1.61.0 \ us-west2-docker.pkg.dev/my-project/my-repo/router-gcp:v1.61.0
Use
docker push
to push the router image to Artifact Registry.Bashdocker push us-west2-docker.pkg.dev/my-project/my-repo/router-gcp:v1.61.0
Validate the router image has been successfully pushed to Artifact Registry. You can use Google Cloud Console and navigate to your repository in Artifact Registry. You can also use the gcloud CLI and run
gcloud artifacts docker images
. For example:Bashgcloud artifacts docker images list us-west2-docker.pkg.dev/my-project/my-repo
Create and deploy Cloud Run service
With the router image pushed to GCP, you can now configure and deploy it as a Cloud Run service.
You can use either Google Cloud console or gcloud CLI. In either case, you need to gather the following information:
Name for your deployed router service (for example,
my-router
)GCP project name (for example,
my-project
)Artifact Registry repo name (for example,
my-repo
)GCP region (for example,
us-west2
)Full image path (for example,
us-west2-docker.pkg.dev/my-project/my-repo/router-gcp:v1.61.0
)APOLLO_KEY
andAPOLLO_GRAPH_REF
secrets
Deploy with Google Cloud console
In GCP console for your project, go to Cloud Run and select Deploy container > Service.
On the Create Service page, fill in the details:
Container image URL: Select your router image
Service name: Enter a name for your deployed router (for example,
my-router
)Region: Select your GCP region
Authentication: Select Allow unauthenticated invocations.
On the Container(s) > Edit Container tab, go to the Settings tab and fill in the details:
Container port: Enter
4000
(must match thesupergraph.listen
port of your router configuration)Container command: Enter
/dist/router
Container arguments: Enter
--dev
(runs the router in development mode)
Also on the Container(s) > Edit Container tab, go to the Variables & Secrets and fill in the details:
Add
APOLLO_KEY
and set it to your graph API keyAdd
APOLLO_GRAPH_REF
and set it to your graph ref
Click Deploy.
Once deployed, select the service from the Cloud Run console, then click on its URL (for example,
https://my-router-123456789012.us-west1.run.app/
) and validate that router's development Sandbox is running successfully.
Deploy with gcloud CLI
To deploy the router with the gcloud CLI, use
gcloud run deploy
with your configuration info in place of the example info:Bashgcloud run deploy my-router \ --image=us-west2-docker.pkg.dev/my-project/my-repo/router-gcp:v1.61.0 \ --command=/dist/router \ --args=--dev \ --set-secrets=APOLLO_KEY=APOLLO_KEY:latest,APOLLO_GRAPH_REF=APOLLO_GRAPH_REF:latest \ --region=us-west2 \ --project=router-container-gcp
Update traffic to your deployed router by running
gcloud run services update-traffic
:Bash1gcloud run services update-traffic my-router --to-latest
Use
gcloud run services
to get the service URL. For example, for a service namedmy-router
:Bash1gcloud run services describe my-router --format 'value(status.url)'
In a browser, go to the service URL and validate the the router's development Sandbox is running successfully.
Congrats, you've successfully deployed the router!