Upgrade to Pro — share decks privately, control downloads, hide ads and more …

try! Serverless App with Swift

try! Serverless App with Swift

E387c487b4fea568909cf805f91afe92?s=128

Yu Tawata

March 19, 2019
Tweet

Transcript

  1. try! Serverless App with Swift '19/03/19 try! Swift Pre Talks

    2019
  2. YU TAWATA • Job: iOS Engineer at DMM.com • Twitter:

    yuta24 • GitHub: yuta24
  3. Why Serverless App with Swift? • Custom Runtime for AWS

    Lambda • Can execute programs written in any programming languages. • Swift 5 • Easier to bridge to python, compared with Swift4
  4. What's AWS Lambda Custom Runtime? • bootstrap • execute programs

    using request params and return the results as responses #!/bin/sh set -euo pipefail # Initialization - load function handler source $LAMBDA_TASK_ROOT/"$(echo $_HANDLER | cut -d. -f1).sh" # Processing while true do HEADERS="$(mktemp)" # Get an event EVENT_DATA=$(curl -sS -LD "$HEADERS" -X GET "http://${AWS_LAMBDA_RUNTIME_API}/2018-06-01/runtime/invocation/next") REQUEST_ID=$(grep -Fi Lambda-Runtime-Aws-Request-Id "$HEADERS" | tr -d '[:space:]' | cut -d: -f2) # Execute the handler function from the script RESPONSE=$($(echo "$_HANDLER" | cut -d. -f2) "$EVENT_DATA") # Send the response curl -X POST "http://${AWS_LAMBDA_RUNTIME_API}/2018-06-01/runtime/invocation/$REQUEST_ID/response" -d "$RESPONSE" done
  5. Let's, try!

  6. How? 1. Create table on AWS Dynamo DB. 2. Create

    AWS Lambda function. 3. Create sample project using SPM. 4. Make executable file (Using Docker: Swift 5 on Ubuntu 16.04). 5. Deploy executable file and bootstrap to AWS Lambda.
  7. let boto3 = Python.import("boto3") let dynamodb = boto3.resource("dynamodb") let table

    = dynamodb.Table("swift-sample") print("\(table.scan())")
  8. But, a program stopped

  9. Problems 1. Can't load shared libraries. 2. The environment variable

    PYTHON_LIBRARY isn’t set. 3. Can't find boto3 module.
  10. Use aws-lambda-swift Use aws-lambda-swift (Changed to use Swift 5). Deploy

    to shared libraries as a layer. And link dynamically when handling request in bootstrap. EXECUTABLE=$LAMBDA_TASK_ROOT/"$(echo $_HANDLER | cut -d. -f1)" /opt/swift-shared-libs/ld-linux-x86-64.so.2 --library-path /opt/swift-shared-libs/lib $EXECUTABLE
  11. Set env var and deploy boto3 1. Search python lib

    path and Set an environment variable. PYTHON_LIBRARY=/usr/lib64/libpython2.7.so.1.0 2. Deploy boto3 module as a layer. And set /opt/python as PYTHONPATH. $ docker run --privileged -it -v `pwd`:`pwd` -w `pwd` --rm amazonlinux $ yum install python2-pip $ pip install -t ./python boto3 $ zip -r boto3.zip python
  12. import AWSLambdaSwift struct Event: Codable { } struct Result: Codable

    { let result: String } func sample(event: Event, context: Context) -> Result { let boto3 = Python.import("boto3") let dynamodb = boto3.resource("dynamodb") let table = dynamodb.Table("swift-sample") return Result(result: "\(table.scan())") } let runtime = try Runtime() runtime.registerLambda("sample", handlerFunction: sample) try runtime.start()
  13. None
  14. We can develop Serverless app with Swift!!

  15. Reference • Custom AWS Lambda Runtimes • AWS Lambda Layers

    • Boto 3 - The AWS SDK for Python • aws-lambda-swift
  16. Thanks!