Skip to content

🟠 Write AWS Lambda functions in Zig programming language

License

Notifications You must be signed in to change notification settings

vytskalt/aws-lambda-zig

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AWS Lambda Runtime for Zig

Zig v0.13 MIT License

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.

Features

  • Runtime API
  • Extensions API
  • Telemetry API
  • CloudWatch & X-Ray integration
  • Response streaming
  • Life-cycle hooks
  • Layers
  • Structured events
  • Managed build step

Benchmark

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)

Usage

Setup

  1. Add this package as a dependency to your project.
  2. Import the aws-lambda module in your build.zig script.

Minimal Code

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!";
}

Distribute

  1. Build for Linux with aarch64 (neoverse_n1+neon) or x86_64 (+avx2) architecture.
  2. Name the executable bootstrap.
  3. Archive the executable into a zip.
  4. 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.

Demos

Hello World

Returns a short message.

zig build demo:hello --release

Echo

Returns the provided payload.

zig build demo:echo --release

Debug

Returns the function’s metadata, environment variables and the provided payload.

🛑 May expose sensative data to the public.

zig build demo:debug --release

Fail: Handler Error

Always returns an error; the runtime logs the error to CloudWatch.

zig build demo:fail --release

Fail: Oversized Output

Returns an output larger than the Lambda limit; the runtime logs an error to CloudWatch.

zig build demo:oversize --release

Response Streaming

Stream a response to the client.

👉 Be sure to configure the function with streaming enabled.

zig build demo:stream --release

Response Streaming: Fail

Stream a response to the client and eventually fail.

👉 Be sure to configure the function with streaming enabled.

zig build demo:stream_throw --release

License

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.

Acknowledgments

About

🟠 Write AWS Lambda functions in Zig programming language

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Zig 100.0%