This migrates from zig 0.13 to 0.15.2. In addition to dealing with breaking changes in the build system and standard library APIs, the architecture was changed substantially. We now build a standalone CLI, and use that to execute the commands. This avoids sandboxing issues related to TLS and enables easier testing. The commit also includes a simple zip implementation (store only, single file) which avoids the platform restriction (i.e. this build can now theoretically work on Windows).
2.4 KiB
2.4 KiB
lambda-zig: A Custom Runtime for AWS Lambda
This is a custom runtime built in Zig (0.15). Simple projects will execute in <1ms, with a cold start init time of approximately 11ms.
Custom build steps are available for packaging and deploying Lambda functions:
zig build awslambda_package: Package the Lambda function into a zip filezig build awslambda_iam: Create or verify IAM role for the Lambda functionzig build awslambda_deploy: Deploy the Lambda function to AWSzig build awslambda_run: Invoke the deployed Lambda function
Build options:
- function-name: Name of the AWS Lambda function
- payload: JSON payload for function invocation (used with awslambda_run)
- region: AWS region for deployment and invocation
- profile: AWS profile to use for credentials
- role-name: IAM role name for the function (default: lambda_basic_execution)
The Lambda function can be compiled for x86_64 or aarch64. The build system automatically configures the Lambda architecture based on the target.
A sample project using this runtime can be found at https://git.lerch.org/lobo/lambda-zig-sample
Using the Zig Package Manager
To add Lambda package/deployment steps to another project:
- Fetch the dependency:
zig fetch --save git+https://git.lerch.org/lobo/lambda-zig
- Update your
build.zig:
const std = @import("std");
const lambda_zig = @import("lambda_zig");
pub fn build(b: *std.Build) !void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
// Get lambda-zig dependency
const lambda_zig_dep = b.dependency("lambda_zig", .{
.target = target,
.optimize = optimize,
});
const exe_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
// Add lambda runtime to your module
exe_module.addImport("aws_lambda_runtime", lambda_zig_dep.module("lambda_runtime"));
const exe = b.addExecutable(.{
.name = "bootstrap",
.root_module = exe_module,
});
b.installArtifact(exe);
// Add Lambda build steps
try lambda_zig.configureBuild(b, lambda_zig_dep, exe);
}
Note: The build function return type must be !void or catch/deal with errors
to support the Lambda build integration.