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.
- Docker Runtime for OpenWhisk: Install OpenWhisk’s Docker runtime to support custom languages.
- 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).
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.
- 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!