Skip to content

Files

Latest commit

33ec012 · May 25, 2020

History

History
This branch is up to date with swift-serverless/aws-lambda-swift-sprinter:master.

RedisDemo

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
Mar 22, 2020
May 25, 2020
Dec 27, 2019
Dec 27, 2019
Dec 27, 2019
Mar 22, 2020
Feb 8, 2020
May 2, 2020
May 7, 2020
Dec 27, 2019
Dec 27, 2019

RedisDemo

Swift 5 Swift 5.1.5 Swift 5.2.3

This example shows the usage of the LambdaSwiftSprinter framework and the plugin LambdaSwiftSprinterNioPlugin to build a lambda capable to perform an Redis request using RediStack.

Swift code

Define an Event and a Response as Codable.

import AsyncHTTPClient
import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif
import LambdaSwiftSprinter
import LambdaSwiftSprinterNioPlugin
import Logging
import NIO
import NIOFoundationCompat
import RediStack

struct Event: Codable {
    let key: String
    let value: String
}

struct Response: Codable {
    let value: String
}

Add a loger:

let logger = Logger(label: "AWS.Lambda.Redis")

Add a redis connection:

let elasticacheConfigEndpoint = "redis"

let eventLoop = httpClient.eventLoopGroup.next()
let connection = try? RedisConnection.connect(
        to: try .makeAddressResolvingHost(elasticacheConfigEndpoint,
                                          port: RedisConnection.defaultPort),
        on: eventLoop
    ).wait()



enum LambdaError: Error {
    case redisConnectionFailed
}

Define the Lambda:

let syncCodableNIOLambda: SyncCodableNIOLambda<Event, Response> = { (event, context) throws -> EventLoopFuture<Response> in
    
    guard let connection = connection else {
        throw LambdaError.redisConnectionFailed
    }
    
    let future = connection.set(event.key, to: event.value)
        .flatMap {
            return connection.get(event.key)
        }
        .map { content -> Response in
            return Response(value: content ?? "")
        }
    return future
}

If there are not error, the Event will be automatically decoded inside the lambda and then used to perform a set and a get to Redis. The value of the get is returned into the Response.

Then use this boilerplate code to run the lambda:

do {
    
    let sprinter = try SprinterNIO()
    sprinter.register(handler: "setGet", lambda: syncCodableNIOLambda)
    
    try sprinter.run()
} catch {
    logger.error("\(String(describing: error))")
}

This will initialize the Sprinter with a Sprinter based on NIO 2 library.

Then the internal handler setGet is being registered.

Finally the sprinter run.

Any error will be logged.

Note

In this example we used swift-log to log the output.

Deployment

To build this example make sure the following lines on the Makefile are not commented and the other example configurations are commented.

...

# HelloWorld Example Configuration
# SWIFT_EXECUTABLE=HelloWorld
# SWIFT_PROJECT_PATH=Examples/HelloWorld
# LAMBDA_FUNCTION_NAME=HelloWorld
# LAMBDA_HANDLER=$(SWIFT_EXECUTABLE).helloWorld

....

# RedisDemo Example Configuration
SWIFT_EXECUTABLE?=RedisDemo
SWIFT_PROJECT_PATH?=Examples/RedisDemo
LAMBDA_FUNCTION_NAME?=RedisDemo
LAMBDA_HANDLER?=$(SWIFT_EXECUTABLE).setGet

...

Then follow the main README to build and test the code.

Test

The test event is defined in the file event.json

{
    "key": "language",
    "value": "Swift"
}

expected response:

{"value":"Swift"}

Change it to try different output and error conditions.

LambdaSwiftSprinter

To know more refer to LambdaSwiftSprinter.