Region ID
The REGION_ID is an abbreviated code that Google assigns
based on the region you select when you create your app. The code does not
correspond to a country or province, even though some region IDs may appear
similar to commonly used country and province codes. For apps created after
February 2020, REGION_ID.r is included in
App Engine URLs. For existing apps created before this date, the
region ID is optional in the URL.
Learn more about region IDs.
This guide describes how to deploy your existing apps in the flexible environment to Cloud Run.
The steps described in this guide don't affect your existing App Engine app's functionality or traffic flow. The newly created Cloud Run service is a replica of the App Engine service that you can test independently.
App Engine flexible environment and Cloud Run are both fully managed, container-native application platforms, however, they differ structurally. To learn more about the similarities and differences between App Engine and Cloud Run, including the benefits of migrating to Cloud Run, see the comparison summary. To deploy to Cloud Run, use the local configuration of yourapp.yaml
file. This builds a container image from your local source
code, so any recent changes to your code or configuration are included in the
new deployment.
Before you begin
Make sure that your App Engine application runs without errors. You need access to your App Engine source code.
Enable the Cloud Run Admin API and the Artifact Registry API:
Configure your project and region using the following command:
gcloud auth login gcloud config set project PROJECT_ID gcloud config set run/region REGION gcloud components updateReplace the following:
- PROJECT_ID: your Google Cloud project ID.
- REGION: the region where you want to deploy your Cloud Run service.
Check for incompatible features in your application, and remove them before migrating to Cloud Run. To check your application for incompatibility without performing a migration or deployment, run the following command from the directory where your
app.yamlfile is located:gcloud beta app migrate-to-run --dry-runReview the compatibility check results and make the recommended changes if required.
Review the following Cloud Run differences:
Cloud Run uses the term
Revision, instead ofVersion, to represent each time that you deploy changes to a specific service. Deploying your app to a service in Cloud Run for the first time creates its first revision. Each subsequent deployment of a service creates another revision. Learn more about deploying to Cloud Run.You can deploy your source code to Cloud Run using the Google Cloud console or the Google Cloud CLI to configure and manage your app settings. Cloud Run doesn't require file-based configuration; however, YAML configuration is supported.
Every service that you deploy to Cloud Run uses the
run.appdomain in the URL to access the service publicly.Unlike App Engine services that are public by default, Cloud Run services are private by default and require you to configure them for public (unauthenticated) access.
Required roles
You can choose to either create a new service account or use the same user-managed service account in Cloud Run that you are using for the flexible environment. You or your administrator must grant the deployer account and the Cloud Build service account the following IAM roles.
Click to view required roles for the deployer account
To get the permissions that you need to build and deploy from source, ask your administrator to grant you the following IAM roles:
- Cloud Run Source Developer (
roles/run.sourceDeveloper) on your project - Service Usage Consumer (
roles/serviceusage.serviceUsageConsumer) on your project - Service Account User (
roles/iam.serviceAccountUser) on the Cloud Run service identity
Click to view required roles for the Cloud Build service account
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, ask your administrator to grant
Cloud Run Builder
(roles/run.builder) to the Compute Engine default
service account on your project:
gcloud projects add-iam-policy-binding PROJECT_ID \ --member=serviceAccount:PROJECT_NUMBER-compute@developer.gserviceaccount.com \ --role=roles/run.builder
Replace PROJECT_NUMBER with your Google Cloud
project number, and PROJECT_ID with your Google Cloud
project ID. For detailed instructions on how to find your project ID, and project number,
see Creating
and managing projects.
Granting the Cloud Run builder role to the Compute Engine default service account takes a couple of minutes to propagate.
For a list of IAM roles and permissions that are associated with Cloud Run, see Cloud Run IAM roles and Cloud Run IAM permissions. If your Cloud Run service interfaces with Google Cloud APIs, such as Cloud Client Libraries, see the service identity configuration guide. For more information about granting roles, see deployment permissions and manage access.
Use the local configuration of an app.yaml file
Deploy your local App Engine configuration to Cloud Run using the Google Cloud console or the Google Cloud CLI.
Console
In the Google Cloud console, go to the App Engine page.
In the navigation menu, click Migration hub to deploy your App Engine app to Cloud Run.
In the Flexible to Cloud Run migration card, select the Service and Version that you want to migrate, and then click Check compatibility.
In the Check compatibility section, view the compatibility results. The migration hub runs a compatibility checker to identify incompatible features.
Review the results to verify that your app has no blocking incompatibilities, and then click Continue to proceed to the Review & Deploy section.
Copy the command in the Deploy through Gcloud section, replace the PATH_TO_APP_YAML placeholder with the path to your
app.yamlfile, and run the command from your application source directory.Click View deployed versions to see your App Engine services deployed in Cloud Run.
gcloud
To deploy your Cloud Run service using the local app.yaml file
of an existing App Engine configuration, follow these steps:
In your terminal, change to your source directory where the
app.yamlfile exists.Run the following command to deploy your service to Cloud Run:
gcloud beta app migrate-to-runThis command generates the
service.yamlfile configuration for Cloud Run and saves it locally to the same directory as yourapp.yamlfile. For more information, seegcloud beta app migrate-to-run.- When prompted
Proceed with the deployment?, enterYto build a container image from your source code, and deploy the service to Cloud Run.
- When prompted
Test your deployed Cloud Run service. Because your Cloud Run service is private by default, opening the service URL directly in a web browser returns a
403 Forbiddenerror. To view your service, do one of the following:- To test the service privately (recommended), run the
gcloud run services proxycommand, and then open the local proxy URL in a web browser. - If you want your service to be publicly available, enable public access, and then open the service URL in a web browser.
Optional:
If your
app.yamlfile is in a different directory, specify its path using the--appyamlflag:gcloud beta app migrate-to-run --appyaml=PATHReplace PATH with the path to your
app.yamlfile.To generate and export the Cloud Run
service.yamlconfiguration without deploying the service, use the--export-onlyflag:gcloud beta app migrate-to-run --export-only=EXPORT_PATHReplace EXPORT_PATH with the directory or path where you want to save the
service.yamlfile.
- To test the service privately (recommended), run the
Incompatible features
The migration command fails or displays a warning if your app.yaml file
contains any of the following unsupported configurations:
Inbound services:
inbound_services: - warmupSolution: Delete the
inbound_servicessection from yourapp.yamlfile. Cloud Run warms up instances using the container entrypoint, so you don't need to configure warmup requests. If you need to run initialization code before serving traffic, configure your service to run it at startup, before listening for requests or use startup probes. You can also configure minimum instances to keep instances warm.Custom error pages:
error_handlers: - file: default_error.html - error_code: over_quota file: over_quota.htmlSolution: Delete the
error_handlerssection from yourapp.yamlfile, and follow either or both of these strategies:- Implement error handling directly within your application code. Catch
standard HTTP error codes, such as
404or500, and serve your custom HTML error pages from within your application logic. - If you are using Cloud Load Balancing with Cloud Run, configure custom error responses at the load balancer level. This lets you serve custom error pages from Cloud Storage based on the response code from your Cloud Run service.
- Implement error handling directly within your application code. Catch
standard HTTP error codes, such as
URL and static file handlers:
handlers: - url: /stylesheets static_dir: stylesheetsSolution: Delete the
handlerssection from yourapp.yamlfile. Cloud Run doesn't supportapp.yamlrequest routing or static file handlers. Instead, serve static files directly from your Cloud Run service or from Cloud Storage, and handle URL routing within your application code or by using Cloud Load Balancing.Build environment variables:
build_env_variables: BUILD_MODE: "production"The compatibility check in the migration hub reports this configuration as a blocking incompatibility. The
gcloud beta app migrate-to-runcommand doesn't report it, and applies the variables to the build when you deploy from source.Solution: Delete the
build_env_variablessection from yourapp.yamlfile. If your build still requires these values, set build environment variables when you deploy your Cloud Run service from source. If your app needs these values at runtime, set them as environment variables on the service.Basic scaling:
basic_scaling: max_instances: 10 idle_timeout: 10mSolution: Replace the
basic_scalingsection in yourapp.yamlfile withautomatic_scalingormanual_scaling. The compatibility check in the Migration hub blocks versions that use basic scaling.Resource and scaling values outside Cloud Run limits:
manual_scaling: instances: 1500The migration displays a warning if your configuration is outside any of the following Cloud Run limits:
- CPU: 8 vCPUs
- Memory: 512 MiB to 32 GiB
- Minimum, maximum, or manual scaling instances: 1,000
- Concurrent requests per instance: 1,000
Solution: Update your
app.yamlfile to use values within these limits. For more information, see CPU limits, memory limits, and concurrency. If you need more than 1,000 instances, request a quota increase.
Forwarded ports with host port mappings, protocols, or multiple ports:
network: forwarded_ports: - 8080:8000 - 2222/udpSolution: Update
network.forwarded_portsin yourapp.yamlfile to specify only a single numeric container port, such as8080, or deleteforwarded_portsand configure the container port in Cloud Run. Cloud Run accepts only inbound TCP traffic on a single container port and doesn't support host-to-container port mappings or UDP traffic. The migration tool accepts only bare port numbers, so host port mappings and/tcpor/udpprotocol suffixes, such as8080/tcp, cause the migration to fail. If you specify multiple numeric ports, the migration tool logs a warning and configures only the first port.Disk size:
resources: disk_size_gb: 20Solution: Delete
disk_size_gbfrom theresourcessection of yourapp.yamlfile. The migration tool displays a warning and ignores this setting because Cloud Run defaults to an in-memory file system, where writing files consumes instance memory. If your application needs to write files without consuming memory, configure an ephemeral disk volume mount or a Cloud Storage volume mount in Cloud Run to avoid out-of-memory (OOM) errors.
What's next
- Understand how to manage your Cloud Run services.
- See the Cloud Run container runtime contract to understand the requirements and behaviors of containers in Cloud Run.
- Learn how to store dependencies for your service requiring API keys, passwords, or other sensitive information using a Secret Manager.