Custom runtime for lambda - example in main.zig
Go to file
Emil Lerch 53981da7c1
All checks were successful
Generic zig build / build (push) Successful in 51s
update readme as caching now works
2024-08-28 15:23:32 -07:00
.github/workflows upgrade to zig 0.13.0 2024-06-08 10:01:40 -07:00
lambdabuild implement package caching 2024-08-28 15:15:19 -07:00
src fix test 2024-05-10 13:28:30 -07:00
.gitignore out with AWS cli/in with the SDK 2024-08-27 16:29:43 -07:00
build.zig upgrade to zig 0.13.0 2024-06-08 10:01:40 -07:00
build.zig.zon out with AWS cli/in with the SDK 2024-08-27 16:29:43 -07:00
lambdabuild.zig implement package caching 2024-08-28 15:15:19 -07:00
LICENSE add license 2021-10-04 08:14:01 -07:00
README.md update readme as caching now works 2024-08-28 15:23:32 -07:00

lambda-zig: A Custom Runtime for AWS Lambda

This is a sample custom runtime built in zig (0.13). Simple projects will execute in <1ms, with a cold start init time of approximately 11ms.

Some custom build steps have been added to build.zig, which will only currently appear if compiling from a linux operating system:

  • zig build awslambda_iam: Deploy and record a default IAM role for the lambda function
  • zig build awslambda_package: Package the lambda function for upload
  • zig build awslambda_deploy: Deploy the lambda function
  • zig build awslambda_run: Run the lambda function

Custom options:

  • function-name: set the name of the AWS Lambda function
  • payload: Use this to set the payload of the function when run using zig build awslambda_run
  • region: Use this to set the region for the function deployment/run
  • function-role: Name of the role to use for the function. The system will look up the arn from this name, and create if it does not exist
  • function-arn: Role arn to use with the function. This must exist

The AWS Lambda function can be compiled as a linux x86_64 or linux aarch64 executable. The build script will set the architecture appropriately

Caveats:

  • Building on Windows will not yet work, as the package step still uses system commands due to the need to create a zip file, and the current lack of zip file creation capabilities in the standard library (you can read, but not write, zip files with the standard library). A TODO exists with more information should you wish to file a PR.

A sample project using this runtime can be found at https://git.lerch.org/lobo/lambda-zig-sample

Using the zig package manager

The zig package manager works just fine in build.zig, which works well for use of this runtime.

To add lambda package/deployment steps to another project:

  1. zig build init-exe
  2. Add a build.zig.zon similar to the below
  3. Add a line to build.zig to add necessary build options, etc. Not the build function return type should be changed from void to !void

build.zig:

try @import("lambda-zig").lambdaBuildOptions(b, exe);

build.zig.zon:

.{
    .name = "lambda-zig",
    .version = "0.1.0",
    .dependencies = .{
        .@"lambda-zig" = .{
            .url = "https://git.lerch.org/lobo/lambda-zig/archive/fa13a08c4d91034a9b19d85f8c4c0af4cedaa67e.tar.gz",
            .hash = "122037c357f834ffddf7b3a514f55edd5a4d7a3cde138a4021b6ac51be8fd2926000",
        },
    },
}

That's it! Now you should have the 4 custom build steps