DEV Community

loading...
Cover image for #30DaysOfAppwrite : Appwrite Cloud Functions
Appwrite

#30DaysOfAppwrite : Appwrite Cloud Functions

Christy Jacob
・7 min read

Intro

#30DaysOfAppwrite is a month long event focused at giving developers a walk through of all of Appwrite's features, starting from the basics to more advanced features like Cloud Functions! Alongside we will also be building a fully featured Medium Clone to demonstrate how these concepts can be applied when building a real world app. We also have some exciting prizes for developers who follow along with us!

Appwrite Cloud Functions

Welcome to Day 23 πŸ‘‹

If you're familiar with the world of serverless you probably already know what Cloud Functions are. For those of you who don't, think of cloud functions as a piece of code that is stateless and can execute independently without the need for you to manage servers. If you've used AWS Lambdas or similar offerings, you will feel right at home with Appwrite Cloud Functions. Appwrite supports over 13 different runtimes for languages like python, deno, dotnet with many more coming up!

Today, we will walk you through the Functions Dashboard in the Appwrite Console and learn how to create and deploy functions.

Cloud Functions in Appwrite can be triggered in 3 ways

  • REST API - You can use any HTTP client or our SDKs to create and trigger cloud functions.
  • Events - Appwrite emits events when certain actions occur at in the server, like creation of a user, creation of a document and many more. You can configure a function to listen to these events. You can learn more about all the system events in our documentation
  • CRON Schedule - You can also configure your functions to trigger based on a CRON Schedule.

In the following example, we will cover the REST API trigger.

Deploying a function using the Console

We will make use of a python hello-world function for the purposes of this demo. We will first use the Appwrite Console to create and execute the function, and then perform the same steps using our CLI 🀩 . We hope you have Appwrite setup already. If not, we have an easy getting started guide explaining how.

πŸ’» Create Your Function

Once you have Appwrite up and running, head over to the Functions section and click Add Function. In the following dialog, give your function a name and choose the Python 3.9 runtime.

Create Function Dialog

Let's now explore the function runtime that was just created for us.

βš™οΈ Settings

Settings

This is where you can configure all aspects of your function.

Field Description
Name Your function Name
Execute Access Manage who can execute this function using permissions
Timeout (seconds) Limit the execution time of your function to prevent abuse
Events The Events which trigger this function
Schedule (CRON Syntax) Set a CRON Schedule to execute this function
Variables Securely store secrets and other values using the environment variables

πŸ“Š Monitors

Here, you'll be able to find some useful information about your functions executions and some usage metrics like CPU time, executions, errors etc.
Monitors

πŸ“‘ Logs

This is where you can check all your execution logs. You can also inspect your function's outputs to stdout and stderr .

Logs

Now that we're familiar with the dashboard, let's go ahead and create a tag for the function we just created.

✍️ Create a tag

Once the function is created, it's time to upload a new tag. Think of a tag a a version of your function. For this, we will use one of our demo functions.

$ git clone https://github.com/appwrite/demos-for-functions
$ cd python/hello-world
$ cat function.py

print("Hello World, I'm an Appwrite cloud function written in Python.")
Enter fullscreen mode Exit fullscreen mode

This is a simple python function that simply prints a string to stdout. As you might have noticed, we don't have any dependencies in this example so you can skip to the next section. But if you do, you will need one more step before packaging your function. In order to ensure faster function execution times, we don't pull any dependencies at runtime. The dependencies need to be packaged along with the function.

You need to ensure that all your dependencies are listed in your requirements.txt file. You then need to run this simple command to fetch all the dependencies and save all of them in a local .appwrite directory.

PIP_TARGET=./.appwrite pip install -r ./requirements.txt --upgrade --ignore-installed
Enter fullscreen mode Exit fullscreen mode

We have similar installation instructions for other runtimes which you can checkout in our documentation.

πŸ“¦ Packaging the cloud function

Once your dependencies (if any) are installed, its now time to package your function. Create a tar file of your function using

$ cd ../
$ tar -zcvf code.tar.gz hello-world

a hello-world
a hello-world/function.py
Enter fullscreen mode Exit fullscreen mode

Now, head back to your function in the Dashboard and click Deploy Tag. In the subsequent dialog, select the Manual Tab.
You will need to provide an entry point command which essentially is the command that executes your function. In our case, this is python function.py. Next, upload the tar file that we just created. Double check your selection and select Create.

Deploy Tag

βœ… Activate and Execute

Once you create the function, you need to Activate the tag. Great, the tag is now activated and you can now execute the function. Click on Execute Now. In the dialog that pops up, you will be asked to enter any custom data that you would like to pass to your function. You can leave this empty and proceed with the execution.

You can now head over to the Logs tab and examine the output of our function!
Execution Logs

Deploying a function using the CLI

In this section, we will learn how you can deploy the hello-world function using the Appwrite CLI. If you haven't installed the CLI yet, you can take a look at our guide to quickly get started. Make sure you have run appwrite init and your API key has the following scopes before we proceed.

  • functions.read
  • functions.write
  • execution.read
  • execution.write

πŸ’» Create your function

You can create a function using the create command of the functions service.

$ appwrite functions create --name=test --execute="*" --env=python-3.9

$id : 60a285e91b4eb # This is your functionId
$permissions :
name : test
dateCreated : 1621263849
dateUpdated : 1621263849
status : disabled
env : python-3.9
tag :
vars : {}
events : {}
schedule :
scheduleNext : 0
schedulePrevious : 0
timeout : 15
Enter fullscreen mode Exit fullscreen mode

This command takes 3 parameters

  • name - A name for your function.
  • execute - A string that manages access to execute this function. In our case, we use the wildcard permission (*) allowing anyone to execute this function. You can read more about all the available permission strings in our documentation.
  • env - Your function runtime.

✍️ Create a tag

The next step is for us to create a new tag. Think of a tag as a new version/revision of your function. We will make use of the createTag command from the functions service.

$ appwrite functions createTag --code=. --functionId=60a285e91b4eb --command='python function.py'

$id : 60a2a9f9e1f1b # this is your tag id
functionId : 60a2a9ef5f63f # functionId
dateCreated : 1621273081
command : python function.py
size : 221
Enter fullscreen mode Exit fullscreen mode

This command takes in 3 parameters

  • code - The path to your cloud function. Assuming you're still in the demos-for-functions/python/hello-world, you can use . to represent the current directory. Note that references to parent paths like ../../directory are not allowed due to restrictions imposed by docker volume mounts.
  • functionId - The id of your function. In our case, the $id attribute from the previous response.
  • command - Your entry point command.

βœ… Activate the tag

A function can have multiple tags, so in this step we need to activate the tag that you just created. You can do this using the updateTag command from the functions service.

$ appwrite functions updateTag --functionId=60a2a9ef5f63f --tag=60a2a9f9e1f1b

$id : 60a2a9ef5f63f
$permissions :
name : test
dateCreated : 1621273071
dateUpdated : 1621273071
status : disabled
env : python-3.9
tag : 60a2a9f9e1f1b
vars : {}
events : {}
schedule :
scheduleNext :
schedulePrevious : 0
timeout : 15
Enter fullscreen mode Exit fullscreen mode

This command takes in 2 parameters

  • functionId - The id of your function. In our case, the $id attribute from the previous response.
  • tag - The $id of the tag you just created.

πŸš€ Run your function

You can now run your function by creating an execution using the createExecution command of the functions service. This command takes in just one parameter which is your functionId which you are already familiar with.

$ appwrite functions createExecution --functionId=60a2a9ef5f63f

$id : 60a2aa86aa46a # executionId
functionId : 60a2a9ef5f63f
dateCreated : 1621273222
trigger : http
status : waiting
exitCode : 0
stdout :
stderr :
time : 0
Enter fullscreen mode Exit fullscreen mode

πŸ₯³ Get the output

In order to get the result of the execution, you can use the getExecution command from the functions service.

$ appwrite functions getExecution --functionId=60a2a9ef5f63f --executionId=60a2aa86aa46a

$id : 60a2aa86aa46a
functionId : 60a2a9ef5f63f
dateCreated : 1621273222
trigger : http
status : completed
exitCode : 0
stdout : Hello World, I'm an Appwrite cloud function written in Python.

stderr :
time : 0.2251238822937
Enter fullscreen mode Exit fullscreen mode

This command takes in 2 parameters

  • functionId - The id of your function.
  • executionId - The execution id from the previous command.

Perfect! You have just created and executed your first cloud function! You can explore our demos-for-functions repository for more cool examples and use cases of Cloud Functions.

Credits

We hope you liked this write up. You can follow #30DaysOfAppwrite on Social Media to keep up with all of our posts. The complete event timeline can be found here

Feel free to reach out to us on Discord if you would like to learn more about Appwrite, Aliens or Unicorns πŸ¦„. Stay tuned for tomorrow's article! Until then πŸ‘‹

Discussion (3)

Collapse
asies23 profile image
Asiesjhot Singh Chandowk

Does Appwrite support a Golang runtime ?

Collapse
christyjacob4 profile image
Christy Jacob Author

Not yet. But we have it in the works! 😊

Collapse
asies23 profile image
Asiesjhot Singh Chandowk

That sounds great 😊