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

AWS Lambda with Scala

Shigeki Shoji
December 20, 2022

AWS Lambda with Scala

Introduction to AWS Lambda SnapStart and comparison to the Provisioned Concurrency.
In addition, an introduction to the underlying CRaC.

#nakanoshima_dev #aws #lambda #snapstart

Shigeki Shoji

December 20, 2022
Tweet

More Decks by Shigeki Shoji

Other Decks in Technology

Transcript

  1. AWS Lambda with
    Scala
    庄司 重樹

    View Slide

  2. 自己紹介
    庄司 重樹 (@takesection)
    株式会社豆蔵 | コンサルタント
    AWS や各種技術の導入支援、3x AWS Certified
    Scala関西、関ジャバ、JJUG

    View Slide

  3. 歴史
    2014年11月13日: AWS Lambda – Run Code in the Cloud
    https://aws.amazon.com/jp/blogs/aws/run-code-cloud/
    2015年6月15日: AWS Lambda Announces Java Support
    https://aws.amazon.com/jp/blogs/compute/aws-lambda-announces-java-support/
    2019年12月3日: New – Provisioned Concurrency for Lambda Functions
    https://aws.amazon.com/jp/blogs/aws/new-provisioned-concurrency-for-lambda-
    functions/
    2022年11月28日: New – Accelerate Your Lambda Functions with Lambda SnapStart
    https://aws.amazon.com/jp/blogs/aws/new-accelerate-your-lambda-functions-with-
    lambda-snapstart/
    3

    View Slide

  4. Serverless
    AWS Solutions Architect、Karan Desai 氏の2021年12月1日のツイート (re:Invent 2021)
    https://twitter.com/somecloudguy/status/1465741773179789313
    At this rate, by the end of his keynote, Adam Selipsky is going to announce that the
    company's name is being changed to Amazon Web Serverless
    このまま、基調講演の終わりまでに、Adam Selipsky 氏が会社名を Amazon Web
    Serverless に変更することを発表する予定です。
    4

    View Slide

  5. Scala を使う利点
    オブジェクト指向、関数型どちらのプログラミングパラダイムでも記述できる
    Java のライブラリなど豊富なエコシステムの利用が可能
    5

    View Slide

  6. 2019年12月3日: Provisioned Concurrency
    New – Provisioned Concurrency for Lambda Functions
    6

    View Slide

  7. Provisioned Concurrency vs SnapStart
    Provisioned Concurrency
    Pros:
    Arm サポートがある
    Cons:
    あらかじめ低レイテンシーで提供する同時実行数を見積る必要がある
    Provisioned Concurrency の有効化から無効化までの期間 (5分単位) で課金される
    7

    View Slide

  8. SnapStart
    Pros:
    プロビジョニングする同時実行数を見積もる必要がない
    SnapStart にかかる課金はない
    Cons:
    Arm をサポートしていないなど制約がある
    Java 11 runtime を使用する場合のみ
    初期化時にネットワーク等のコネクションを保持している場合等では Runtime hooks の
    実装が必要になる
    8

    View Slide

  9. デモンストレーション
    コンストラクタで無駄に 10 秒 WAIT を入れている。
    https://github.com/takesection-sandbox/aws-lambda-scala
    package com.pigumer
    import com.amazonaws.services.lambda.runtime.{Context, RequestStreamHandler}
    import java.io.{InputStream, OutputStream}
    import java.nio.charset.StandardCharsets
    import scala.util.Try
    class Main extends RequestStreamHandler {
    val sleep = Try(Thread.sleep(10000))
    override def handleRequest(input: InputStream, output: OutputStream, context: Context): Unit = {
    output.write("\"Hello, World!\"".getBytes(StandardCharsets.UTF_8));
    output.flush();
    }
    }
    9

    View Slide

  10. SnapStart があるとき
    RESTORE_START Runtime Version: java:11.v15 Runtime Version ARN
    RESTORE_REPORT Restore Duration: 204.27 ms
    START RequestId: 2f98d5aa-c63b-41e8-a9d2-0d2c21d7410d Version: 2
    END RequestId: 2f98d5aa-c63b-41e8-a9d2-0d2c21d7410d
    REPORT RequestId: 2f98d5aa-c63b-41e8-a9d2-0d2c21d7410d
    Duration: 13.82 ms Billed Duration: 129 ms
    Memory Size: 512 MB
    Max Memory Used: 71 MB
    Restore Duration: 204.27 ms
    10

    View Slide

  11. SnapStart がないとき
    START RequestId: 7ec844a7-7c81-44fc-9746-16a5b8c22262 Version: $LATEST
    END RequestId: 7ec844a7-7c81-44fc-9746-16a5b8c22262
    REPORT RequestId: 7ec844a7-7c81-44fc-9746-16a5b8c22262
    Duration: 10806.85 ms
    Billed Duration: 10807 ms
    Memory Size: 512 MB
    Max Memory Used: 20 MB
    11

    View Slide

  12. CRIU / CRaC
    JJUG CCC 2022 Fall: コンテナ環境でのJava技術の進化
    https://youtu.be/nJJHSwywei0
    Coordinated Restore at Checkpoint (CRaC) は JVM 用にカスタマイズされた Container
    Restore In Userspace (CRIU) を使って、プロセスイメージの保存と復元を行う。
    CRaC には、Resource インターフェースがあり、プロセスイメージを保存する前の処理を実
    行する beforeCheckpoint メソッドとリストア後にネットワーク接続等を復元するための
    afterRestore メソッドがある。
    12

    View Slide

  13. CRaC を試す
    CRaCによるJavaの高速化
    https://developer.mamezou-tech.com/blogs/2022/12/02/jdk-crac/
    Amazon Linux 2 を使用した例が記事に書かれている。つまり、Lambda だけでなく同じよう
    にスナップショットを保存しておいて、リストアをして起動をはやくすることができる。
    13

    View Slide

  14. Serverless Badge
    Serverless Learning Plan
    https://explore.skillbuilder.aws/learn/public/learning_plan/view/92/serverless-
    learning-plan
    14

    View Slide

  15. 参考
    Handling uniqueness with Lambda SnapStart
    https://docs.aws.amazon.com/lambda/latest/dg/snapstart-uniqueness.html
    Runtime hooks for Lambda SnapStart
    https://docs.aws.amazon.com/lambda/latest/dg/snapstart-runtime-hooks.html
    Best practices for working with Lambda SnapStart
    https://docs.aws.amazon.com/lambda/latest/dg/snapstart-best-practices.html
    15

    View Slide

  16. ご静聴ありがとうございました

    View Slide