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

  • Linux, macOS or Windows with WSL2
  • Python 3.10 – can be downloaded from https://www.python.org/
  • 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.

🚧

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.
  2. Change the directory to backend-component-workspace.
  3. Create the file .accesstoken and add an access token (when building separately from a UI Component):
    1. Create the token by following Request a bearer token.
    2. Copy the file from your UI Component project after running npx cdk login.
  4. Run the command make run.
$ make run
ixoncdkingress - INFO - ixoncdkingress dev
ixoncdkingress - INFO - wsgiref listening on http://127.0.0.1:8020/
ixoncdkingress - INFO - CBC_PATH: ./functions
ixoncdkingress - INFO - config.context_config: {}
  1. Open your browser and go to the http://127.0.0.1:8020/ page. It should look like the image below.
  2. Enter functions.hello in the Function field and click the Run function button.

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

400

Your first function

Let’s create a function that lets us greet either the world or a specific person if we want.
It is mandatory to add the @FunctionContext.expose decorator to functions that you want to use in IXON Cloud.

  1. In the tutorial/backend/functions directory, create the file functions.py and open it in your editor.
  2. Add the following lines:
from ixoncdkingress.function.context import FunctionContext

@FunctionContext.expose
def hello(context, parameter = "World"):
  return {"message": "Hello " + parameter}
  1. Restart the test environment by running make run again.
  2. Go back to http://127.0.0.1:8020/.
  3. Enter functions.hello in the Functions field and click Run function.

You should see the following result:

{ "message": "Hello World" }

Change the function

Now let’s try to greet someone else.

  1. In the first row of the table below Function input enter parameter under Key and Developer under Value.
400
  1. Run the function again.

It now says Hello 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.

The workspace also comes with a sample function. Try calling this function too. Python names its functions based on the file path, file name and function name, separated by a period (.).