Introduction: Why C++ in Serverless?
The world of serverless computing is strongly associated with JavaScript (Node.js), Python, or Java. However, for tasks critical to performance and memory consumption, C++ becomes an ideal candidate. AWS Lambda supports C++ through the Native Runtime, allowing you to run binary files with minimal overhead.
In this article, we will break down how to write, compile, and deploy a C++ function on AWS Lambda. You will learn about the advantages of C++ in serverless architecture, environment setup, and cold start optimization.
1. Advantages of C++ over Python and Node.js
Performance and Cold Start
The main drawback of interpreted languages is the delay during cold start. A Lambda container with Python can initialize in 200-500 ms, while with C++ it takes less than 50 ms. This is critical for APIs with high latency requirements.
- Low memory consumption: A C++ binary takes ~1-3 MB compared to 20+ MB for Node.js with dependencies.
- Direct access to system resources: Working with sockets, files, and memory without a VM layer.
- Cost savings: Due to fast execution, you pay less for milliseconds of operation.
2. Setting Up the Environment for C++ on AWS Lambda
To compile C++ code for Lambda, you need to use Amazon Linux 2 (or a compatible image). The recommended approach is a Docker container with the official Lambda image.
Requirements
- CMake ≥ 3.15
- GCC 7.3+ or Clang 7+
- AWS SDK for C++ (optional, for working with S3/DynamoDB)
- Docker (for building in an environment identical to Lambda)
Basic Dockerfile for Building
FROM public.ecr.aws/lambda/provided:al2
# Install build toolsRUN yum install -y gcc-c++ cmake3 make
# Copy source filesCOPY . /codeWORKDIR /code/build
# Build static binaryRUN cmake3 .. -DCMAKE_BUILD_TYPE=Release \ -DCMAKE_CXX_FLAGS="-static-libstdc++ -static-libgcc" \ && make -j$(nproc)
# Copy result to Lambda directoryRUN cp handler /var/task/bootstrapCMD ["handler"]3. Writing a Lambda Function in C++
Unlike Python, where the handler is a function, in C++ we create an executable file that Lambda runs as a process. Input data is passed through stdin, and the result is output to stdout.
Simple Example: JSON Processing
#include <iostream>#include <string>#include <nlohmann/json.hpp> // Library for working with JSON
using json = nlohmann::json;
int main() { // Read entire stdin (input JSON from Lambda) std::string input; std::getline(std::cin, input);
try { auto event = json::parse(input);
// Extract parameters std::string name = event.value("name", "World"); int number = event.value("number", 0);
// Form response json response = { {"message", "Hello, " + name + "!"}, {"computed", number * 2}, {"language", "C++"} };
std::cout << response.dump() << std::endl; return 0; } catch (const std::exception& e) { std::cerr << "{\\"error\\":\\"" << e.what() << "\\"}" << std::endl; return 1; }}CMakeLists.txt Structure
cmake_minimum_required(VERSION 3.15)project(lambda-handler)
set(CMAKE_CXX_STANDARD 17)set(CMAKE_CXX_STANDARD_REQUIRED ON)
# Static linking for portabilityset(CMAKE_EXE_LINKER_FLAGS "-static-libstdc++ -static-libgcc")
# Include nlohmann/json (vcpkg or fetch)find_package(nlohmann_json REQUIRED)
add_executable(handler main.cpp)target_link_librar