Everything you need to know to get Appwrite Functions works on PHP runtime

Photo by Ben Griffiths on Unsplash

Everything you need to know to get Appwrite Functions works on PHP runtime

Knight's photo
Knight
ยทOct 29, 2022ยท

It took me a few days to figure out how to get the Function feature to work on Appwrite; I almost gave up and looked for some help on Appwrite Github Discussion, but before the Appwrite Maintainer zoomed into my issue, I found out what went wrong.

Let me share with you all the issues I faced so that you don't have to waste a few days like me. I m using the Digital Ocean Marketplace to install my Appwrite instance, which explains why you don't face the same issue as me if you are not using a similar setup. Since I m more like a PHP guy, I m going to share how to get the Appwrite function to work on PHP runtime.

Check your instance setting first

If you are using DO-based Appwrite, check your .env on two settings.

  • _APP_FUNCTIONS_RUNTIMES
  • _APP_FUNCTIONS_ENVS

In my previous post, I mentioned that the DO-based Appwrite is outdated; you might find it for the two above settings still using PHP7.4 as the default PHP runtime. However, based on Appwrite-supported runtime documentation, PHP7.4 is no longer on the list. So update your env setting and restart your docker.

docker compose up -d --remove-orphans

Setup the Appwrite CLI

Once the setting is updated, let's start to build some functions! First, get Appwrite CLI to installed first; it is going to help you deploy your function to your Appwrite instance quickly. Then, follow the steps on Appwrite documentation; the only thing I will need to remind you of is that the endpoint needs to be in the domain with the v1 format.

https://[appwrite instance domain]/v1

One easy way to test your instance to make sure the endpoint is working is to access the below URL using your browser; you should be able to see your Appwrite instance version.

https://[appwrite instance domain]/v1/health/version

During the setup of your Appwrite Cli, You can always use the command to check the config status.

appwrite client --debug

You should have something similar to the screenshot to show that your CLI is working.

CleanShot 2022-10-29 at 22.12.36@2x.png

Write Appwrite function

Once your Appwrite CLI is functioning correctly, let's start some real work.

appwrite init function

This will initiate the function, fill in the function name, and press enters for ID to let it auto-generate and choose the runtime. Once done, you can see an appwrite.json file and a functions folder. Within the functions folder, you can see a new folder with the function name you had to fill in during the initial. There should be an index file sitting under functions > [function name] > src; that's the file you want to edit.

Let's change the index file to something simple to test that the function is working.

require_once 'vendor/autoload.php';

return function($req, $res) {

  $res->json([
    'areDevelopersAwesome' => true
  ]);
};

Once you save the changes, move back to the root folder that has the appwrite.json and continues with this command to deploy the function to your Appwrite instance. The important thing here is to remember to press the space bar to select the function. If you click enter, it will pass through, but the Appwrite instance will never execute the function successfully.

CleanShot 2022-10-29 at 22.42.20@2x.png

appwrite deploy function

As you can once the function is deployed successfully, you should be able to see it on your Appwrite instance.

CleanShot 2022-10-29 at 22.43.21@2x.png

Remember I said you need to select before you deploy; as you can see, those deployments with "scriptFile" are what I deployed without selecting which function; the result is my function keeps failing without telling me what was wrong. CleanShot 2022-10-29 at 14.54.52@2x.png

Finally, click on the execute; you don't need to fill in anything for the custom data and check the log; you should see the function run without any issues.

CleanShot 2022-10-29 at 22.43.39@2x.png

Bonus tips - for those who are not familiar with docker. If you want to check your Appwrite docker executor log file, you can use

docker logs appwrite-executor

or if you need to check your runtime instance log file, you can use

docker ps

to list out all the instances and use docker logs to check the specific log file.

ย 
Share this