Connect to Cloud SQL for SQL Server from App Engine standard environment

Learn how to deploy a sample app on App Engine standard environment connected to a SQL Server instance by using the Google Cloud console and a client application.

Assuming that you complete all the steps in a timely manner, the resources created in this quickstart typically cost less than one dollar (USD).

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.

    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 the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. 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 the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  5. Verify that billing is enabled for your Google Cloud project.

  6. Enable the Cloud APIs necessary to run a Cloud SQL sample app on App Engine standard environment.

    Console

    Click the button below to enable the APIs required for this quickstart.

    Enable APIs

    This enables the following APIs:

    • Cloud SQL Admin API
    • Compute Engine API
    • Cloud Build API
    • Cloud Logging API

    gcloud

    Click the following button to open Cloud Shell, which provides command-line access to your Google Cloud resources directly from the browser. Cloud Shell can be used to run the gcloud commands presented throughout this quickstart.

    Open Cloud Shell

    Run the following gcloud command using Cloud Shell:

    gcloud services enable sqladmin.googleapis.com compute.googleapis.com \
    cloudbuild.googleapis.com logging.googleapis.com

    This command enables the following APIs:

    • Cloud SQL Admin API
    • Compute Engine API
    • Cloud Build API
    • Cloud Logging API

Set up Cloud SQL

Create a Cloud SQL instance

Private IP

Take the following steps to set up a Cloud SQL instance that you connect to over private IP:

Console

Allocate an IP address range and create a private connection to configure private services access for Cloud SQL

  1. In the Google Cloud console, go to the VPC networks page.

    Go to VPC networks

  2. Select the default VPC network.
  3. Select the Private service connection tab.
  4. Select the Allocated IP ranges for services tab.
  5. Click Allocate IP range.
  6. For the Name of the allocated range, specify google-managed-services-default.
  7. Select the Automatic option for IP range and specify the prefix length as 16.
  8. Click Allocate to create the allocated range.
  9. Select the Private connections to services tab for the default VPC network.
  10. Click Create connection to create a private connection between your network and a service producer.
  11. For the Assigned allocation, select google-managed-services-default.
  12. Click Connect to create the connection.

Create an instance with private IP address and SSL enabled

  1. In the Google Cloud console, go to the Cloud SQL Instances page.

    Go to Cloud SQL Instances

  2. Click Create instance.
  3. From the Create instance menu, select New instance.
  4. Click Choose SQL Server.
  5. Make sure that Enterprise Plus is selected as the Cloud SQL edition for your instance.
  6. In the Instance ID field, enter quickstart-instance.
  7. In the Password field, enter a password for the sqlserver user. Save this password for future use.
  8. In the Choose region and zonal availability section, select Single zone.
  9. Click the Show configuration options menu.
  10. Expand the Machine configuration node.
  11. From the Machine shapes region, select the 4 vCPU, 32 GB shape.
  12. Expand the Connections node.
  13. Clear the Public IP checkbox to create an instance only with a private IP address.
  14. Select the Private IP checkbox.
  15. Select the Private Service Access (PSA) checkbox.
  16. In the VPC Network * dropdown, select default.
  17. In the Security section, make sure that Allow only SSL connections is selected to enable SSL connections.
  18. Click Create instance and then wait for the instance to initialize and start.

gcloud

Allocate an IP address range and create a private connection to configure private services access for Cloud SQL

  1. Run the gcloud compute addresses create command to allocate an IP address range.

    gcloud compute addresses create google-managed-services-default \
    --global --purpose=VPC_PEERING --prefix-length=16 \
    --description="peering range for Google" --network=default
  2. Run the gcloud services vpc-peerings connect command to create a private connection to the allocated IP address range. Replace YOUR_PROJECT_ID with your project's project ID.

    gcloud services vpc-peerings connect --service=servicenetworking.googleapis.com \
    --ranges=google-managed-services-default --network=default \
    --project=YOUR_PROJECT_ID

Create an instance with private IP address and SSL enabled

  1. Before running the command as follows, replace DB_ROOT_PASSWORD with the password of your database user.

    Optionally, modify the values for the following parameters:

    • --database-version: The database engine type and version. If left unspecified, the API default is used. See the gcloud database versions documentation to see the current available versions.
    • --cpu: The number of cores in the machine.
    • --memory: A whole number value indicating how much memory to include in the machine. A size unit can be provided (for example, 3072MB or 9GB). If no units are specified, GB is assumed.
    • --region: The regional location of the instance (for example asia-east1, us-east1). If left unspecified, the default us-central1 is used. See the full list of regions.
  2. Run the gcloud sql instances create command to create a Cloud SQL instance with a Private IP address.

    gcloud sql instances create quickstart-instance \
    --database-version=SQLSERVER_2017_STANDARD \
     --cpu=1 \
     --memory=4GB \
     --region=us-central \
     --root-password=DB_ROOT_PASSWORD \
     --no-assign-ip \
    --network=default
  3. Run the gcloud sql instances patch command to enable only allow SSL connections for the instance.

  4. gcloud sql instances patch quickstart-instance --require-ssl

Create a database

Take the following steps to create a database:

Console

  1. In the Google Cloud console, go to the Cloud SQL Instances page.

    Go to Cloud SQL Instances

  2. Select quickstart-instance.
  3. From the SQL navigation menu, select Databases.
  4. Click  Create database.
    1. In the Database name field of the New database dialog, enter quickstart-db.
    2. Click Create.

gcloud

Run the gcloud sql databases create command to create a database.

gcloud sql databases create quickstart-db --instance=quickstart-instance

Deploy a sample app to App Engine standard environment

Create the App Engine application

Create the App Engine application in your Google Cloud project. This enables the App Engine service, creates a default App Engine application and creates a App Engine service account that will be used to connect to Cloud SQL.

Console

  1. In the Google Cloud console, go to the App Engine page.

    Go to App Engine

  2. Click the Create application button.
  3. Select us-central from the Select a region drop-down menu.
  4. Click Next.
  5. Click the I'll do this later link once it appears on the Get started page.

gcloud

  1. Run the following gcloud app create command to create an App Engine application:
  2. gcloud app create
  3. When prompted to choose the region where you want your App Engine application located, enter the numeric choice for us-central.

Configure the App Engine service account

Configure the service account used by App Engine so that it has the Cloud SQL Client role with permissions to connect to Cloud SQL.

Console

  1. In the Google Cloud console, go to the Service accounts page.

    Go to IAM

  2. For the service account named App Engine default service account, click the pencil icon.
  3. Click Add another role.
  4. Add the Role named Cloud SQL Client.
  5. Click Save.

gcloud

  1. Run the following gcloud command to get a list of your project's service accounts:
    gcloud iam service-accounts list
  2. Copy the EMAIL of the App Engine service account.
  3. Run the following command to add the Cloud SQL Client role to App Engine service account:
    gcloud projects add-iam-policy-binding YOUR_PROJECT_ID \
      --member="serviceAccount:SERVICE_ACCOUNT_EMAIL" \
      --role="roles/cloudsql.client"

Configure and deploy a Cloud SQL sample app

With a Cloud SQL instance, database, and service account with client permissions, you can now configure and deploy a sample application to connect to your Cloud SQL instance.

Clean up

To avoid incurring charges to your Google Cloud account for the resources used on this page, follow these steps.

  1. In the Google Cloud console, go to the Cloud SQL Instances page.

    Go to Cloud SQL Instances

  2. Select the quickstart-instance instance to open the Instance details page.
  3. In the icon bar at the top of the page, click Delete.
  4. In the Delete instance dialog box, type quickstart-instance, and then click Delete to delete the instance. Disabling your app stops it from running instances and serving requests. If your app is processing a request, your app completes the request before being disabled.

To disable an App Engine app and retain its data, do the following:

  • In the Google Cloud console, go to the Settings page:

    Go to Settings

  • In the Application settings tab, click Disable application.

  • In the App ID field, enter the ID of the app you want to disable, and then click Disable.

    Optional cleanup steps

    If you're not using the Cloud SQL client role that you assigned to the Compute Engine service account, you can remove it.

    1. In the Google Cloud console, go to the Service accounts page.

      Go to IAM

    2. Click the edit icon (which looks like a pencil) for the IAM account named Compute Engine default service account.
    3. Delete the Cloud SQL client role.
    4. Click Save.

    If you're not using the APIs that were enabled as part of this quickstart, you can disable them.

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

      Go to APIs

    2. Select any API that you would like to disable and then click the Disable API button.

    What's next

    Based on your needs, you can learn more about creating Cloud SQL instances.

    You also can learn about creating SQL Server users and databases for your Cloud SQL instance.

    For more information about pricing, see Cloud SQL for SQL Server pricing.

    Learn more about:

    Additionally, you can learn about connecting to a Cloud SQL instance from other Google Cloud applications: