Skip to content

Commit 7a3a99e

Browse files
committed
pick lambda handler based on $_HANDLER environment variable
1 parent 811fa15 commit 7a3a99e

File tree

4 files changed

+25
-9
lines changed

4 files changed

+25
-9
lines changed

AWSLambdaSwift/Sources/AWSLambdaSwift/Runtime.swift

+12-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Foundation
22

3-
func log(_ object: Any, flush: Bool = false) {
3+
public func log(_ object: Any, flush: Bool = false) {
44
fputs("\(object)\n", stderr)
55
if flush {
66
fflush(stderr)
@@ -13,7 +13,7 @@ public class Runtime {
1313
var counter = 0
1414
let urlSession: URLSession
1515
let awsLambdaRuntimeAPI: String
16-
let handler: String
16+
let lambdaName: String
1717
var lambdas: [String: (JSONDictionary) -> JSONDictionary]
1818

1919
public init() throws {
@@ -25,9 +25,13 @@ public class Runtime {
2525
let handler = environment["_HANDLER"] else {
2626
throw RuntimeError.missingEnvironmentVariables
2727
}
28-
28+
29+
guard let periodIndex = handler.index(of: ".") else {
30+
throw RuntimeError.invalidHandlerName
31+
}
32+
2933
self.awsLambdaRuntimeAPI = awsLambdaRuntimeAPI
30-
self.handler = handler
34+
self.lambdaName = String(handler[handler.index(after: periodIndex)...])
3135
}
3236

3337
func getNextInvocation() throws -> (input: JSONDictionary, requestId: String) {
@@ -75,7 +79,10 @@ public class Runtime {
7579
counter += 1
7680
log("Invocation-Counter: \(counter)")
7781

78-
let lambda = lambdas["lambda"]!
82+
guard let lambda = lambdas[lambdaName] else {
83+
throw RuntimeError.unknownLambdaHandler
84+
}
85+
7986
let output = lambda(input)
8087
try postInvocationResponse(for: requestId, response: output)
8188
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
enum RuntimeError: Error {
22
case missingEnvironmentVariables
3+
case invalidHandlerName
34
case endpointError(String)
45
case missingData
56
case invalidData
7+
case unknownLambdaHandler
68
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
import AWSLambdaSwift
22

3-
func lambda(input: JSONDictionary) -> JSONDictionary {
4-
print(input)
3+
func lambda1(input: JSONDictionary) -> JSONDictionary {
4+
log(input)
55
return ["hello": "world", "number": 42, "strings": ["one", "two", "three"]]
66
}
77

8+
func lambda2(input: JSONDictionary) -> JSONDictionary {
9+
log(input)
10+
return ["result": "success"]
11+
}
12+
813
let runtime = try Runtime()
9-
runtime.registerLambda("lambda", handler: lambda)
14+
runtime.registerLambda("lambda1", handler: lambda1)
15+
runtime.registerLambda("lambda2", handler: lambda2)
1016
try runtime.start()

bootstrap

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
#!/bin/sh
2-
/opt/swift-shared-libs/ld-linux-x86-64.so.2 --library-path /opt/swift-shared-libs/lib ./ExampleLambda
2+
EXECUTABLE=$LAMBDA_TASK_ROOT/"$(echo $_HANDLER | cut -d. -f1)"
3+
/opt/swift-shared-libs/ld-linux-x86-64.so.2 --library-path /opt/swift-shared-libs/lib $EXECUTABLE

0 commit comments

Comments
 (0)