C++ Functions Framework
The C++ Functions Framework is an open source library that allows you to write serverless functions in C++ and easily deploy them to Cloud Run.
In this blogpost, we’ll walk through installing the framework and deploying your first serverless C++ function.
Set Up Cloud Shell
For a stable environment with pre-installed tools, let’s use Cloud Shell — an online shell environment for Google Cloud.
Open a full-screen shell here: shell.cloud.google.com/?show=terminal
This environment comes with tools like docker
and pack
, we’ll use these soon. You can verify they are installed with these commands:
docker --version
# Example output: Docker version 20.10.3, build 48d30b5
pack --version
# Example output: 0.17.0+git-d9cb4e7.build-2045
Get the sample code
The C++ Functions Framework includes samples for getting started. Download the framework in your home directory:
cd $HOME
git clone https://github.com/GoogleCloudPlatform/functions-framework-cpp
The rest of this guide will assume you are issuing commands in this repo’s directory:
cd $HOME/functions-framework-cpp
We’ll use the HTTP hello world sample, which looks like this:
Notice the Function Framework’s gcf HTTP request and response classes and nlohmann JSON parsing in the above sample.
Set up the Buildpack
We will be using Cloud Native Buildpacks to create the container image that will be deployed to Cloud Run. The first time your run these commands it can take several minutes, maybe as long as an hour, depending on your workstation’s performance.
Run the following commands:
docker build -t gcf-cpp-develop -f build_scripts/Dockerfile .
docker build -t gcf-cpp-runtime --target gcf-cpp-runtime -f build_scripts/Dockerfile build_scripts
pack builder create gcf-cpp-builder:bionic --config pack/builder.toml
pack config trusted-builders add gcf-cpp-builder:bionic
pack config default-builder gcf-cpp-builder:bionic
Build a Docker Image Locally
After setting up, build a Docker image with your function using the pack
command:
pack build \
--builder gcf-cpp-builder:bionic \
--env FUNCTION_SIGNATURE_TYPE=http \
--env TARGET_FUNCTION=hello_world_http \
--path examples/site/hello_world_http \
gcf-cpp-hello-world-http
If all goes well, you’ll see the following response:
Successfully built image gcf-cpp-hello-world-http
(Optional) Test the container locally
Optionally, you can test Docker container you just created on localhost
. Run the container image we just built:
ID=$(docker run --detach --rm -p 8080:8080 gcf-cpp-hello-world-http)
Then send a HTTP request to your container:
curl http://localhost:8080
# Output: Hello, World!
Nice! When done, you can clean up the container with:
docker kill "${ID}"
Deploy to Cloud Run
To deploy to Cloud Run, we first need to build and push the container to Google Container Registry.
Build the image and push to Container Registry with the following command:
GOOGLE_CLOUD_PROJECT=$(gcloud config get-value project)
pack build \
--builder gcf-cpp-builder:bionic \
--env FUNCTION_SIGNATURE_TYPE=http \
--env TARGET_FUNCTION=hello_world_http \
--path examples/site/hello_world_http \
"gcr.io/$GOOGLE_CLOUD_PROJECT/gcf-cpp-hello-world-http"
Then, deploy to Cloud Run, using the same gcr.io
URL:
gcloud run deploy gcf-cpp-hello-world-http \
--project="${GOOGLE_CLOUD_PROJECT}" \
--image="gcr.io/${GOOGLE_CLOUD_PROJECT}/gcf-cpp-hello-world-http:latest" \
--region="us-central1" \
--platform="managed" \
--allow-unauthenticated
Obtain the URL of the public Run service and send a request via cURL
:
HTTP_SERVICE_URL=$(gcloud run services describe \
--project="${GOOGLE_CLOUD_PROJECT}" \
--platform="managed" \
--region="us-central1" \
--format="value(status.url)" \
gcf-cpp-hello-world-http)curl -H "Authorization: Bearer $(gcloud auth print-identity-token)" "${HTTP_SERVICE_URL}"
You should see the output Hello World!
.
Congrats, you deployed a C++ function to Google Cloud! 🎉
Thanks for Reading!
If you think this project is interesting, give it a star on GitHub and check out the copious amount of examples and docs.