How to Implement and Use ANSI C in an Existing OpenWhisk Service
Overview
Integrating ANSI C into an Apache OpenWhisk service allows developers to create high-performance serverless functions using the C language. This guide explains how to implement and use ANSI C in OpenWhisk, leveraging Docker-based runtimes.
Prerequisites
- Apache OpenWhisk Deployment: Ensure OpenWhisk is deployed on Kubernetes or another supported platform. Follow deployment instructions[4].
- Docker Runtime for OpenWhisk: Install OpenWhisk’s Docker runtime to support custom languages[9].
- ANSI C Environment: Install a C compiler (e.g., GCC) and ensure you can compile and test C programs locally.
Steps to Implement ANSI C in OpenWhisk
1. Create an ANSI C Program
Write your ANSI C function code. For example, create a file named example.c
:
#include
int main(int argc, char *argv[]) {
printf("This is an example log message from an arbitrary C program!\n");
printf("{ \"msg\": \"Hello from arbitrary C program!\", \"args\": \"%s\" }",
(argc == 1) ? "undefined" : argv[1]);
return 0;
}
Compile the program locally using:
gcc -o example example.c
2. Prepare a Docker Image for the ANSI C Runtime
Create a custom Docker image that includes the compiled ANSI C program. Use the following Dockerfile
:
FROM ubuntu:20.04
RUN apt-get update && apt-get install -y gcc libc-dev
COPY example /usr/local/bin/example
ENTRYPOINT ["/usr/local/bin/example"]
Build and tag the Docker image:
docker build -t openwhisk-ansi-c-runtime .
3. Deploy the Docker Runtime in OpenWhisk
Push the Docker image to a container registry accessible by your Kubernetes cluster:
docker tag openwhisk-ansi-c-runtime /openwhisk-ansi-c-runtime
docker push /openwhisk-ansi-c-runtime
Update your OpenWhisk deployment to include this runtime by modifying its runtime configuration (runtime.json
)[9].
4. Register the Action in OpenWhisk
Use the wsk
CLI to create an action using the custom runtime:
wsk -i action create ansiCAction --docker /openwhisk-ansi-c-runtime --web true
5. Invoke the Action
Test your action using wsk
CLI or via its web URL:
wsk -i action invoke ansiCAction --result --param args "test"
Or access it via its URL:
curl -X POST -d '{"args": "test"}'
Benefits of Using ANSI C in OpenWhisk
- Performance: ANSI C provides faster execution compared to interpreted languages.
- Flexibility: Custom runtimes allow tailored solutions for specific use cases.
- Integration: Seamlessly integrates with other OpenWhisk features like triggers and rules[5].
Additional Notes
- For complex applications, consider integrating external libraries like OPC UA ANSI C Stack for advanced functionalities[6][7].
- Monitor resource usage when deploying high-performance functions with Docker-based runtimes.
By following this guide, you can leverage ANSI C to build efficient serverless functions within Apache OpenWhisk!
Citations:
[1] https://blog.zhaw.ch/splab/2019/03/15/building-a-sample-mqtt-based-application-on-openwhisk/
[2] https://dalelane.co.uk/blog/?p=3741
[3] https://norma.ncirl.ie/5963/1/ankitkumar.pdf
[4] https://www.robertobandini.it/2020/12/13/apache-openwhisk-hello-world/
[5] https://www-inf.telecom-sudparis.eu/COURS/CSC5004/practicals/basics-openwhisk.pdf
[6] https://github.com/linshenqi/UA-AnsiC
[7] https://stackoverflow.com/questions/47394623/client-implementation-using-the-opc-foundation-ansi-c-stack
[8] https://opcfoundation.org/forum/opc-ua-implementation-stacks-tools-and-samples/ansi-c-sample-applications/
[9] https://github.com/apache/openwhisk-runtime-docker/blob/master/sdk/docker/example.c