Write AWS Lambda functions in the Zig programming language to achieve blazing fast invocations and cold starts!
Tip
Check out AWS SDK for Zig for a comprehensive Zig-based AWS cloud solution.
- Runtime API
- Extensions API
- Telemetry API
- CloudWatch & X-Ray integration
- Response streaming
- Life-cycle hooks
- Layers
- Structured events
- Managed build step
Using zig allows creating small and fast functions.
Running the basic Echo demo on arm64
(256 MB
, Amazon Linux 2023):
- ❄️
~11ms
cold-start duration - ⚡
~1.5ms
invocation duration - 💾
11 MB
max memory consumption - ⚖️
1.7 MB
function size (zip)
- Add this package as a dependency to your project.
- Import the
aws-lambda
module in yourbuild.zig
script.
const lambda = @import("aws-lambda");
/// The handler’s logging scope.
/// In release builds only _error_ level logs are sent to CloudWatch.
const log = lambda.log;
/// Entry point for the lambda runtime.
pub fn main() void {
lambda.serve(handler);
}
/// Eeach event is processed separetly by this function.
/// The function must have the following signature:
fn handler(
allocs: lambda.Allocators, // Persistant GPA & invocation-scoped Arena.
context: *const lambda.Context, // Function metadata (including env).
event: []const u8, // JSON payload.
) ![]const u8 {
return "Hey there!";
}
- Build for Linux with
aarch64
(neoverse_n1
+neon
) orx86_64
(+avx2
) architecture. - Name the executable
bootstrap
. - Archive the executable into a zip.
- Upload the archive to Lambda (using Amazon Linux 2023 or another OS-only runtime). This shouls work through the console, CLI, SAM or anyCI solution.
Returns a short message.
zig build demo:hello --release
Returns the provided payload.
zig build demo:echo --release
Returns the function’s metadata, environment variables and the provided payload.
🛑 May expose sensative data to the public.
zig build demo:debug --release
Always returns an error; the runtime logs the error to CloudWatch.
zig build demo:fail --release
Returns an output larger than the Lambda limit; the runtime logs an error to CloudWatch.
zig build demo:oversize --release
Stream a response to the client.
👉 Be sure to configure the function with streaming enabled.
zig build demo:stream --release
Stream a response to the client and eventually fail.
👉 Be sure to configure the function with streaming enabled.
zig build demo:stream_throw --release
The author and contributors are not responsible for any issues or damages caused by the use of this software, part of it, or its derivatives. See LICENSE for the complete terms of use.
Note
AWS Lambda Runtime for Zig is not an official Amazon Web Services software, nor is it affiliated with Amazon Web Services, Inc.