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

Commit

Permalink
More Events.
Browse files Browse the repository at this point in the history
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.)
  • Loading branch information
fabianfett committed Jan 11, 2020
1 parent 28602a7 commit 21eaecd
Show file tree
Hide file tree
Showing 32 changed files with 2,165 additions and 132 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
xcuserdata
Layer
Examples/**/lambda.zip
packaged.yaml
packaged.yaml
bootstrap
92 changes: 0 additions & 92 deletions .swiftpm/xcode/xcshareddata/xcschemes/AWSLambda.xcscheme

This file was deleted.

61 changes: 61 additions & 0 deletions Examples/EventSources/Package.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{
"object": {
"pins": [
{
"package": "async-http-client",
"repositoryURL": "https://github.com/swift-server/async-http-client.git",
"state": {
"branch": null,
"revision": "51dc885a30ca704b02fa803099b0a9b5b38067b6",
"version": "1.0.0"
}
},
{
"package": "swift-base64-kit",
"repositoryURL": "https://github.com/fabianfett/swift-base64-kit.git",
"state": {
"branch": null,
"revision": "3ffa48a7047fc9ac6581cd53ab1df29466d8f13b",
"version": "0.2.0"
}
},
{
"package": "swift-log",
"repositoryURL": "https://github.com/apple/swift-log.git",
"state": {
"branch": null,
"revision": "74d7b91ceebc85daf387ebb206003f78813f71aa",
"version": "1.2.0"
}
},
{
"package": "swift-nio",
"repositoryURL": "https://github.com/apple/swift-nio.git",
"state": {
"branch": null,
"revision": "ff01888051cd7efceb1bf8319c1dd3986c4bf6fc",
"version": "2.10.1"
}
},
{
"package": "swift-nio-extras",
"repositoryURL": "https://github.com/apple/swift-nio-extras.git",
"state": {
"branch": null,
"revision": "53808818c2015c45247cad74dc05c7a032c96a2f",
"version": "1.3.2"
}
},
{
"package": "swift-nio-ssl",
"repositoryURL": "https://github.com/apple/swift-nio-ssl.git",
"state": {
"branch": null,
"revision": "ccf96bbe65ecc7c1558ab0dba7ffabdea5c1d31f",
"version": "2.4.4"
}
}
]
},
"version": 1
}
18 changes: 18 additions & 0 deletions Examples/EventSources/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// swift-tools-version:5.1
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "EventSources",
dependencies: [
.package(path: "../.."),
.package(url: "https://github.com/apple/swift-nio.git", .upToNextMajor(from: "2.9.0")),
.package(url: "https://github.com/apple/swift-log.git", .upToNextMajor(from: "1.1.1")),
],
targets: [
.target(
name: "EventSources",
dependencies: ["LambdaRuntime", "Logging", "NIO", "NIOFoundationCompat", "NIOHTTP1"]),
]
)
3 changes: 3 additions & 0 deletions Examples/EventSources/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# EventSources

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).
119 changes: 119 additions & 0 deletions Examples/EventSources/Sources/EventSources/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import LambdaRuntime
import NIO
import Logging
import Foundation

LoggingSystem.bootstrap(StreamLogHandler.standardError)


let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
defer { try! group.syncShutdownGracefully() }
let logger = Logger(label: "AWSLambda.EventSources")

func handleSNS(event: SNS.Event, ctx: Context) -> EventLoopFuture<Void> {
ctx.logger.info("Payload: \(String(describing: event))")

return ctx.eventLoop.makeSucceededFuture(Void())
}

func handleSQS(event: SQS.Event, ctx: Context) -> EventLoopFuture<Void> {
ctx.logger.info("Payload: \(String(describing: event))")

return ctx.eventLoop.makeSucceededFuture(Void())
}

func handleDynamoStream(event: DynamoDB.Event, ctx: Context) -> EventLoopFuture<Void> {
ctx.logger.info("Payload: \(String(describing: event))")

return ctx.eventLoop.makeSucceededFuture(Void())
}

func handleCloudwatchSchedule(event: Cloudwatch.Event<Cloudwatch.ScheduledEvent>, ctx: Context)
-> EventLoopFuture<Void>
{
ctx.logger.info("Payload: \(String(describing: event))")

return ctx.eventLoop.makeSucceededFuture(Void())
}

func handleAPIRequest(req: APIGateway.Request, ctx: Context) -> EventLoopFuture<APIGateway.Response> {
ctx.logger.info("Payload: \(String(describing: req))")

struct Payload: Encodable {
let path: String
let method: String
}

let payload = Payload(path: req.path, method: req.httpMethod.rawValue)
let response = try! APIGateway.Response(statusCode: .ok, payload: payload)

return ctx.eventLoop.makeSucceededFuture(response)
}

func handleLoadBalancerRequest(req: ALB.TargetGroupRequest, ctx: Context) ->
EventLoopFuture<ALB.TargetGroupResponse>
{
ctx.logger.info("Payload: \(String(describing: req))")

struct Payload: Encodable {
let path: String
let method: String
}

let payload = Payload(path: req.path, method: req.httpMethod.rawValue)
let response = try! ALB.TargetGroupResponse(statusCode: .ok, payload: payload)

return ctx.eventLoop.makeSucceededFuture(response)
}

func printPayload(buffer: NIO.ByteBuffer, ctx: Context) -> EventLoopFuture<ByteBuffer?> {
let payload = buffer.getString(at: 0, length: buffer.readableBytes)
ctx.logger.error("Payload: \(String(describing: payload))")

return ctx.eventLoop.makeSucceededFuture(nil)
}

func printOriginalPayload(_ handler: @escaping (NIO.ByteBuffer, Context) -> EventLoopFuture<ByteBuffer?>)
-> ((NIO.ByteBuffer, Context) -> EventLoopFuture<ByteBuffer?>)
{
return { (buffer, ctx) in
let payload = buffer.getString(at: 0, length: buffer.readableBytes)
ctx.logger.info("Payload: \(String(describing: payload))")

return handler(buffer, ctx)
}
}

do {
logger.info("start runtime")
let environment = try Environment()
let handler: LambdaRuntime.Handler

switch environment.handlerName {
case "sns":
handler = printOriginalPayload(LambdaRuntime.codable(handleSNS))
case "sqs":
handler = printOriginalPayload(LambdaRuntime.codable(handleSQS))
case "dynamo":
handler = printOriginalPayload(LambdaRuntime.codable(handleDynamoStream))
case "schedule":
handler = printOriginalPayload(LambdaRuntime.codable(handleCloudwatchSchedule))
case "api":
handler = printOriginalPayload(APIGateway.handler(handleAPIRequest))
case "loadbalancer":
handler = printOriginalPayload(ALB.handler(multiValueHeadersEnabled: true, handleLoadBalancerRequest))
default:
handler = printPayload
}

let runtime = try LambdaRuntime.createRuntime(eventLoopGroup: group, handler: handler)
defer { try! runtime.syncShutdown() }
logger.info("starting runloop")

try runtime.start().wait()
}
catch {
logger.error("error: \(String(describing: error))")
}


Loading

0 comments on commit 21eaecd

Please sign in to comment.