- A Cloud Guru
Integrating Cloud Functions and Cloud SQL
The implementation of microservices, like Google Cloud Functions, makes it possible for serverless architecture to effortlessly scale. Where Cloud Functions really shine is interacting with other Google Cloud services, such as Cloud SQL. In this hands-on lab, we’ll set up a Cloud SQL instance and database and then build a Cloud Function that, once triggered, inserts a field of data.
Table of Contents
- From the main Google Cloud console navigation, choose APIs & Services > Library.
- Search for Cloud SQL Admin, and enable the service if necessary.
- Return to the API Library page, and search for Cloud Functions. Then, enable it if necessary.
- Return to the API Library page, and search for Cloud Build. Then, enable it if necessary.
Set Up the Required Code
- From the main console navigation, choose SQL.
- Click Create instance.
- Select Choose MySQL.
- Set the instance ID to acg-db.
- Set the password to root.
- From the Database version list, choose MySQL 8.0.
- Leave the region at its default value.
- Set Zonal availability to Single.
- Expand Customize your instance and then expand Machine type.
- From the Machine Type list, choose Standard.
- Keep all the other default settings and click Create Instance.
- Navigate to IAM & Admins > IAM.
- Edit the permissions for the default Compute Engine service account.
- Select Add a new role and choose Cloud SQL > Client.
- Click Save.
Create Database, Set Up Schema, and Enter Data
- Activate the Cloud Shell by clicking its icon in the top row.
- If the Cloud Shell does not display the current project ID in yellow, open the Project panel by clicking the project name in the upper-left.
- Copy the current project ID.
- In the Cloud Shell, enter the following command:
gcloud config set project <PROJECT_ID>
- Connect to your Cloud SQL instance with the following command:
gcloud sql connect acg-db --user=root
- Enter your SQL password when prompted.
- Create and switch to a new database with the following commands:
CREATE DATABASE content; USE content;
- Create and populate a table with the following commands:
CREATE TABLE books (entryID INT NOT NULL AUTO_INCREMENT, title VARCHAR(100),PRIMARY KEY(entryID)); INSERT into books(title) VALUES ("Netrepreneur");
- Verify the entry with the following command:
SELECT * from books;
Create a Database Instance and Set Up Server Account Permissions
- From the main navigation, go to Storage > Browser.
- Choose Create bucket.
- In the Name field, enter a unique name.
- From the Default storage class options, choose Regional.
- Leave the remaining values as their defaults, and click Create.
Create a Cloud Function
- Navigate to the Cloud Functions dashboard.
- Click Create function.
- Apply the following settings:
- Trigger: HTTP
- Authentication: Allow unauthorized invocations
- Expand Runtime, build, connections and security settings.
- In the Runtime service account section of the Runtime tab, choose Compute Engine default service account from the Runtime service account list.
- Click Save and then Next.
- Runtime: Python 3.9
- Source code: Inline editor
- In the main.py field, paste the copied code.
- From the Cloud Shell Editor, open
requirements.txt, and copy all.
- Switch to the
requirements.txtfile, paste the copied code. 10.In the Entry point field, enter insert.
- Click Deploy.
Test Cloud Function
- After the Cloud Function is created, click its name.
- Choose the Trigger tab, and click the link.
- Verify "OK" in the browser.
- Return to the Cloud Shell editor.
- If necessary, reconnect with the Cloud SQL instance:
gcloud sql connect acg-db --user=root
- Verify the new value has been entered into the database table:
USE content; SELECT * from books;
What's a lab?
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.