Migrate an App Engine standard environment app to Cloud Run

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 migrate an existing App Engine app that connects to a Cloud SQL instance with a public IP.

In general, the steps in this guide demonstrate how you can deploy the same application source code in Cloud Run and then configure it to use the same Cloud SQL database user to connect your existing instance and database.

The steps in this guide don't include how to use an internal private IP connection because that requires you to first modify your application code. However, after you deploy your app to Cloud Run, you can then use the steps in Connect to Cloud SQL from Cloud Run to learn the requirements and how to use Private IPs.

To understand more about the similarities and differences between App Engine and Cloud Run, including the benefits for migrating to Cloud Run, see the comparison summary.

Before you begin

  1. Ensure that Cloud Run meets your application requirements. Review App Engine and Cloud Run comparison to determine if the Cloud Run resources like CPU and memory meet your needs.
  2. This guide assumes that your app runs without errors.

  3. You need access to your Cloud SQL instance, including the database username and password for connecting your app. Cloud Run uses encryption and connects through the Cloud SQL Auth proxy using either Unix sockets or Cloud SQL connectors.

  4. Review the following Cloud Run differences:

    • Cloud Run uses the term Revision, instead of Version to represent each time 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 Google Cloud CLI or the Google Cloud console to configure and manage your apps settings. Cloud Run does not require file-based configuration, however YAML configuration is supported and you can use the app2run tool to translate your existing App Engine file for Cloud Run.

    • Every service that you deploy to Cloud Run uses the run.app domain 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.

Migrate your app to Cloud Run

At a high level, the process to migrate your App Engine app to Cloud Run consists of the following steps:

  1. Enable the required APIs
  2. Configure the Cloud Run service account
  3. Deploy your app in Cloud Run

Enable the required APIs

Before you can deploy your app to Cloud Run, you must first enable both the Cloud Run and Artifact Registry APIs.

Use the Google Cloud console to enable the APIs:

Go to APIs and Services

Configure the Cloud Run service account

You can choose to either create a new service account or continue using the same user-managed service account in Cloud Run that you are using for App Engine. In the service account, you must ensure that the following Identity and Access Management (IAM) roles or the equivalent permissions are configured:

For deploying to Cloud Run, you must have one of the following:

For public IP connections to Cloud SQL, you must have one of the following:

Deploy your app in Cloud Run

It is not necessary to make any code changes to deploy your App Engine app to Cloud Run.

In the following steps, you deploy your app to a new service in Cloud Run and concurrently configure that service to connect to Cloud SQL.

Like App Engine standard environment, Cloud Run supports source-based deployments. You need access to your source code repository.

Cloud Run internally uses buildpacks and Cloud Build to automatically build container images from your source code and does not require you to manually build a container or specify a Dockerfile. However, if a Dockerfile is present it will be used.

Deploying a Cloud Run service from source uses Artifact Registry, so this feature is only available in regions supported by Artifact Registry.

To deploy the same source code that you previously deployed to App Engine:

  1. Change to your source directory where the source code of your application resides.

      cd YOUR_APPENGINE_CODE_DIR
    
  2. Deploy to Cloud Run.

    To build your source code and deploy your application, run the deploy command with the --source flag. You must set configuration flags to include the same SQL connection environment variables defined in the app.yaml file of your App Engine app:

      gcloud run deploy run-sql --source SOURCE \
        --allow-unauthenticated \
        --add-cloudsql-instances INSTANCE_CONNECTION_NAME\
        --set-env-vars INSTANCE_UNIX_SOCKET="/cloudsql/INSTANCE_CONNECTION_NAME" \
        --set-env-vars INSTANCE_CONNECTION_NAME="INSTANCE_CONNECTION_NAME" \
        --set-env-vars DB_NAME="DB_NAME" \
        --set-env-vars DB_USER="DB_USER" \
        --set-env-vars DB_PASS="DB_PASS"
    

    Replace:

    • SOURCE with the path to your App Engine source directory
    • INSTANCE_CONNECTION_NAME with the instance connection name of your Cloud SQL instance, or a comma delimited list of connection names. You can find the INSTANCE_CONNECTION_NAME by running:
        gcloud instances describe INSTANCE_NAME
    
    • DB_NAME with the name of your database.
    • DB_USER with the username of your database.
    • DB_PASS with the password of your database user.
  3. Enter a name of the SERVICE when prompted.

  4. Respond to any prompts to install required APIs by responding y when prompted. You only need to do this once for a project. Wait for the build and deploy to complete. When finished, a message similar to this one is displayed:

    Service [my-app] revision [my-app-00000-xxx] has been deployed and is serving 100 percent of traffic. Service URL: https://sample.run.app
    

    To learn more about deploying source code to Cloud Run, see Deploying from source code.

Next steps