Connecting to Cloud SQL with Cloud Functions
1. Introduction
Last Updated: 2021-05-11
What is Cloud SQL?
Cloud SQL is a fully-managed database service that makes it easy to set up, maintain, manage, and administer your relational databases on Google Cloud Platform.
What is Cloud Functions?
Cloud Functions is a lightweight compute solution for developers to create single-purpose, stand-alone functions that respond to Cloud events without needing to manage a server or runtime environment.
What you'll build
In this codelab, you'll write a Cloud Function in Python. The function:
- Connects to a Cloud SQL Database instance.
- Sends an insert statement to a table in the database.
What you'll learn
- How to access the Cloud Functions web UI in the Google Cloud Console.
- How to create a Cloud Function.
- How to test a Cloud Function.
- How to connect to a Cloud SQL database instance (either MySQL or PostgreSQL) using Python.
- How to write to a Cloud SQL database using Python.
2. Requirements
- A browser, such asChrome orFirefox.
- A Google Cloud Platform project that contains your Cloud SQL instance.
- If you don't already have one, you can follow the steps in the Quickstart forMySQL orPostgreSQL. Do all the steps except for the clean up.
- Your instance contains a MySQL or PostgreSQL database with a table.
- Your instance connection name, the database and table names, the database user name and the user's password.
- You can find theconnection_name in the Console UI. Navigate to your SQL instance's Overview page. The connection name is in the format:PROJECT_ID:REGION:INSTANCE_NAME
- The database names are your own. If you have not already created a Cloud SQL database, you can follow the steps in the Quickstart forMySQL orPostgreSQL. Here is an example of a simple database and table for MySQL:
- CREATE DATABASE library;
- USE library;
- CREATE TABLE books (title VARCHAR(100));
- INSERT into books (title) VALUES ("Cloud SQL for Winners");
- A Service account with theCloud SQL Client role.
- In the Console UI, navigate to theIAM & Admins > IAM page.
- You can use the default Compute Engine service account. It has the suffixcompute@developer.gserviceaccount.com.
- SelectEdit using the pencil icon.
- ClickADD ANOTHER ROLE and addCloud SQL > Client.
- ClickSAVE.
3. Prepare the code and create the function
Prepare the code
The Cloud Function code for connecting to a Cloud SQL database is right here. Some of the variable values depend on whether your Cloud SQL database is MySQL or PostgreSQL, and depend on your own database information.
The Cloud Functions UI in the Cloud Console includes a text editor. You can copy/paste and edit the code there, or edit the code locally first, and then copy/paste it into the UI.
requirements.txt
#ThisfiletellsPythonwhichmodulesitneedstoimportSQLAlchemy==1.3.12#IfyourdatabaseisMySQL,uncommentthefollowingline:#PyMySQL==0.9.3#IfyourdatabaseisPostgreSQL,uncommentthefollowingline:#pg8000==1.13.2main.py
#Thisfilecontainsallthecodeusedinthecodelab.importsqlalchemy#Dependingonwhichdatabaseyouareusing,you'll set some variables differently.# In this code we are inserting only one field with one value.# Feel free to change the insert statement as needed for your own table'srequirements.#Uncommentandsetthefollowingvariablesdependingonyourspecificinstanceanddatabase:#connection_name=""#table_name=""#table_field=""#table_field_value=""#db_name=""#db_user=""#db_password=""#IfyourdatabaseisMySQL,uncommentthefollowingtwolines:#driver_name='mysql+pymysql'#query_string=dict({"unix_socket":"/cloudsql/{}".format(connection_name)})#IfyourdatabaseisPostgreSQL,uncommentthefollowingtwolines:#driver_name='postgres+pg8000'#query_string=dict({"unix_sock":"/cloudsql/{}/.s.PGSQL.5432".format(connection_name)})#Ifthetypeofyourtable_fieldvalueisastring,surrounditwithdoublequotes.definsert(request):request_json=request.get_json()stmt=sqlalchemy.text('insert into {} ({}) values ({})'.format(table_name,table_field,table_field_value))db=sqlalchemy.create_engine(sqlalchemy.engine.url.URL(drivername=driver_name,username=db_user,password=db_password,database=db_name,query=query_string,),pool_size=5,max_overflow=2,pool_timeout=30,pool_recycle=1800)try:withdb.connect()asconn:conn.execute(stmt)exceptExceptionase:return'Error: {}'.format(str(e))return'ok'Create the function
- In a browser, go to theGoogle Cloud Platform Console UI.
- SelectCloud Functions from the Navigation menu.
- ClickCREATE FUNCTION on the button bar.
- Enter a name for the function.
- Select theHTTP trigger. (Make a note of the URL displayed beneath the trigger item. It will be in this format:https://REGION-PROJECT_ID.cloudfunctions.net/FUNCTION_NAME)
- UnderAuthentication, selectAllow unauthenticated invocations to make the function public.
- Expand theRuntime, Build and Connections Settings InRuntime service account, select a service account that has theCloud SQL Client role.
- Click theNEXT button.
- SelectPython 3.7 for theruntime option.
- SelectInline editor for thesource code option.
- SelIn the source code editor windows, delete the existing content for bothrequirements.txt andmain.py, and replace them with your edited versions of the code above.
- Enterinsert as the name of theEntry point.
- ClickDeploy and wait while the function is created. The spinner stops spinning and a green check appears on the subsequent page when the function is ready to use.
4. Test the function
- In a browser, go to theGoogle Cloud Platform Console UI.
- SelectCloud Functions from the Navigation menu.
- Click on the name of the function you created earlier.
- Select theTESTING link in the middle of the page.
- SelectTEST THE FUNCTION.
- The result should appear:ok (If the test fails, you'll see a stack trace to help with debugging.)
- In a browser, go to the URL that you saved earlier, when you created the function. If you forgot to save the URL, you can get it from theTRIGGER link.
- Theok result should appear in the browser as well.
5. Clean up
To avoid incurring charges to your Google Cloud account for the resources used in this codelab, follow these steps.
Delete the Cloud SQL instance
- Go to theCloud SQL Instances page in the Google Cloud Console.
- Select the instance you created to open the Instance details page.
- In the icon bar at the top of the page, clickDelete.
- In the Delete instance window, type the name of your instance, then clickDelete to delete the instance. You cannot reuse an instance name for about 7 days after an instance is deleted.
Delete the Cloud Function
- Go to theCloud Functions page in the Google Cloud Console.
- Select the three dots underActions for your function and chooseDelete.
- Confirm deletion by clicking theDELETE button.
6. Congratulations
Congratulations, you've successfully built a Cloud Function that works with Cloud SQL.
Specifically, you've created a Cloud Function that connects and writes to a Cloud SQL database instance.
7. What's next?
Check out some of these codelabs...
- Connecting to Cloud SQL: Compute Engine, Private IP and Cloud SQL Proxy
- Introduction to Cloud SQL Insights
Further reading
Reference docs
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.