$30 off During Our Annual Pro Sale. View Details »

introducing AWS Lambda For Java Developer

introducing AWS Lambda For Java Developer

Ching Yi Chan

August 01, 2015
Tweet

More Decks by Ching Yi Chan

Other Decks in How-to & DIY

Transcript

  1. "84-BNCEB • 讓開發者專注於功能開發,減少 infrastructure 維運 負擔的事件驅動(event-driven)服務 • ⾃自動依 event 或

    request 的速率決定 auto scaling。 • 結合 AWS 其他產品提供的 Event 或 Stream 資料, ⾜足以應付⼤大多數的背景服務實作。 • 例如:使⽤用者上傳圖⽚片,製作圖⽚片縮圖以便在各種 device 或不同情境使⽤用。
  2. ੂБ streams poll & invoke pull mode S3 SNS Cognito

    Echo User App DynamoDB Kinesis push mode invoke directly Event Event/RequestResponse
  3. ੂБ streams poll & invoke pull mode S3 SNS Cognito

    Echo User App DynamoDB Kinesis push mode invoke directly Event Event/ whatever mode API Gateway
  4. ੂБ streams poll & invoke pull mode S3 SNS Cognito

    Echo User App DynamoDB Kinesis push mode invoke directly Event Event Event RequestResponse • Invocation Type • ResquestResponse • synchronously • return supported types • Event • asynchronously • must return void
  5. ੂБᐑྤ ኹ׬-BNCEBලᇌdלഒලᇌሙၤ 實際上會在 Linux Container 上執⾏行 僅 /tmp 有寫⼊入權限 上傳的

    ZIP 被解壓縮⾄至 /var/task ⺫⽬目錄 LD_LIBRARY_PATH="/var/runtime :/var/task:/var/task/lib"
  6. -BNCEB'VODUJPO • lambda function 必需是 stateless 的。 • lambda function

    除了可讀寫 /tmp 路徑,其他路徑都 是唯讀或無法存取,不應該依賴或假設執⾏行環境的狀 態。 • 不能假設 lambda function 總是在同⼀一台機器上執⾏行 (隨著 event/request rate 會⾃自動 auto scaling) • 可以將 native library 包成 deployable package (ZIP format) 內,但記得標⽰示為 executable 權限。
  7. ᇜᄳ-BNCEB'VODUJPO outputType handler-name(inputType input, Context context) { ... } •

    Invocation Type • ResquestResponse • synchronously • return supported types • Event • asynchronously • must return void • supported types • simple java types • POJO (java bean convention) • InputStream
 OutputStream ՉྼʔึᏨݟ̴݊ʔ݊WPJE
  8. ᛆࠢ-BNCEB&YFDVUPS JO*".3PMF { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow",

    "Action": [ "logs:CreateLogGroup", "logs:CreateLogStream", "logs:PutLogEvents" ], "Resource": "*" } ] } ცΎ̋ɪԴ͜Չ̴؂ਕึ͜ՑٙᛆࠢdԷν4ᛘᄳ
  9. ᛆࠢԴ͜-BNCEB"1* { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action":

    [ "lambda:*", "iam:PassRole" ], "Resource": "*" } ] } މ%&.0˙کdҢࡁᔊఊԴ͜MBNCEB°ܸ֛הϞᛆࠢ JBN1BTT3PMFʔ̋œೌجխ̣MBNCEBBQJd ྅݊œһอMBNCEBGVODUJPODPEFఱʔঐਂə
  10. 1FFQ-BNCEB&OWJSPONFOU • ⽤用 Lambda 呼叫 Linux 指令,觀察 Lambda 執⾏行環 境

    • Java 版本,No SecurityManager • 環境變數 • Image • User
  11. ffprobe version 0.9, Copyright (c) 2007-2011 the FFmpeg developers built

    on Jan 8 2015 14:27:50 with clang 6.0 (clang-600.0.56) configuration: --prefix=/opt/muzee --disable-yasm --disable-doc --enable-openssl --enable-nonfree libavutil 51. 32. 0 / 51. 32. 0 libavcodec 53. 42. 0 / 53. 42. 0 libavformat 53. 24. 0 / 53. 24. 0 libavdevice 53. 4. 0 / 53. 4. 0 libavfilter 2. 53. 0 / 2. 53. 0 libswscale 2. 1. 0 / 2. 1. 0 Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'foo.mp4': Metadata: major_brand : mp42 minor_version : 0 compatible_brands: isommp42 creation_time : 2014-11-22 13:54:19 Duration: 00:05:12.21, start: 0.000000, bitrate: 675 kb/s Stream #0:0(und): Video: h264 (Constrained Baseline) (avc1 / 0x31637661), yuv420p, 640x360 [SAR 1:1 DAR 16:9], 576 kb/s, 30 fps, 30 tbr, 30 tbn, 60 tbc Metadata: handler_name : VideoHandler Stream #0:1(und): Audio: aac (mp4a / 0x6134706D), 44100 Hz, stereo, s16, 95 kb/s Metadata: creation_time : 2014-11-22 13:54:20 handler_name : FYBNQMF -BNCEB8JUI/BUJWF-JCSBSJFT • 利⽤用 FFMpeg 製作影⽚片預覽圖 • 使⽤用 ffprobe 取得影⽚片⻑⾧長度,每 30 秒建⽴立⼀一個縮圖 任務 (VideoPreviewTaskGenerator) • 使⽤用 ffmpeg 執⾏行縮圖任務 { "bucket": "qty.lambda.video", "offset": 60, "image_key": "images/sample.mp4.2.png", "key": "videos/sample.mp4" }
  12. FYBNQMF -BNCEB8JUI/BUJWF-JCSBSJFT • 註冊的 S3 Event • OnVideoUploaded 取得 video

    duration 建⽴立截圖 任務 • OnReceivedPreviewJob 建⽴立縮圖
  13. • 注意檔案權限,執⾏行檔與 shared object 需要可執⾏行 權限 task buildZip(type: Zip) {

    from compileJava from processResources into('lib') { from configurations.runtime } eachFile { if (it.name.contains(".so.")) fileMode 0755 if (it.name.contains("ffprobe")) fileMode 0755 if (it.name.contains("ffmpeg")) fileMode 0755 } } FYBNQMF -BNCEB8JUI/BUJWF-JCSBSJFT
  14. Չ˼ਪᕚ "84$-* aws lambda create-function help --runtime (string) The runtime

    environment for the Lambda function you are uploading. Currently, Lambda supports "java" and "nodejs" as the runtime. KBWBʑ݊࿁ٙ