aws_lambda_runtime
aws_lambda_runtime
An AWS Lambda runtime for crystal.
While this is a working implementation, it is not tested yet and the API is likely subject to change. Feel free to give it a try, nonetheless!
Installation
-
Add the dependency to your
shard.yml
:dependencies: aws_lambda_runtime: github: lagr/aws_lambda_runtime
-
Run
shards install
Usage
The basic usage which should be suitable for most use cases is as follows:
require "aws_lambda_runtime"
AwsLambdaRuntime.run do |event, invocation|
# do your thing
puts "event is a #{event.class.name}."
# => event is a JSON::Any.
# return something that responds to to_json
{ "serialize-this": "my friend" }
end
This will provide event
as JSON::Any
and invocation
as AwsLambdaRuntime::Invocation
to the block.
Advanced Usage
Passing a Proc, e.g. one derived from a method, permits to bypass the parsing of the event as JSON::Any
:
def handler(event, invocation)
# do your thing
puts "event is a #{event.class.name}."
# => event is a JSON::Any.
# return something that responds to to_json
{ "serialize-this": "my friend" }
end
AwsLambdaRuntime.run(->handler(JSON::Any, AwsLambdaRuntime::Invocation))
Working on the raw String
can be handy to define custom mappings for the expected event data or to defer parsing to a later point in time:
class SQSMessage
include JSON::Serializable
@[JSON::Field(key: "messageId")]
property message_id : String
property attributes : JSON::Any
end
class SQSEvent
include JSON::Serializable
@[JSON::Field(key: "Records")]
property messages : Array(SQSMessage)
end
def handler(event, invocation)
puts "event is a #{event.class.name}."
sqs_event = SQSEvent.from_json(event)
{ "first-message-attributes": sqs_event.messages.first.attributes }
end
AwsLambdaRuntime.run(->handler(String, AwsLambdaRuntime::Invocation))
Development
TODO: :-)
Notable Mentions
This shard is inspired by crambda.
Contributing
- Fork it (https://github.com/lagr/aws_lambda_runtime/fork)
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request
Contributors
- Lars Greiving - creator and maintainer
aws_lambda_runtime
- 3
- 0
- 0
- 0
- 0
- about 4 years ago
- December 7, 2020
MIT License
Mon, 05 May 2025 00:18:20 GMT