Developing a Cloud Function

In this tutorial we’ll create a new Cloud Function and test it in isolation. A basic understanding of Python is assumed.

What you need

📘

Note:

If you are using WSL on Windows, make sure to install Python 3.10 or higher on it. You can find out how to do so in the Installing Python3 in WSL section.

  • Docker – can be downloaded from https://www.docker.com/.
  • Code editor – it is recommended to use one that understands Python, such as Microsoft Visual Studio Code or JetBrains PyCharm Community.

Getting started

We’ll start by setting up a development environment for your new project.

❗️

Do not edit the Makefile!

Please do not make any changes to the Makefile as it is not intended to be changed.

  1. Download the workspace from Github as a ZIP file and extract it. Please note: do NOT clone the workspace, as it could cause Git-related issues!

  2. Navigate to the backend-component-workspace directory in your File Explorer.

  3. Create the file .accesstoken and add an access token (when building separately from a UI Component; if you build a UI Component instead, the npx cdk loginwill already do that for you):

  4. Run the command make run. If this command returns an error message concerning installable modules, follow the steps below in the Steps to install the right .venv:

$ make run
# Update your package list
sudo apt update

# Install the required .venv package
sudo apt install python3.12-venv

# Clean up the old, failed attempt by removing the broken venv directory
rm -rf venv

# Run the original command again
make run
🚧

Note for Windows Users

If you are a Windows user, make sure to run any make commands in the WSL terminal opened on VS Code (or other editors).

  1. Open your browser and go to the http://127.0.0.1:8020/ page. It should look like the image below.
  2. Python names its functions based on the file path, file name and function name, separated by a full stop (.). Enter functions.example.greet in the Function field and click the Run function button, located before Function output.

Sadly, it will return an error message, because we haven’t created the function yet!

How to create your first function

Let’s test out a function that lets us greet either a person or someone else specifically, if we want.

With ixoncdkingress v0.0.21, it is now possible to use the context_values.yaml file to set up fields that we will be able to access directly from the FunctionContext. Follow these steps to

  1. Change the usernameand password, we have changed the values of usernameand password in serviceAccount
  2. In the functions directory, you will find a file called example.py. Open it in your editor;
  3. This is what you will see:
from ixoncdkingress.function.context import FunctionContext

@FunctionContext.expose
def greet(context: FunctionContext, name: str = 'person'):
    """
    A simple example function that greets someone.
    """
    return f'Greetings {name}, my username is {context.config["serviceAccount"]["username"]}!'
  1. Restart the test environment by running make run again;
  2. Go back to http://127.0.0.1:8020/;
  3. Enter functions.example.greet in the Functions field and click Run function.

You should see the following result:

"Greetings person!"

Change the function

Now let’s try to greet someone else.

  1. In the first row of the table below Function input enter name under Key and Developer under Value:
  1. Run the function again and take a look at the output:

It now says Greetings Developer! instead, fantastic! We now have all the ingredients for a Cloud Function.

Things to try out from here

Try saying Hello to others. See what happens if you change the Key to a different value, and how you can make the Cloud Function work with that other value.