Serverless in C++: AWS Lambda, Performance, and Real Code

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Why C++ in the Serverless World?

\n\n

When 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\n

C++ 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\n

How Does Serverless Work for C++?

\n\n

The 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\n

The 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 bootstrap script (or the binary itself, renamed to bootstrap).
  • \n
  • You upload the ZIP to Lambda and select "Provide your own bootstrap."
  • \n
\n\n

The same can be done with Google Cloud Functions (via Cloud Run or Knative) and with Azure Functions (via a Docker image).

\n\n

Building a Simple Lambda Function in C++

\n\n

Let'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\n

For compilation, we'll need the g++ compiler and the nlohmann/json library (download the header). We compile statically to avoid dragging .so files:

\n\n
g++ -std=c++17 -static -O2 main.cpp -o bootstrap\n
\n\n

Now create a ZIP archive with a single bootstrap file and upload it to Lambda. That's it! The function is ready.

\n\n

Error Handling and Logging

\n\n

In 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.

\n\n
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\n

Alternatives to AWS Lambda for C++

\n\n

Don't want to bother with Custom Runtime? There are other options.

\n\n

Google Cloud Run + Knative

\n\n

You 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\n

Azure Functions with Docker

\n\n

Similarly — use Docker and any image with a C++ runtime. Azure supports custom handlers via custom handlers.

\n

Blogs

Book Recommendations