Quickstart: Build and deploy a Node.js web app to Cloud Run

Learn how to use a single command to build and deploya "Hello World" web application from a code sample to Google Cloudusing Cloud Run.

By following the steps in this quickstart, Cloud Run automaticallybuilds a Dockerfile for you when youdeploy from source code.

Before you begin

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Note: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. Make sure that you have the following role or roles on the project: Cloud Run Admin, Cloud Run Source Developer, Logs Viewer

    Check for the roles

    1. In the Google Cloud console, go to theIAM page.

      Go to IAM
    2. Select the project.
    3. In thePrincipal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.

    4. For all rows that specify or include you, check theRole column to see whether the list of roles includes the required roles.

    Grant the roles

    1. In the Google Cloud console, go to theIAM page.

      Go to IAM
    2. Select the project.
    3. ClickGrant access.
    4. In theNew principals field, enter your user identifier. This is typically the email address for a Google Account.

    5. In theSelect a role list, select a role.
    6. To grant additional roles, clickAdd another role and add each additional role.
    7. ClickSave.
  5. Install the Google Cloud CLI.

    Note: If you installed the gcloud CLI previously, make sure you have the latest version by runninggcloud components update.
  6. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  7. Toinitialize the gcloud CLI, run the following command:

    gcloudinit
  8. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Note: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.

    Go to project selector

  9. Make sure that billing is enabled for your Google Cloud project.

  10. Make sure that you have the following role or roles on the project: Cloud Run Admin, Cloud Run Source Developer, Logs Viewer

    Check for the roles

    1. In the Google Cloud console, go to theIAM page.

      Go to IAM
    2. Select the project.
    3. In thePrincipal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.

    4. For all rows that specify or include you, check theRole column to see whether the list of roles includes the required roles.

    Grant the roles

    1. In the Google Cloud console, go to theIAM page.

      Go to IAM
    2. Select the project.
    3. ClickGrant access.
    4. In theNew principals field, enter your user identifier. This is typically the email address for a Google Account.

    5. In theSelect a role list, select a role.
    6. To grant additional roles, clickAdd another role and add each additional role.
    7. ClickSave.
  11. Install the Google Cloud CLI.

    Note: If you installed the gcloud CLI previously, make sure you have the latest version by runninggcloud components update.
  12. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  13. Toinitialize the gcloud CLI, run the following command:

    gcloudinit
  14. To set the default project for your Cloud Run service:
    gcloudconfigsetprojectPROJECT_ID
    ReplacePROJECT_ID with your Google Cloud project ID.
  15. If you are under a domain restriction organization policyrestricting unauthenticated invocations for your project, you will need to access your deployed service as described underTesting private services.

  16. Enable the Cloud Run Admin API and the Cloud Build API:

    gcloudservicesenablerun.googleapis.com\cloudbuild.googleapis.com

    After the Cloud Run Admin API is enabled, the Compute Engine default service account is automatically created.

  17. Make sure that you have the Service Account User role granted on the service identity. Bydefault, the service identity is the Compute Engine default service account.

    Grant the roles

    To grant access on the service identity resource, use thegcloud iam service-accounts add-iam-policy-binding command, replacing the highlighted variables with the appropriate values:

    gcloudiamservice-accountsadd-iam-policy-bindingSERVICE_ACCOUNT_EMAIL\--member="PRINCIPAL"\--role="roles/iam.serviceAccountUser"

    Replace the following:

    • SERVICE_ACCOUNT_EMAIL: the service account email address you are using as the service identity, such as:
      • The Compute Engine default service account:PROJECT_NUMBER-compute@developer.gserviceaccount.com
      • A service account that you created:SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com
    • PRINCIPAL: the user identifier. This is typically the email address for a Google Account.
  18. Grant the Cloud Build service account the following IAM role.

    Click to view required roles for the Cloud Build service account

    Cloud Build automatically uses theCompute Engine default service account as the default Cloud Build service account to build your source code and Cloud Run resource, unless you override this behavior. For Cloud Build to build your sources, ask your administrator to grantCloud Run Builder (roles/run.builder) to the Compute Engine default service account on your project:

    gcloudprojectsadd-iam-policy-bindingPROJECT_ID\--member=serviceAccount:PROJECT_NUMBER-compute@developer.gserviceaccount.com\--role=roles/run.builder

    ReplacePROJECT_NUMBER with your Google Cloud project number, andPROJECT_ID with your Google Cloud project ID. For detailed instructions on how to find your project ID, and project number, seeCreating and managing projects.

    Granting the Cloud Run builder role to the Compute Engine default service account takes a couple of minutes topropagate.

    Note:

    Theiam.automaticIamGrantsForDefaultServiceAccounts organization policy constraint prevents the Editor role from being automatically granted to default service accounts. If you created your organization after May 3, 2024, this constraint is enforced by default.

    We strongly recommend that you enforce this constraint to disable the automatic role grant. If you disable the automatic role grant, you must decide which roles to grant to the default service accounts, and thengrant these roles yourself.

    If the default service account already has the Editor role, we recommend that you replace the Editor role with less permissive roles.To safely modify the service account's roles, usePolicy Simulator to see the impact of the change, and thengrant and revoke the appropriate roles.

Write the sample service

To create and deploy a Node.js service, follow these steps:

  1. Create a new directory namedhelloworld and change directoryinto it:

    mkdir helloworldcd helloworld
  2. Create apackage.json file with the following contents:

    {"name":"helloworld","description":"Simple hello world sample in Node","version":"1.0.0","private":true,"main":"index.js","type":"module","scripts":{"start":"node index.js"},"engines":{"node":">=16.0.0"},"author":"Google LLC","license":"Apache-2.0","dependencies":{"express":"^4.17.1"}}
  3. In the same directory, create aindex.js file, and copy the followinglines into it:

    importexpressfrom'express';constapp=express();app.get('/',(req,res)=>{constname=process.env.NAME||'World';res.send(`Hello${name}!`);});constport=parseInt(process.env.PORT)||8080;app.listen(port,()=>{console.log(`helloworld: listening on port${port}`);});

    This code creates a basic web server that listens on the port defined by thePORT environment variable.

Your app is finished and ready to be deployed.

Deploy to Cloud Run from source

Important: This quickstart assumes that you have owner or editor roles in the project you are using for the quickstart. Otherwise, refer to theCloud Run Source Developer role for the required permissions for deploying a Cloud Run resource from source.

Deploy from source automatically builds a container image from source codeand deploys it.

To deploy from source:

  1. In your source code directory, deploy the current folder using the following command:

    gcloudrundeploy--source.
    1. When you are prompted for the service name, press Enter to accept thedefault name, for examplehelloworld.

    2. If you are prompted to enable additional APIs on the project,for example, the Artifact Registry API, respond by pressingy.

    3. When you are prompted for region: select theregionof your choice, for exampleeurope-west1.

    4. If you are prompted to create a repository in the specified region, respond by pressingy.

    5. If you are prompted toallow unauthenticated invocations:respondy. You might not see this prompt if there is a domainrestriction organization policy that prevents it; for more details see theBefore you begin section.

    Then wait a few moments until the deployment is complete. On success, thecommand line displays the service URL.

  2. Visit your deployed service by opening the service URL in a web browser.

Success: You deployed a Node.js web app to Cloud Run.

Cloud Run locations

Cloud Run is regional, which means the infrastructure thatruns your Cloud Run services is located in a specific region and ismanaged by Google to be redundantly available acrossall the zones within that region.

Meeting your latency, availability, or durability requirements are primaryfactors for selecting the region where your Cloud Run services are run.You can generally select the region nearest to your users but you should considerthe location of theother Google Cloudproducts that are used by your Cloud Run service.Using Google Cloud products together across multiple locations can affectyour service's latency as well as cost.

Cloud Run is available in the following regions:

Subject toTier 1 pricing

  • asia-east1 (Taiwan)
  • asia-northeast1 (Tokyo)
  • asia-northeast2 (Osaka)
  • asia-south1 (Mumbai, India)
  • europe-north1 (Finland)leaf iconLow CO2
  • europe-north2 (Stockholm)leaf iconLow CO2
  • europe-southwest1 (Madrid)leaf iconLow CO2
  • europe-west1 (Belgium)leaf iconLow CO2
  • europe-west4 (Netherlands)leaf iconLow CO2
  • europe-west8 (Milan)
  • europe-west9 (Paris)leaf iconLow CO2
  • me-west1 (Tel Aviv)
  • northamerica-south1 (Mexico)
  • us-central1 (Iowa)leaf iconLow CO2
  • us-east1 (South Carolina)
  • us-east4 (Northern Virginia)
  • us-east5 (Columbus)
  • us-south1 (Dallas)leaf iconLow CO2
  • us-west1 (Oregon)leaf iconLow CO2

Subject toTier 2 pricing

  • africa-south1 (Johannesburg)
  • asia-east2 (Hong Kong)
  • asia-northeast3 (Seoul, South Korea)
  • asia-southeast1 (Singapore)
  • asia-southeast2 (Jakarta)
  • asia-south2 (Delhi, India)
  • australia-southeast1 (Sydney)
  • australia-southeast2 (Melbourne)
  • europe-central2 (Warsaw, Poland)
  • europe-west10 (Berlin)leaf iconLow CO2
  • europe-west12 (Turin)
  • europe-west2 (London, UK)leaf iconLow CO2
  • europe-west3 (Frankfurt, Germany)leaf iconLow CO2
  • europe-west6 (Zurich, Switzerland)leaf iconLow CO2
  • me-central1 (Doha)
  • me-central2 (Dammam)
  • northamerica-northeast1 (Montreal)leaf iconLow CO2
  • northamerica-northeast2 (Toronto)leaf iconLow CO2
  • southamerica-east1 (Sao Paulo, Brazil)leaf iconLow CO2
  • southamerica-west1 (Santiago, Chile)leaf iconLow CO2
  • us-west2 (Los Angeles)
  • us-west3 (Salt Lake City)
  • us-west4 (Las Vegas)

If you already created a Cloud Run service, you can view theregion in the Cloud Run dashboard in theGoogle Cloud console.

Clean up

Remove your test project

While Cloud Run does not charge when the service is not in use, youmight still becharged for storing the container image in Artifact Registry.You candelete your container imageor delete your Google Cloud project to avoid incurring charges.Deleting your Google Cloud project stops billing for all the resources usedwithin that project.

    Caution: Deleting a project has the following effects:
    • Everything in the project is deleted. If you used an existing project for the tasks in this document, when you delete it, you also delete any other work you've done in the project.
    • Custom project IDs are lost. When you created this project, you might have created a custom project ID that you want to use in the future. To preserve the URLs that use the project ID, such as anappspot.com URL, delete selected resources inside the project instead of deleting the whole project.
  1. In the Google Cloud console, go to theManage resources page.

    Go to Manage resources

  2. In the project list, select the project that you want to delete, and then clickDelete.
  3. In the dialog, type the project ID, and then clickShut down to delete the project.

What's next

For more information on building a container from code source and pushing toa repository, see:

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-07-22 UTC.