Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,17 @@ You can include this as a dependency in your project in `shards.yml` file

```
dependencies:
lambda_builder:
lambda:
github: spinscale/crystal-aws-lambda
branch: master
```

Now run the the `shards` command to download the dependency. You can now create your own lambda handlers like this


```crystal
require "lambda_builder"
require "lambda"

runtime = Lambda::Builder::Runtime.new
runtime = Lambda::Runtime.new

runtime.register_handler("httpevent") do |input|
req = Lambda::Builder::HTTPRequest.new(input)
Expand Down Expand Up @@ -131,7 +130,6 @@ sls deploy

This will start a sample runtime, that includes a HTTP endpoint, a scheduled event and an SQS listening event.


## Contributing

1. Fork it (<https://github.com/spinscale/crystal-aws-lambda/fork>)
Expand Down
2 changes: 1 addition & 1 deletion example/shard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ authors:
- Alexander Reelsen <alexander@reelsen.net>

dependencies:
lambda_builder:
lambda:
github: spinscale/crystal-aws-lambda
branch: master

Expand Down
4 changes: 2 additions & 2 deletions example/src/bootstrap.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "lambda_builder"
require "lambda"

runtime = Lambda::Builder::Runtime.new
runtime = Lambda::Runtime.new

Log.define_formatter(
LambdaFormatter,
Expand Down
2 changes: 1 addition & 1 deletion shard.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: lambda_builder
name: lambda
version: 0.1.1

authors:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require "../spec_helper"
require "../../spec_helper"

describe Lambda::Builder::HTTPRequest do
it "parses properly" do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require "../spec_helper"
require "../../spec_helper"

describe Lambda::Builder::HTTPResponse do
it "always returns status code" do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def mock_next_invocation(body : String)
.to_return(status: 200, body: body, headers: {"Lambda-Runtime-Aws-Request-Id" => "54321", "Lambda-Runtime-Trace-Id" => "TRACE-ID", "Content-Type": "application/json"})
end

describe Lambda::Builder::Runtime do
describe Lambda::Runtime do
io = IO::Memory.new

Spec.before_each do
Expand All @@ -18,21 +18,21 @@ describe Lambda::Builder::Runtime do

it "can read the runtime API from the environment" do
ENV["AWS_LAMBDA_RUNTIME_API"] = "my-host:12345"
runtime = Lambda::Builder::Runtime.new
runtime = Lambda::Runtime.new
runtime.host.should eq "my-host"
runtime.port.should eq 12345
end

it "should be able to register a handler" do
runtime = Lambda::Builder::Runtime.new
runtime = Lambda::Runtime.new
# handler = do |_input| JSON.parse Lambda::Builder::HTTPResponse.new(200).to_json end
runtime.register_handler("my_handler") do |_input|
JSON.parse(%q({ "foo" : "bar"}))
end
runtime.handlers["my_handler"].should_not be_nil
end

it "can run with an event" do
it "can run with a JSON::Any handler" do
body = %Q({ "input" : { "test" : "test" }})
mock_next_invocation body

Expand All @@ -42,7 +42,7 @@ describe Lambda::Builder::Runtime do
HTTP::Client::Response.new(202)
end

runtime = Lambda::Builder::Runtime.new
runtime = Lambda::Runtime.new
runtime.register_handler("my_handler") do
JSON.parse(%q({ "foo" : "bar" }))
end
Expand All @@ -63,7 +63,7 @@ describe Lambda::Builder::Runtime do
HTTP::Client::Response.new(202)
end

runtime = Lambda::Builder::Runtime.new
runtime = Lambda::Runtime.new
runtime.register_handler("my_handler") do
response = Lambda::Builder::HTTPResponse.new(200, "text body")
response.headers["Content-Type"] = "application/text"
Expand All @@ -84,7 +84,7 @@ describe Lambda::Builder::Runtime do
HTTP::Client::Response.new(202)
end

runtime = Lambda::Builder::Runtime.new
runtime = Lambda::Runtime.new
runtime.register_handler("my_handler") do
raise "anything"
end
Expand All @@ -100,12 +100,64 @@ describe Lambda::Builder::Runtime do
HTTP::Client::Response.new(202)
end

runtime = Lambda::Builder::Runtime.new
runtime = Lambda::Runtime.new
runtime.register_handler("my_handler") do
JSON.parse "{}"
end
runtime.process_handler

ENV["_X_AMZN_TRACE_ID"].should eq "TRACE-ID"
end

it "can run with a (String -> String) handler" do
mock_next_invocation request_body_v2

WebMock.stub(:post, "http://localhost/2018-06-01/runtime/invocation/54321/response").to_return do |request|
request.body.to_s.should eq "Hi I'm a string"
HTTP::Client::Response.new(202)
end

runtime = Lambda::Runtime.new
runtime.register_handler("my_handler", String, String) do
"Hi I'm a string"
end
runtime.process_handler
end

it "can run with a (HTTPRequest -> HTTPResponse) handler" do
mock_next_invocation request_body

expected = Lambda::Builder::HTTPResponse.new(200, "Hello from Crystal").as_json.to_json

WebMock.stub(:post, "http://localhost/2018-06-01/runtime/invocation/54321/response").to_return do |request|
request.body.to_s.should eq expected
HTTP::Client::Response.new(202)
end

runtime = Lambda::Runtime.new
runtime.register_handler("my_handler", Lambda::Builder::HTTPRequest, Lambda::Builder::HTTPResponse) do
Lambda::Builder::HTTPResponse.new(200, "Hello from Crystal")
end
runtime.process_handler
end

it "can run with a (APIGatewayV2HTTPRequest -> APIGatewayV2HTTPResponse) handler" do
mock_next_invocation request_body_v2

expected = Lambda::Events::APIGatewayV2HTTPResponse.new(200, "yolo").to_json

WebMock.stub(:post, "http://localhost/2018-06-01/runtime/invocation/54321/response").to_return do |request|
request.body.to_s.should eq expected
HTTP::Client::Response.new(202)
end

runtime = Lambda::Runtime.new

runtime.register_handler("my_handler", Lambda::Events::APIGatewayV2HTTPRequest, Lambda::Events::APIGatewayV2HTTPResponse) do |input|
response = Lambda::Events::APIGatewayV2HTTPResponse.new(200, "yolo")
response
end

runtime.process_handler
end
end
59 changes: 58 additions & 1 deletion spec/spec_helper.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
require "spec"
require "webmock"

require "../src/lambda_builder"
require "../src/lambda"

Log.setup(:trace)

def request_body
<<-END
Expand Down Expand Up @@ -116,3 +118,58 @@ def request_body
}
END
end

def request_body_v2
<<-END
{
"version": "2.0",
"routeKey": "OPTIONS /{proxy+}",
"rawPath": "/hi",
"rawQueryString": "",
"headers": {
"accept": "*/*",
"accept-encoding": "gzip, deflate, br",
"accept-language": "en-US,en;q=0.9",
"access-control-request-headers": "authorization,content-type",
"access-control-request-method": "POST",
"content-length": "0",
"content-type": "application/x-www-form-urlencoded",
"forwarded": "by=4.235.36.19;for=108.29.59.253;host=local.dev;proto=https",
"host": "local.dev",
"origin": "https://local.dev",
"referer": "https://local.dev/",
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-site",
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36",
"via": "HTTP/1.1 AmazonAPIGateway",
"x-amzn-trace-id": "Self=1-60ad1af6-6041ce5d35bd44ad26b42c70;Root=1-60ad1af6-7bbc041f64b16ac44ad97952",
"x-forwarded-for": "3.235.36.19",
"x-forwarded-port": "443",
"x-forwarded-proto": "https"
},
"pathParameters": {
"proxy": "score"
},
"requestContext": {
"routeKey": "OPTIONS /{proxy+}",
"accountId": "333957572119",
"stage": "$default",
"requestId": "f5Emhi3LIAMEM7A=",
"apiId": "07lpms4hxh",
"domainName": "local.dev",
"domainPrefix": "local",
"time": "25/May/2021:15:42:46 +0000",
"timeEpoch": 1621957366418,
"http": {
"method": "OPTIONS",
"path": "/hi",
"protocol": "HTTP/1.1",
"sourceIp": "3.235.36.19",
"userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36"
}
},
"isBase64Encoded": false
}
END
end
File renamed without changes.
Loading