Connect to Cloud SQL for SQL Server from Cloud Run functions
Learn how to deploy a sample app on Cloud Run functions 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
- 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.
-
Make sure 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.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud APIs necessary to run a Cloud SQL sample app on Cloud Run functions.
Console
Click Enable APIs to enable the APIs required for this quickstart.
This enables the following APIs:
- Cloud Run functions API
- Cloud SQL Admin API
- Cloud Run Admin API
- Cloud Build API
- Artifact Registry API
- Eventarc API
- Compute Engine API
- Service Networking 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.Run the following
gcloud
command using Cloud Shell:gcloud services enable cloudfunctions.googleapis.com sqladmin.googleapis.com run.googleapis.com cloudbuild.googleapis.com artifactregistry.googleapis.com eventarc.googleapis.com compute.googleapis.com servicenetworking.googleapis.com pubsub.googleapis.com logging.googleapis.com
This command enables the following APIs:
- Cloud Run functions API
- Cloud SQL Admin API
- Cloud Run API
- Cloud Build API
- Artifact Registry API
- Eventarc API
- Compute Engine API
- Service Networking API
- Cloud Logging API
Add the Cloud Run functions Invoker IAM role to your user.
Set up Cloud SQL
Create a Cloud SQL instance
Create a database
Console
-
In the Google Cloud console, go to the Cloud SQL Instances page.
- Select
quickstart-instance
. - From the SQL navigation menu, select Databases.
- Click Create database.
- In the Database name field of the New database dialog
box, enter
quickstart-db
. - Click Create.
gcloud
Run the gcloud
sql databases create
command to create a database.
gcloud sql databases create quickstart-db --instance=quickstart-instance
Configure a Cloud Run functions service account
Console
-
In the Google Cloud console, go to the IAM page.
- For the service account named Compute Engine default service account, click the pencil icon.
- Click ADD ANOTHER ROLE.
- Add the Role named Cloud SQL Client.
- Click Save.
gcloud
-
Run the following
gcloud
command to get a list of your project's service accounts:gcloud iam service-accounts list
- Copy the EMAIL of the Compute Engine service account.
- Run the following command to add the Cloud SQL Client
role to Compute Engine service account:
gcloud projects add-iam-policy-binding YOUR_PROJECT_ID \ --member="serviceAccount:SERVICE_ACCOUNT_EMAIL" \ --role="roles/cloudsql.client"
Create a function
-
In the Google Cloud console, go to the Cloud Run functions page.
- Click Create function.
- For the Environment, select 2nd gen.
- In the Function name field, enter
quickstart-function
. - In the Authentication region, select Allow unauthenticated invocations.
- Click Next.
- From the Runtime menu, select one of the following languages:
- In the Source code menu, verify that the Inline editor is selected. In this step, you use the default function provided in the editor.
Language | Version number |
---|---|
Go | 1.20 |
Python | 3.10 |
Deploy the function
- At the bottom of the Cloud Run functions page, click Deploy. The Google Cloud console redirects you to the function's details page.
- When the function deployment is complete, click the URL link to see
a web browser displaying the message
Hello, World!
.
Deploy the Cloud SQL sample app as a function
You configure and deploy the Cloud SQL sample app as a function in Cloud Run functions. First, you use Cloud Shell to configure and build the sample app. Then, you use Cloud Run functions to deploy the sample app.
Configure a Cloud SQL sample app
Deploy the sample app
The steps to deploy the sample to Cloud Run functions depend on the type of IP address you assigned 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.
-
In the Google Cloud console, go to the Cloud SQL Instances page.
- Select the
quickstart-instance
instance to open the Instance details page. - In the icon bar at the top of the page, click Delete.
- In the Delete instance dialog box, type
quickstart-instance
, and then click Delete to delete the instance. -
In the Google Cloud console, go to the Cloud Run functions page.
- Select the checkbox next to the
quickstart-function
service name. - Click Delete at the top of the Cloud Run functions page.
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.
-
In the Google Cloud console, go to the IAM page.
- Click the edit icon (which looks like a pencil) for the IAM account named Compute Engine default service account.
- Delete the Cloud SQL client role.
- Click Save.
If you're not using the APIs that were enabled as part of this quickstart, you can disable them.
- APIs that were enabled within this quickstart:
- Compute Engine API
- Cloud SQL Admin API
- Cloud Run API
- Container Registry API
- Cloud Build API
In the Google Cloud console, go to the APIs page.
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:
- Configuring your Cloud SQL instance with a public IP address.
- Configuring your Cloud SQL instance with a private IP address.
Additionally, you can learn about connecting to a Cloud SQL instance from other Google Cloud applications: