lambda_runtime

Version, currently main branch1 version
  • main branchlatestJun 3, 2022

github.com/aws-samples/lambda-crystal-runtime

Custom lambda runtime for Crystal

7 stars
1 dependent
License: MIT-0

Installation

# Add this to your shard.yml
dependencies:
  lambda_runtime:
    github: aws-samples/lambda-crystal-runtime
    branch: main

main is a branch, not a release, so this tracks it rather than pinning a version.

Then run:

shards install

shard.yml

Crystal
no constraint declared
License
MIT-0
Author
Taichiro Suzuki

Dependencies

This version declares no dependencies.

README

Custom AWS Lambda runtime for Crystal

We will show you how to use AWS Lambda's Custom Runtime feature to execute Crystal's code. This project can be utilized as a library, but we recommend that you do NOT use it for production workloads as it is only sample code. Check lambda-crystal-runtime-example to know how to implement the runtime.

Step-by-Step Guide

1. Build a docker image

First, create a Docker image to be used as a builder. Clone this repository and execute the following command

docker build -t crystal-lambda-builder .

2. Add a dependency

Add this repository to dependency by adding the following statement to shard.yml

dependencies:
  lambda_runtime:
    github: aws-samples/lambda-crystal-runtime

Then run shards install to install.

3. Write Lambda function

Write a Lambda function in Crystal. Create src/example.cr and make the following contents.

require "lambda_runtime"

lambda_event_loop do |event_body|
  puts event_body

  "ok"
end

This function prints the received event_body and returns the string ok. It is very simple code.

4. Build Crystal code

Add the following statement to shard.yml.

targets:
  example:
    main: src/example.cr

Instead of doing a shards build like a normal Crystal project, run the following command

docker run -v $(pwd):/lambda-src crystal-lambda-builder

After successful execution, the lambda directory will be created, containing a zip file called example.zip. You can then create a function from the Lambda management console, upload the zip file as source code, and it is ready to run.

Security

See CONTRIBUTING for more information.

License

This library is licensed under the MIT-0 License. See the LICENSE file.