This repository contains OpenWhisk function templates for writing serverless functions in the Gleam programming language.
-
Make sure OpenWhisk has been deployed to your Kubernetes cluster and the OpenWhisk CLI tool has been installed. See here and here for a brief introduction on how to do this.
-
Download the Gleam function templates from this repo and enter into the main directory:
git clone https://github.com/nicklasxyz/gleam_openwhisk && \
cd gleam_openwhisk
- Add new functionality to the function that is going to be deployed and managed by OpenWhisk:
vim template/function/src/function.gleam
# ... Extend or add whatever you want to the 'run' function
Note: The files and directories in the template
directory are a part of a usual Gleam project, but structured specifically for use with OpenWhisk. All new functionality should primarily be implemented as a part of the function
module residing in the gleam_openwhisk/template/function/
directory. Extra dependencies should be added to the rebar.config
file in the root of the template
directory. The project can be compiled and tested locally as usual.
- Build and push the function:
# Define your docker image name and function name below
export FUNC_NAME= # For example: test-function
export IMAGE_NAME= # For example: username/test-function:latest
# Then build and push the docker image to Docker Hub:
docker build template --tag $IMAGE_NAME && docker push $IMAGE_NAME
- Create a package and deploy the serverless function:
wsk -i package create demo && \
wsk -i action create /guest/demo/$FUNC_NAME --docker $IMAGE_NAME --web true
# To remove function deployments run:
wsk -i action delete /guest/demo/$FUNC_NAME
- Wait a few seconds, then we can invoke the function by sending a request through curl:
### Retrieve function invocation URL
export FUNC_URL=$(wsk -i action get /guest/demo/$FUNC_NAME --url | tail -1)
### Example POST request:
curl -k \
-d "{\"name\": \"YourNameHere\"}" \
-H "Content-Type: application/json" \
-X POST $FUNC_URL; \
echo
# If nothing was changed in the 'gleam_openwhisk/template/function/src/function.gleam'
# file before deployment then we should just see the default response:
>> {"int_field":42,"string_field":"Hello YourNameHere, from Gleam & OpenWhisk!"}
# The function can also be invoked from the commandline via the OpenWhisk CLI:
wsk -i action invoke /guest/demo/$FUNC_NAME --result --param name YourNameHere
For more information on how GET, POST, PUT and DELETE methods work see the OpenWhisk API gateway docs
The general webserver setup is taken from this repository by gleam-lang.