IronFunctions and PHP

Posted: 2017-07-12 10:34:17

A colleague of mine Dave Hall poing out IronFunctions. And being in the pursuit of serverless options especially ones that even have examples of PHP I had to give it a try. This post is a note2self in that the intro here has some bits that need fixing BUT I think it is close and I hope to add more updates here about the tool.

All the steps below will only work once you take on the initial hello world here. After that you have the fn cli tool installed and a running local iron-functions server.

For one I needed to apply each patch seen here

Once I had all that in place I re-reran the fn install command from the folder where I download the iron-io/functions repo into

cd ../functions
curl -LSs https://goo.gl/VZrL8t | sh

Then I restarted the service

docker run --rm -it --name functions -v ${PWD}/data:/app/data -v /var/run/docker.sock:/var/run/docker.sock -p 8080:8080 iron/functions

btw you need to have Docker working on your machine. This is so easy now that, for example mac, it should be a gui install.

Ok now back to my example project

cd ../example_php

In here I have several files since the above fix

worker.php
func.yml
Dockerfile

the cat hello.payload.php in the example docs did not work so I used curl and POST instead but this is not what I want to do I want to test this from the CLI first so will come back to that later

This folder has a composer.json file. Just run composer init since it just needs to be a basic install of PHP.

worker.php

<?php
require 'vendor/autoload.php';
stream_set_blocking(STDIN, 0);
$payload = json_decode(file_get_contents("php://stdin"), true);
if (isset($payload['name'])) {
    var_dump($payload);
    echo "Hello 2 ", $payload['name'],"!\n\n";
} else {
    var_dump($payload);
    echo "Hello World 2!\n\n";
}

func.yml

name: alnutile/hello
version: 0.0.7
runtime: php
entrypoint: ./func
build:
- docker run --rm -v "$PWD":/worker -w /worker iron/php:dev composer install

Dockerfile

FROM iron/php

WORKDIR /app
ADD . /app

ENTRYPOINT ["php", "worker.php"]

Then as the docs note https://github.com/iron-io/functions/tree/master/examples/hello/php I do all the install steps.

Once those are done ran curl to see it work:

>curl -X POST http://localhost:8080/r/phpapp1/hello -d '{ "name": "bar" }'

Which results in:

array(1) {
  ["name"]=>
  string(3) "bar"
}
Hello 2 bar!

For updates I added one more file update.sh

#!/bin/sh
# update a function with a new version and push it
fn bump && fn build && fn push
# then update the route
fn routes update phpapp1 /hello

just to save some time there.

More soon on using this with Lumen and other options. I tried this but some PHP items are missing on the Alpine docker image to make this work.