Why C++ in the Serverless World?
\n\nWhen you hear "serverless," the first things that come to mind are JavaScript, Python, or Go. But what if I told you that you can run C++ in AWS Lambda, Google Cloud Functions, and even on custom runtimes? And this isn't just a hack, but a perfectly valid scenario for tasks where every millisecond counts.
\n\nC++ in serverless is about speed and cold starts. While a Python function loads the interpreter and dependencies, C++ has already finished executing. But there are nuances: you'll need to compile a binary for Linux, package it into a ZIP, and specify a custom runtime. Sounds complicated? Actually, it's not. Let's break it down.
\n\nHow Does Serverless Work for C++?
\n\nThe thing is, cloud providers don't natively support compiled languages. AWS Lambda, for example, officially only supports Python, Node.js, Java, Go, .NET, and Ruby. But Lambda has a Custom Runtime mechanism — you can provide any binary that communicates with the Lambda Runtime API via HTTP.
\n\nThe scheme is simple:
\n- \n
- You compile your C++ code for Amazon Linux 2 (or a compatible environment). \n
- You package the executable file along with a
bootstrapscript (or the binary itself, renamed tobootstrap). \n - You upload the ZIP to Lambda and select "Provide your own bootstrap." \n
The same can be done with Google Cloud Functions (via Cloud Run or Knative) and with Azure Functions (via a Docker image).
\n\nBuilding a Simple Lambda Function in C++
\n\nLet's write a function that takes a JSON with two numbers and returns their sum. It's a classic, but in C++.
\n\n// main.cpp\n#include <iostream>\n#include <string>\n#include <nlohmann/json.hpp>\n\nusing json = nlohmann::json;\n\nint main() {\n // Read the event from stdin (Lambda passes it this way)\n std::string input;\n std::getline(std::cin, input);\n\n auto event = json::parse(input);\n\n int a = event["a"];\n int b = event["b"];\n int sum = a + b;\n\n json result;\n result["statusCode"] = 200;\n result["body"] = std::to_string(sum);\n\n // Output to stdout — this is the Lambda response\n std::cout << result.dump() << std::endl;\n\n return 0;\n}\n\n\nFor compilation, we'll need the g++ compiler and the nlohmann/json library (download the header). We compile statically to avoid dragging .so files:
g++ -std=c++17 -static -O2 main.cpp -o bootstrap\n\n\nNow create a ZIP archive with a single bootstrap file and upload it to Lambda. That's it! The function is ready.
Error Handling and Logging
\n\nIn a real project, you need to properly handle JSON parsing errors and return a clear response. AWS Lambda expects your binary to exit with code 0 on success and a non-zero code on error. Logs can be written to stderr — they will appear in CloudWatch.
if (!event.contains("a") || !event.contains("b")) {\n std::cerr << "Missing parameters" << std::endl;\n json error;\n error["statusCode"] = 400;\n error["body"] = "Bad Request";\n std::cout << error.dump() << std::endl;\n return 1;\n}\n\n\nAlternatives to AWS Lambda for C++
\n\nDon't want to bother with Custom Runtime? There are other options.
\n\nGoogle Cloud Run + Knative
\n\nYou can package your C++ application into a Docker image and run it on Cloud Run. This isn't exactly a "function," but rather container-based serverless, though for C++ it's the simplest path. Cold starts will be longer (seconds), but there are no runtime restrictions.
\n\nAzure Functions with Docker
\n\nSimilarly — use Docker and any image with a C++ runtime. Azure supports custom handlers via custom handlers.