Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.
  • Labs icon Lab
  • A Cloud Guru
Labs

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.

Labs

Path Info

Level
Clock icon Beginner
Duration
Clock icon 45m
Published
Clock icon May 24, 2019

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Table of Contents

  1. Challenge

    Enable APIs

    1. From the main Google Cloud console navigation, choose APIs & Services > Library.
    2. Search for Cloud SQL Admin, and enable the service if necessary.
    3. Return to the API Library page, and search for Cloud Functions. Then, enable it if necessary.
    4. Return to the API Library page, and search for Cloud Build. Then, enable it if necessary.
  2. Challenge

    Set Up the Required Code

    1. From the main console navigation, choose SQL.
    2. Click Create instance.
    3. Select Choose MySQL.
    4. Set the instance ID to acg-db.
    5. Set the password to root.
    6. From the Database version list, choose MySQL 8.0.
    7. Leave the region at its default value.
    8. Set Zonal availability to Single.
    9. Expand Customize your instance and then expand Machine type.
    10. From the Machine Type list, choose Standard.
    11. Keep all the other default settings and click Create Instance.
    12. Navigate to IAM & Admins > IAM.
    13. Edit the permissions for the default Compute Engine service account.
    14. Select Add a new role and choose Cloud SQL > Client.
    15. Click Save.
  3. Challenge

    Create Database, Set Up Schema, and Enter Data

    1. Activate the Cloud Shell by clicking its icon in the top row.
    2. 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.
    3. Copy the current project ID.
    4. In the Cloud Shell, enter the following command:
      gcloud config set project <PROJECT_ID>
      
    5. Connect to your Cloud SQL instance with the following command:
      gcloud sql connect acg-db --user=root
      
    6. Enter your SQL password when prompted.
    7. Create and switch to a new database with the following commands:
      CREATE DATABASE content;
      USE content;
      
    8. 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");
      
    9. Verify the entry with the following command:
      SELECT * from books;
      
  4. Challenge

    Create a Database Instance and Set Up Server Account Permissions

    1. From the main navigation, go to Storage > Browser.
    2. Choose Create bucket.
    3. In the Name field, enter a unique name.
    4. From the Default storage class options, choose Regional.
    5. Leave the remaining values as their defaults, and click Create.
  5. Challenge

    Create a Cloud Function

    1. Navigate to the Cloud Functions dashboard.
    2. Click Create function.
    3. Apply the following settings:
      • Name: acg-sql-function-1
      • Trigger: HTTP
      • Authentication: Allow unauthorized invocations
    4. Expand Runtime, build, connections and security settings.
    5. In the Runtime service account section of the Runtime tab, choose Compute Engine default service account from the Runtime service account list.
    6. Click Save and then Next.
      • Runtime: Python 3.9
      • Source code: Inline editor
    7. In the main.py field, paste the copied code.
    8. From the Cloud Shell Editor, open requirements.txt, and copy all.
    9. Switch to the requirements.txt file, paste the copied code. 10.In the Entry point field, enter insert.
    10. Click Deploy.
  6. Challenge

    Test Cloud Function

    1. After the Cloud Function is created, click its name.
    2. Choose the Trigger tab, and click the link.
    3. Verify "OK" in the browser.
    4. Return to the Cloud Shell editor.
    5. If necessary, reconnect with the Cloud SQL instance:
      gcloud sql connect acg-db --user=root
      
    6. Verify the new value has been entered into the database table:
      USE content;
      SELECT * from books;
      

The Cloud Content team comprises subject matter experts hyper focused on services offered by the leading cloud vendors (AWS, GCP, and Azure), as well as cloud-related technologies such as Linux and DevOps. The team is thrilled to share their knowledge to help you build modern tech solutions from the ground up, secure and optimize your environments, and so much more!

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.

Provided environment for hands-on practice

We will provide the credentials and environment necessary for you to practice right within your browser.

Guided walkthrough

Follow along with the author’s guided walkthrough and build something new in your provided environment!

Did you know?

On average, you retain 75% more of your learning if you get time for practice.

Start learning by doing today

View Plans