Deploy a Cloud Run app by using Cloud Shell
Learn how to set up and deploy a sample Hello World application to Cloud Run and view the status of your live service's resources.
Before you begin
- 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.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Required roles
To get the permissions that you need to deploy a Cloud Run app, ask your administrator to grant you the following IAM roles on your project:
-
Cloud Run Admin (
roles/run.admin) -
Cloud Run Source Developer (
roles/run.sourceDeveloper) -
Service Account User (
roles/iam.serviceAccountUser) -
Service Usage Admin (
roles/serviceusage.serviceUsageAdmin) -
Project IAM Admin (
roles/resourcemanager.projectIamAdmin) -
Logs Viewer (
roles/logging.viewer)
For more information about granting roles, see Manage access to projects, folders, and organizations.
You might also be able to get the required permissions through custom roles or other predefined roles.
Grant the Cloud Build service account access to your project
Cloud Build automatically uses the Compute 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, grant the Cloud Build service
account the Cloud Run
Builder
(roles/run.builder) role on your project:
gcloud projects add-iam-policy-binding PROJECT_ID \ --member=serviceAccount:SERVICE_ACCOUNT_EMAIL_ADDRESS \ --role=roles/run.builder
Replace PROJECT_ID with your Google Cloud
project ID and SERVICE_ACCOUNT_EMAIL_ADDRESS with the
email address of the Cloud Build service account. If you're using the
Compute Engine default service account as the Cloud Build service account, then
use the following format for the service account email address:
PROJECT_NUMBER-compute@developer.gserviceaccount.com
Replace PROJECT_NUMBER with your Google Cloud
project number.
For detailed instructions on how to find your project ID, and project number, see Creating and managing projects.
Granting the Cloud Run builder role takes a couple of minutes to propagate.
Create an application
- Launch the Cloud Shell Editor.
- From the Cloud Code status bar, select New Application.
- Choose Cloud Run application as your preferred application type.
From the list of sample applications (Node.js, Python with Flask and Django frameworks, Java, Go), choose a Hello World app.
For example, choosing
Python (Flask): Hello Worldcreates a starter Python (Flask) Hello World app.Select a folder as your application location and then click Create New Application.
Cloud Shell Editor loads the application in a new workspace. After the workspace loads, your app is accessible in the explorer view.
Deploy to Cloud Run
To deploy your application to Cloud Run, follow these steps:
- Open the command palette (press
Ctrl/Cmd+Shift+Por click View > Command Palette) and then run the Deploy to Cloud Run command. This brings up the Cloud Run Deploy UI. - If prompted, authorize Cloud Code to make Google Cloud API calls.
- Set your Google Cloud project.
- If prompted, enable the Cloud Run API by clicking Enable APIs.
- In the Deploy to Cloud Run dialog, select a pre-existing service or create a new one.
- If you're creating a new service, choose Cloud Run (fully managed) under Deployment Platform.
- Choose a region to deploy to.
- For Authentication, select Allow unauthenticated invocations to make this a public service.
After you define your preferred settings, you can click Deploy.
Cloud Code builds your image, pushes it to the registry, and deploys your app to Cloud Run. Your service is now live!
View your running service by following the URL displayed in your Cloud Run: Deploy dialog.
Edit your service
To edit your application, follow these steps:
- With the Explorer view, find the
app.pyfile that implements the service behavior. - Change the message "It's running!" to a message of your choosing.
- After you make this change, choose the Deploy to Cloud Run command using the Command Palette.
- Check that the service you just created is selected in Service Settings.
- Click Deploy.
- After your application finishes building and deploying, you can refresh your service to see your updated message.
View logs
You can also view logs from your deployed application with the Log Viewer that comes with Cloud Code.
To view logs, follow these steps:
- Click
Cloud
Code and then expand the Cloud Run section. Right-click your Cloud Run service and then click View logs.
Refresh your app in the browser, and see the new logs generated by clicking the Logs refresh button.
Clean up
To avoid incurring charges to your Google Cloud account for the resources used on this page, follow these steps.
To delete your project:
Go to the Projects page in the Google Cloud console:
Select the project you created for this quickstart and then click Delete.
This shuts down the project and schedules it for deletion.
If you're using a pre-existing project and would like to delete resources you created for this quickstart:
Container image: While Cloud Run doesn't charge when a service isn't in use, you might still be charged for storing the container image in Artifact Registry. To delete your image, open the Artifact Registry page, select the repository that contains your image, select your image, click Delete, and then click Delete.
Cloud Run service: To delete a Cloud Run service, from the Cloud Run page in the Google Cloud console, select the service you'd like to delete, click Delete, and then click Delete.
What's next
- Discover the variety of features that the Cloud Shell Editor interface has to offer.
- Learn how to configure Cloud Shell to customize your experience.