Skip to content
This repository was archived by the owner on Jul 14, 2020. It is now read-only.

Commit 21eaecd

Browse files
committed
More Events.
Added the following Event types: - ALB Event - Cloudwatch Event - SQS Event - SNS Event - DynamoDB Event Improvements to: - APIGateway Added multi purpose type: - Added AWSNumber (Thanks for the idea @Ro-M.)
1 parent 28602a7 commit 21eaecd

32 files changed

+2165
-132
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
xcuserdata
55
Layer
66
Examples/**/lambda.zip
7-
packaged.yaml
7+
packaged.yaml
8+
bootstrap

.swiftpm/xcode/xcshareddata/xcschemes/AWSLambda.xcscheme

Lines changed: 0 additions & 92 deletions
This file was deleted.

Examples/EventSources/Package.resolved

Lines changed: 61 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Examples/EventSources/Package.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// swift-tools-version:5.1
2+
// The swift-tools-version declares the minimum version of Swift required to build this package.
3+
4+
import PackageDescription
5+
6+
let package = Package(
7+
name: "EventSources",
8+
dependencies: [
9+
.package(path: "../.."),
10+
.package(url: "https://github.com/apple/swift-nio.git", .upToNextMajor(from: "2.9.0")),
11+
.package(url: "https://github.com/apple/swift-log.git", .upToNextMajor(from: "1.1.1")),
12+
],
13+
targets: [
14+
.target(
15+
name: "EventSources",
16+
dependencies: ["LambdaRuntime", "Logging", "NIO", "NIOFoundationCompat", "NIOHTTP1"]),
17+
]
18+
)

Examples/EventSources/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# EventSources
2+
3+
This package/executable can be used to test the different Event types. This especially usefull during development of new Event types. We use the sam `template.yaml` to deploy the external resources needed for testing. Please be aware, if you deploy the given `template.yaml` costs may occur (especially when using the LoadBalancer).
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
import LambdaRuntime
2+
import NIO
3+
import Logging
4+
import Foundation
5+
6+
LoggingSystem.bootstrap(StreamLogHandler.standardError)
7+
8+
9+
let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
10+
defer { try! group.syncShutdownGracefully() }
11+
let logger = Logger(label: "AWSLambda.EventSources")
12+
13+
func handleSNS(event: SNS.Event, ctx: Context) -> EventLoopFuture<Void> {
14+
ctx.logger.info("Payload: \(String(describing: event))")
15+
16+
return ctx.eventLoop.makeSucceededFuture(Void())
17+
}
18+
19+
func handleSQS(event: SQS.Event, ctx: Context) -> EventLoopFuture<Void> {
20+
ctx.logger.info("Payload: \(String(describing: event))")
21+
22+
return ctx.eventLoop.makeSucceededFuture(Void())
23+
}
24+
25+
func handleDynamoStream(event: DynamoDB.Event, ctx: Context) -> EventLoopFuture<Void> {
26+
ctx.logger.info("Payload: \(String(describing: event))")
27+
28+
return ctx.eventLoop.makeSucceededFuture(Void())
29+
}
30+
31+
func handleCloudwatchSchedule(event: Cloudwatch.Event<Cloudwatch.ScheduledEvent>, ctx: Context)
32+
-> EventLoopFuture<Void>
33+
{
34+
ctx.logger.info("Payload: \(String(describing: event))")
35+
36+
return ctx.eventLoop.makeSucceededFuture(Void())
37+
}
38+
39+
func handleAPIRequest(req: APIGateway.Request, ctx: Context) -> EventLoopFuture<APIGateway.Response> {
40+
ctx.logger.info("Payload: \(String(describing: req))")
41+
42+
struct Payload: Encodable {
43+
let path: String
44+
let method: String
45+
}
46+
47+
let payload = Payload(path: req.path, method: req.httpMethod.rawValue)
48+
let response = try! APIGateway.Response(statusCode: .ok, payload: payload)
49+
50+
return ctx.eventLoop.makeSucceededFuture(response)
51+
}
52+
53+
func handleLoadBalancerRequest(req: ALB.TargetGroupRequest, ctx: Context) ->
54+
EventLoopFuture<ALB.TargetGroupResponse>
55+
{
56+
ctx.logger.info("Payload: \(String(describing: req))")
57+
58+
struct Payload: Encodable {
59+
let path: String
60+
let method: String
61+
}
62+
63+
let payload = Payload(path: req.path, method: req.httpMethod.rawValue)
64+
let response = try! ALB.TargetGroupResponse(statusCode: .ok, payload: payload)
65+
66+
return ctx.eventLoop.makeSucceededFuture(response)
67+
}
68+
69+
func printPayload(buffer: NIO.ByteBuffer, ctx: Context) -> EventLoopFuture<ByteBuffer?> {
70+
let payload = buffer.getString(at: 0, length: buffer.readableBytes)
71+
ctx.logger.error("Payload: \(String(describing: payload))")
72+
73+
return ctx.eventLoop.makeSucceededFuture(nil)
74+
}
75+
76+
func printOriginalPayload(_ handler: @escaping (NIO.ByteBuffer, Context) -> EventLoopFuture<ByteBuffer?>)
77+
-> ((NIO.ByteBuffer, Context) -> EventLoopFuture<ByteBuffer?>)
78+
{
79+
return { (buffer, ctx) in
80+
let payload = buffer.getString(at: 0, length: buffer.readableBytes)
81+
ctx.logger.info("Payload: \(String(describing: payload))")
82+
83+
return handler(buffer, ctx)
84+
}
85+
}
86+
87+
do {
88+
logger.info("start runtime")
89+
let environment = try Environment()
90+
let handler: LambdaRuntime.Handler
91+
92+
switch environment.handlerName {
93+
case "sns":
94+
handler = printOriginalPayload(LambdaRuntime.codable(handleSNS))
95+
case "sqs":
96+
handler = printOriginalPayload(LambdaRuntime.codable(handleSQS))
97+
case "dynamo":
98+
handler = printOriginalPayload(LambdaRuntime.codable(handleDynamoStream))
99+
case "schedule":
100+
handler = printOriginalPayload(LambdaRuntime.codable(handleCloudwatchSchedule))
101+
case "api":
102+
handler = printOriginalPayload(APIGateway.handler(handleAPIRequest))
103+
case "loadbalancer":
104+
handler = printOriginalPayload(ALB.handler(multiValueHeadersEnabled: true, handleLoadBalancerRequest))
105+
default:
106+
handler = printPayload
107+
}
108+
109+
let runtime = try LambdaRuntime.createRuntime(eventLoopGroup: group, handler: handler)
110+
defer { try! runtime.syncShutdown() }
111+
logger.info("starting runloop")
112+
113+
try runtime.start().wait()
114+
}
115+
catch {
116+
logger.error("error: \(String(describing: error))")
117+
}
118+
119+

0 commit comments

Comments
 (0)