Slide 1

Slide 1 text

© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. AWS Lambda Updates ポール (okadanob@) A W S r e : I n v e n t 2 0 2 2 r e : C a p Solutions Architect Amazon Web Services Japan

Slide 2

Slide 2 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. 自己紹介 ȃȞȑ [関⼼のある技術] Java, Kotlin, Python, Serverless, 釣り

Slide 3

Slide 3 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. AWS Lambda Updates • Amazon Inspector support for Lambda functions • AWS Lambda SnapStart

Slide 4

Slide 4 text

© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.

Slide 5

Slide 5 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. Amazon InspectorがLambda関数の診断をサポート • Amazon InspectorがLambda関数および Layer内で使われているアプリケーション パッケージの脆弱性検知に対応 • Java, NodeJS, Python の関数とLayer • Layer のスキャン対象は関数で 利用されている特定のバージョンのみ • スキャン対象のカスタマイズも可能 e.g. タグでの除外対象設定 • Lambda関数がデプロイされたタイミングで 開始し、関数のアップデートまたは 新たな脆弱性の公開をトリガに自動的に 再診断 • Amazon Inspectorをサポートする全ての リージョンで利用可能

Slide 6

Slide 6 text

© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.

Slide 7

Slide 7 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. Lambda のライフサイクル Warm Start Cold Start コンテナ 生成 パッケージ ロード パッケージ 展開 ランタイム起動 初期化 関数・メソッド 起動 deploy

Slide 8

Slide 8 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. AWS Lambda SnapStart Lambda 関数のコールドスタート時のレイテンシーを短縮が可能 • Lambda + Java でのコールドスタートを大幅に短縮 • スナップショットを利用する事でLambda 呼び出し時の init フェーズを削減する仕組み • 対象は Java11 の Corretto ランタイム • 追加料金は不要 • 東京リージョンを含む9リージョンで利用可能 • SAM, CDKも対応済み

Slide 9

Slide 9 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. SnapStart の概要 Init publish-version Encrypted snapshot stored Tiered low- latency cache State: Pending

Slide 10

Slide 10 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. SnapStart の概要 Init publish-version Encrypted snapshot stored Tiered low- latency cache State: Pending Invoke Resume State: Active

Slide 11

Slide 11 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. SnapStart の概要 Init publish-version Encrypted snapshot stored Tiered low- latency cache State: Pending Invoke Resume State: Active Invoke Resume Invoke Resume

Slide 12

Slide 12 text

© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.

Slide 13

Slide 13 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved.

Slide 14

Slide 14 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. Java Java はアプリケーション開発で使われているプラットフォームの⼀つ Java コードをバイトコードにコンパイルし、JVM上で処理実⾏が可能 バイトコード 機械語 ソースコード Java コンパイラ JIT コンパイラ JVM ※他にもコンポーネント有り ライブラリ JRE JDK

Slide 15

Slide 15 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. クラウドコンピューティングの発展 VM VM ホスト クラウド VM コンテナ / サーバーレス モノリス マイクロサービス 必要に応じた インフラリソースの利⽤ よりクラウドの特徴を ⽣かした実⾏環境へ 単⼀アプリケーションから 複数の⼩さなアプリケーションに

Slide 16

Slide 16 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. JVM + アプリケーションの起動時間 サーバー コンテナ サーバーレス アプリケーションプロセスのライフサイクルが異なる 必要な量だけ コンテナが 起動/停⽌ イベントに応じて 処理を実⾏/停⽌/ スケール 起動後は 動かしっぱなし

Slide 17

Slide 17 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. JVM + アプリケーションの起動時間 サーバー コンテナ サーバーレス コンテナが 起動毎に必要 イベントに応じた 処理を実⾏毎に必要 起動後は動き続ける のであまり課題に ならない

Slide 18

Slide 18 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. ピークパフォーマンスまでの時間 コンテナ JIT の最適化まで ⼀定の時間が必要 毎回ピークパフォーマンスまで 時間が必要。 最適化されても停⽌してしまう。

Slide 19

Slide 19 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. Java と モダンなインフラの組み合わせ時の課題 • JVM + アプリケーションの起動時間が都度発⽣する Ø 弾⼒性のあるインフラでは初回起動の時間が応答速度やスケールに影響する • JIT コンパイルによるピークパフォーマンスまでの時間 Ø 起動毎に最適化を⾏う必要があり、⾮効率 Ø 最適化に⾄らない場合も ライフサイクルの短い環境下では課題

Slide 20

Slide 20 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. Java と モダンなインフラの組み合わせ時の課題 • JVM + アプリケーションの起動時間が都度発⽣する Ø 柔軟性のあるインフラでは初回起動の時間が応答速度やスケールに影響する • JIT コンパイルによるピークパフォーマンスまでの時間 Ø 起動毎に最適化を⾏う必要があり、⾮効率 Ø 最適化に⾄らない場合も ライフサイクルの短い環境下では課題 起動時間を縮めることで、Java をクラウドでも活⽤

Slide 21

Slide 21 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. JVM の起動時のプロセス JVM を起動 クラスを読み込む インタープリタ実⾏ 実⾏時に JIT コンパイル • アプリケーションが実⾏されている部分 • プロファイルを取得して JIT コンパイル • JVM の起動部分 • Java のバイトコードのロード

Slide 22

Slide 22 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. JVM の起動時のプロセス JVM を起動 クラスを読み込む インタープリタ実⾏ 実⾏時に JIT コンパイル • アプリケーションが実⾏されている部分 • プロファイルを取得して JIT コンパイル • JVM の起動部分 • Java のバイトコードのロード

Slide 23

Slide 23 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. Create execution environment Init Download code Start runtime Initialize function code Run Lambda handler Invoke 6,600 ms 600 ms Invocation model

Slide 24

Slide 24 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. Create execution environment Init Download code Start runtime Initialize function code Class loading Runtime dependency injection Just-in-time compilation Java initialization

Slide 25

Slide 25 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. Current solutions Class loading Runtime dependency injection Just-in-time compilation Lightweight dependencies • Jackson-jr • Slf4j-simple • Micronaut • Quarkus • Dagger • No DI • More memory • Tiered compilation • AOT GraalVM native-image

Slide 26

Slide 26 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. 0 2 4 6 8 10 12 14 0 1 2 3 4 5 6 7 8 9 Tiered compilation New frameworks (Micronaut/Quarkus) No framework No optimization Lightweight dependencies Function handler (Spring Cloud Functions) GraalVM Effort to modernize Cold start (seconds) Provisioned Concurrency Cold start optimizations for a Spring Boot Migration

Slide 27

Slide 27 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. Pre snapshot hook (Optional) microVM snapshot technology Create execution environment Init Download code Start runtime Initialize function code Invocation model

Slide 28

Slide 28 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. Run Lambda handler Invoke Resume Resume snapshot Post snapshot hook (Optional) Pre snapshot hook (Optional) Create execution environment Init Download code Start runtime Initialize function code Invocation model

Slide 29

Slide 29 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. Project CRaC 実行中に Java インスタンスからチェックポイントを作成する機能の研究 をするプロジェクト 現状は Linux 環境での実装が進めらている。 内部で Linux の CRIU を活用したインスタンスのイメージを作成する。 OpenJDK のリリースには含まれていない ( まだ 実験的な取り組みの状態 ) https://openjdk.org/projects/crac/

Slide 30

Slide 30 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. CRIU Checkpoint Restore in Userspace の略 プロセスの動いているメモリ状態などをイメージにダンプし そのイメージからのリストア起動を実現するツール CPU Registers Memory Dump

Slide 31

Slide 31 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. CRaC Co-ordinated Restore at Checkpoint チェックポイントを作成し、アプリケーションにも復元を認識させる チェックポイントの 作成 復元処理 https://openjdk.org/projects/crac/

Slide 32

Slide 32 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. Pre snapshot hook (Optional) microVM snapshot technology Create execution environment Init Download code Start runtime Initialize function code Invocation model

Slide 33

Slide 33 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. SnapStart を含むLambda 関数の処理ステップ

Slide 34

Slide 34 text

© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.

Slide 35

Slide 35 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. Enabling SnapStart

Slide 36

Slide 36 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. AWS SAM

Slide 37

Slide 37 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. AWS CDK

Slide 38

Slide 38 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. SnapStart 前後の Hook

Slide 39

Slide 39 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. Example

Slide 40

Slide 40 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. START RequestId: 373c5199-54f0-4e9a-ab91-e4c7d3cdcbe5 Version: $LATEST END RequestId: 373c5199-54f0-4e9a-ab91-e4c7d3cdcbe5 REPORT RequestId: 373c5199-54f0-4e9a-ab91-e4c7d3cdcbe5 Duration: 14.20 ms Billed Duration: 15 ms Memory Size: 512 MB Max Memory Used: 82 MB Init Duration: 437.56 ms RESTORE_START Runtime Version: java:11.v15 Runtime Version ARN: arn:aws:lambda:ap-northeast- 1::runtime:0a25e3e7a1cc9ce404bc435eeb2ad358d8fa64338e618d0c224fe509403583ca RESTORE_REPORT Restore Duration: 298.18 ms START RequestId: d0f8b567-98b6-4a14-8e60-8b44d0f913f3 Version: 2 END RequestId: d0f8b567-98b6-4a14-8e60-8b44d0f913f3 REPORT RequestId: d0f8b567-98b6-4a14-8e60-8b44d0f913f3 Duration: 77.83 ms Billed Duration: 287 ms Memory Size: 512 MB Max Memory Used: 75 MB Restore Duration: 298.18 ms START RequestId: 373c5199-54f0-4e9a-ab91-e4c7d3cdcbe5 Version: $LATEST END RequestId: 373c5199-54f0-4e9a-ab91-e4c7d3cdcbe5 REPORT RequestId: 373c5199-54f0-4e9a-ab91-e4c7d3cdcbe5 Duration: 14.20 ms Billed Duration: 15 ms Memory Size: 512 MB Max Memory Used: 82 MB Init Duration: 437.56 ms 実行ログ

Slide 41

Slide 41 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. SnapStart の注意点 (1) • 現在は Java 11のみサポート • 利用にはバージョンとエイリアスを有効化が必要 • SnapStart が非対応な機能 • Arm64 アーキテクチャ • X-Ray • EFS • Lambda Extensions API • Provisioned Concurrency • 512MBを超えるエフェメラルストレージ

Slide 42

Slide 42 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. SnapStart の注意点 (2) • NW接続 • AWS SDKで関わるNW切断/再接続は自動的に再開されるので考慮不要 • ただし、ユーザー独自の処理で初期化フェーズでNW再接続をしている場合は resume時に再接続を行う必要がある • ユニークなID生成 • 初期化フェーズではID生成をしない • 関数ハンドラー内、もしくはRuntime-hookを使ってresume時に生成をする • ランダムなID生成 • java.security.SecureRandom 等を使ってスナップスタートの影響を受けない乱数生成をする

Slide 43

Slide 43 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. AWS Lambda SnapStart Bug Scanner 静的解析ツールの SpotBugs のプラグイン ルールベースの為、全ての課題を検出できるわけではない点は注意 SnapStart 利用時の潜在的なバグをチェックが可能(以下、参考例) • 乱数生成処理のチェック • ユニークID生成処理チェック • Timestamp取得処理 https://github.com/aws/aws-lambda-snapstart-java-rules

Slide 44

Slide 44 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. Java を利用の場合は他にも高速化の方法もある AWS Dev Day で解説した資料もご参考ください。 https://speakerdeck.com/bulbulpaul/bokutatiha-java-apurikesiyonnoqi-dong-su-du-wodokomadesuo-merareruka

Slide 45

Slide 45 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. Java on Lambda workshop

Slide 46

Slide 46 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. AWS Lambda Updates • Amazon Inspector support for Lambda functions • AWS Lambda SnapStart

Slide 47

Slide 47 text

J a p a n