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

AWS Lambda 내부동작 파헤치기

Taewoo Kim
November 18, 2020

AWS Lambda 내부동작 파헤치기

Developers.IO Korea Online #02 에서 발표한 세션 자료입니다.

Youtube 세션영상보기 : https://youtu.be/_wDRrFa-6Gc

Taewoo Kim

November 18, 2020
Tweet

More Decks by Taewoo Kim

Other Decks in Technology

Transcript

  1. ߊ಴੗ ࣗѐ • AWS Solutions Architect (Classmethod, Inc) • 2020

    APN AWS Top Engineers (by AWS Japan) • 12x AWS Certified Engineer • જইೞח AWS ࢲ࠺झ : AWS AppSync / AWS Amplify / AWS Lambda https://dev.classmethod.jp/author/kim-taewoo/ ೠҴয & ੌࠄয ӝࣿ࠶۽Ӓ
  2. ࠄ ࣁ࣌ীࢲ ੉ঠӝ ೡ ղਊ • ݒפ૑٘ ۨ߰ীࢲ ࠄ AWS

    Lambda • ௏٘ ۨ߰ীࢲ ࠄ AWS Lambda • ੋ೐ۄ ۨ߰ীࢲ ࠄ AWS Lambda • ߊ಴ղਊ ੿ܻ
  3. AWS Lambda Managed Console • ੉ ߆ীب Custom Runtime, Lambda

    Layer, Event Destinations ١ ݆ ਷ ӝמٜ੉ ੓਺. • ୭Ӕীח Lambda Extensions ө૑ ١੢! • Ӕؘ….. • ॶ ࣻ ੓ח ӝמ਷ ݆਷ؘ, ޥ૑ ݽܰѷ׮!!! ઁ؀۽ ঌҊ ॳҊ र׮!!!! • Ӓ۠ ٜ࠙ਸ ਤೠ ࣁ࣌ੑפ׮ :)
  4. AWS Lambda Managed Console • ੉ ߆ীب Custom Runtime, Lambda

    Layer, Event Destinations ١ ݆ ਷ ӝמٜ੉ ੓਺. • ୭Ӕীח Lambda Extensions ө૑ ١੢! • Ӕؘ….. • ॶ ࣻ ੓ח ӝמ਷ ݆਷ؘ, ޥ૑ ݽܰѷ׮!!! ઁ؀۽ ঌҊ ॳҊ र׮!!!! • Ӓ۠ ٜ࠙ਸ ਤೠ ࣁ࣌ੑפ׮ :) ֎ ݏणפ׮ƃƃ য়ט਷ ݒפ૑٘ ۨ߰ীࢲ੄ Lambda ࠁ׮ח, Lambda ੄ ղࠗ ز੘ী ખ ؊ ୡ੼ਸ ݏ୾ ੉ঠӝೞ۰Ҋ ೤פ׮! ݒפ૑٘ ۨ߰ীࢲ੄ ૕ޙ਷ ࣁ࣌ղਊҗ ߹ѐ۽ ૕ޙ ೞ࣊ب જणפ׮ :)
  5. AWS Lambda - event • AWS Lambda о Invoke ؼ

    ٸ ׮ܲ ࢲ࠺झীࢲ ੉߮౟ (഑਷ ؘ੉ఠ) ܳ ੹׳߉ח׮. • ੉ ٸ, যڃ ࢲ࠺झ۽ࠗఠ Invoke غ঻ח૑ী ٮۄ event ੄ ҳઑо ׳ۄ૓ ׮! • event ח ԙ JSON ഋకо ইפ؊ۄب list, string, number ١੄ ੌ߈੸ੋ ఋੑٜ੉ۄب ҡଳ׮!
  6. AWS Lambda - event • ࢎप, event ё୓੄ “੉ܴ” ਵ۽

    ੋ೧ Invoke ߑधী ؀೧ ഒز੉ ৢ ࣻ ੓׮. • Invoke ߑधীח ௼ѱ 2о૑ ߑध ( Event / RequestResponse ) ਵ۽ ա׍. • RequestResponse ߑध੄ ؀಴੸ੋ ৘ : API Gateway -> Lambda • RequestResponse ߑधਵ۽ invoke غ؊ۄب ؘ੉ఠח event ё୓ܳ ా೧ Lambda ೣࣻী ੹׳ؽ • ૕ޙ!! 
 Lambda о Event ߑधਵ۽ ഐ୹ػ ҃਋, प೯ Ѿҗ۽ ߈ജغח ؘ੉ఠо ੓ਵݶ ੉ ؘ ੉ఠח যڌѱ Invoke ܳ ਃ୒ೠ ௿ۄ੉঱౟ীѱ ׮द ੹׳ؼө?
  7. AWS Lambda - context • AWS Lambda ੄ Runtime ੿ࠁܳ

    ೩ٜ۞ীѱ ઁҕೞӝ ਤ೧ ࢎਊ.
 
 
 
 
 
 
 
 
 

  8. AWS Lambda - context • ۽Ӧ ١੄ ਊب۽ ࢎਊೡ ࣻ

    ੓ח ݫఋؘ੉ఠо ਖ਼ڱ ઁҕػ׮.
 (function_name / function_version / invoked_function_arn / memory_limit_in_mb / aws_request_id / log_group_name ١) • য়ۖزউ प೯غח AWS Lambda ೣࣻ੄ ҃਋, context ੄ get_remaining_time_in_millis() о ॶݽо ੓ਸ૑ب..?!
  9. ੉۠Ѣ ঌইࢲ য٣׮ ॄݡযਃ? • п ࢲ࠺झٜ੉ ߊࢤदఃח Event ఋੑਸ

    ޷ܻ ੿੄೧فҊ, ਬ׫పझ౟ী ഝਊ ೞѢա ੉߮౟ ఋੑী ٮܲ ࠙ӝ୊ܻ, ഑਷ ؊ աইоࢲ AWS Lambda ܳ ؊ ਌ ੜ ഝਊೡ ࣻ ੓ח ա݅੄ ۄ੉࠳۞ܻ/೐ۨ੐ਕ௼ܳ ݅٘ח ؘীب ഝਊೡ ࣻ ੓׮! • ۽Ӧ ߂ X-Ray ١ਸ ాೠ ࠙࢑ ౟ۨ੉यਸ ਤ೧ࢲ যڃ ݫఋؘ੉ఠܳ թӝݶ જਸ૑, ژח ੷੢ػ ۽Ӓ ӝ۾ਸ যڌѱ Ѩ࢝ೞݶ જਸ૑ী ؀ೠ ൦౟ܳ ঳ਸ ࣻ ੓׮! • ޖ঺੉ٚ૑ ਋ࢶ਷ ੜ ঌҊ ੓যঠ ੸੺ೡ ٸী ੸੺൤ ഝਊೡ ࣻ ੓ח ߨ!
  10. Ӓېࢲ ੷חਃ… (Ӓ੷ ࠄੋ ೐۽ં౟ ഘࠁ੉޲۽ ઱੄!!) • AWS AppSync

    ীࢲ ؘ੉ఠࣗझ۽ AWS Lambda ܳ ࢸ੿೮ਸٸ ߊࢤೞח event ҳઑܳ ౵ঈ೧ࢲ, औҊ ബҗ੸ਵ۽ GraphQL API ܳ ѐߊೡ ࣻ ੓ח ࢲߡܻ झ GraphQL ೐ۨ੐ਕ௼ • ই૒ шդইӝ ࣻળ • ഑द ҙब੓ਵन ٜ࠙਷ ೠߣ ॄࠁदҊ ೖ ٘ߔ ࠗఌ٘݀פ׮ :)
  11. Ӓ ߆ীب… ## Global Scope • ೩ٜ۞ ೣࣻ ߄Ӵ੄ ৔৉਷

    ௒٘ झఋ౟द (init) ী݅ प ೯غח ࠗ࠙. • ೣࣻ ઙܐറীب Worker ֢٘о ઙܐغ૑ ঋח ੉ ࢚, ݫݽܻী թই੓਺. • ୡӝച(Initialization) ী ҙೠ కझ௼ܳ ੉ ৔৉ী ߓ஖ ೧ࢲ ੸੺൤ ഝਊೞݶ AWS Lambda ੄ ಌನݢझ ೱ ࢚ী ب਑੉ ؽ. • BUT, ൔ൤ ഝਊೞח RDB ழ֏࣌ী ഝਊೞח ҃਋ ױ੼ب ੓ਵפ ઱੄!
  12. AWS Lambda Invoke • AWS Lambda ܳ ࢎਊೞݶ ੋ೐ۄ ѣ੿হ੉

    ࠺ૉפझ ۽૒ਸ ҳഅೞחؘ ૘઺ೡ ࣻ ੓যࢲ ࢤ࢑ࢿ੉ ֫ই૑Ҋ, উ੿੸ੋ ࢲ࠺झ ਍৔ਸ ೡ ࣻ ੓׮! • Ӕؘ, ੉ѱ যڌѱ оמೠѢ૑?! • AWS Lambda ೣࣻо Invoke غݶ, AWS ղࠗ੸ਵ۽ যڃ ੌ੉ ੌযաח Ѣ૑?!
  13. AWS Lambda Invoke • InvocationType ਷ ࣁо૑۽ ա׍ (Invoke द

    যڃ ߑधਵ۽ प೯ೡ૑ Ѿ ੿ оמ) • Event | RequestResponse | DryRun • Event ח Asynchronous ߑध, RequestResponse ח Synchronous ߑ ध, DryRun ਷ ౵ۄ޷ఠ ߂ ӂೠਸ Ѩࢎ೧ࢲ प೯ оמೠ૑ী ؀ೠ ୓௼݅ ਸ ࣻ೯ • ৈӝࢲ ԝԝ൤ ࠊঠೡ ղਊ! (ّ੢ীࢲ ҅ࣘ)
  14. AWS Lambda Invoke • For asynchronous invocation, Lambda adds events

    to a queue before sending them to your function. If your function does not have enough capacity to keep up with the queue, events may be lost. Occasionally, your function may receive the same event multiple times, even if no error occurs. To retain events that were not processed, configure your function with a dead-letter queue.
  15. AWS Lambda Invoke • For asynchronous invocation, Lambda adds events

    to a queue before sending them to your function. If your function does not have enough capacity to keep up with the queue, events may be lost. Occasionally, your function may receive the same event multiple times, even if no error occurs. To retain events that were not processed, configure your function with a dead-letter queue. ٍীࢲ ࢸݺೞѷ૑݅, ੉Ѥ SQS
  16. AWS Lambda Invoke • For asynchronous invocation, Lambda adds events

    to a queue before sending them to your function. If your function does not have enough capacity to keep up with the queue, events may be lost. Occasionally, your function may receive the same event multiple times, even if no error occurs. To retain events that were not processed, configure your function with a dead-letter queue. ٮۄࢲ, ߈٘द ࣻ೯غযঠೞח కझ௼ח ߹ب੄ Ҋёࣗਬ SQS ܳ ా೧ Invoke दெঠೣ
  17. AWS Lambda Invoke • For asynchronous invocation, Lambda adds events

    to a queue before sending them to your function. If your function does not have enough capacity to keep up with the queue, events may be lost. Occasionally, your function may receive the same event multiple times, even if no error occurs. To retain events that were not processed, configure your function with a dead-letter queue. Event-Driven ߑधਵ۽ ز੘ೞח Lambda ೣࣻۄݶ ߈٘द Idempotency (ݵ١ࢿ) ܳ Ҋ۰ೞৈ ੘ࢿغযঠೠ׮
  18. AWS Lambda Invoke • For asynchronous invocation, Lambda adds events

    to a queue before sending them to your function. If your function does not have enough capacity to keep up with the queue, events may be lost. Occasionally, your function may receive the same event multiple times, even if no error occurs. To retain events that were not processed, configure your function with a dead-letter queue. Asynchronous ߑधਵ۽ ഐ୹ػ Lambda ೣࣻо पಁೞח ҃ ਋, Dead Letter Queue ܳ ా೧ पಁ ঌܿਸ ߉ਸ ࣻ ੓׮
  19. AWS Lambda Security Whitepaper • Event ߑधਵ۽ ഐ୹غח ҃਋
 :

    Amazon S3, Amazon SNS, EventBridge (CloudWatch Events) ١ • RequestResponse ߑधਵ۽ ഐ୹غח ҃਋
 : Amazon Kinesis, DynamoDB Streams, SQS, ALB, API Gateway
  20. Invocation Type ী ٮܲ AWS Lambda ౟۞࠶ग౴ • ੉۠ Ѫٜਸ

    ঌݶ غݶ, ইې੄ ࢚ടীࢲ ޖ঺ਸ ഛੋ೧ঠೡ ૑ ౸ױೡ ࣻ ੓ Ҋ, झझ۽ ౟۞࠶ग౴ਸ ೡ ࣻ ੓ѱػ׮. • दաܻয়1) ؀۝੄ ؘ੉ఠܳ SQS ী ֍Ҋ Lambda ܳ Invoke दఃח दա ܻয়ীࢲ Lambda о पಁ೮׮. ؘ੉ఠח য٣۽ тө? Lambda ೣࣻח ӝ׮ܻҊ ੓ਵݶ ׮द ژ प೯ؼө? • दաܻয়2) ؀۝੄ ౵ੌਸ S3 ী স۽٘೧ࢲ Lambda ܳ Invoke दఃח द աܻয়ীࢲ Lambda о पಁ೮׮. ੉߮౟ח য٣۽ тө? Lambda ೣࣻח ӝ׮ܻҊ ੓ਵݶ ׮द ژ प೯ؼө?
  21. Invocation Type ੉ Event ੌ ٸ ઱੄੼! • Invocation Type

    ੉ Event ੋ ҃਋, Invoke ੄ Ѿҗо HTTP STATUS 2XX о ߈ജؽ. • ૊, पઁ Lambda ੄ प೯Ѿҗ৬ ҙ۲হ੉ Invoke ೡ ੉߮౟ܳ ੜ ੹׳೮׮ ח ੄޷۽ 2XX ܳ ߈ജೞ৓ਵ޲۽ पઁ Lambda ௏٘о ੜ ز੘೮ח૑ ഛ ੋೡ ࣻ ੓ח ߑߨ੉ হ਺. • ੉۠ ҃਋ী ೙ਃೠ Ѫ੉ AWS Lambda ੄ Dead Letter Queue
  22. AWS Lambda ইఃఫ୛৬ ҙ۲ػ ࣁ࣌ • AWS re:Invent 2018: [REPEAT

    1] A Serverless Journey: AWS Lambda Under the Hood (SRV409-R1) Synchronous Invocation ੄ ز੘ী ؀ೠ Өࣼೠ ࢸݺਸ ೧઱ח ࣁ࣌!
  23. AWS Lambda ইఃఫ୛৬ ҙ۲ػ ࣁ࣌ • AWS re:Invent 2019: [REPEAT

    1] A serverless journey: AWS Lambda under the hood (SVS405-R1) Asynchronous Invocation ੄ ز੘ী ؀ೠ Өࣼೠ ࢸݺਸ ೧઱ח ࣁ࣌!
  24. AWS Lambda ইఃఫ୛৬ ҙ۲ػ ࣁ࣌ • AWS Lambda ղࠗ ز੘

    ߑध ߂ ഝਊ ߑߨ ੗ࣁ൤ ࢓ಝࠁӝ- ӣੌഐ ࣛܖ࣌ૉ ইఃఫ౟ ݒפ੷(AWS) AWS Summit Seoul 2019 ীࢲ ӣੌഐ SA ש੄ Synchronous Invocation ী ؀ೠ ೠҴয ࣁ࣌
  25. AWS Lambda Under The Hood AWS re:Invent 2019: [REPEAT 1]

    A serverless journey: AWS Lambda under the hood (SVS405-R1)
  26. AWS Lambda Under The Hood • ۽٘ߖ۠य (Load Balancing) •

    झாੌ স & ׮਍ (Scaling Up and Down) • पಁ ׮ܖӝ (Handling Failures) • ৘ஏоמೠ ծ਷ ۨ੉ఢद (Predictable Low Latency) • द௸ܻ౭ܳ ਤೠ ജ҃ Ҋ݀ (Security Isolation) • ࢎਊܫ ҙܻ (Managing Utilization) • … (and many other things)
  27. Synchronous Invoke • Front End Invoke - Orchestrate both synchronous

    and asynchronous Invokes • Counting Service - Provides a region wide view of customer concurrency to help enforce set limits • Worker Manger - Tracks container idle and busy state and schedules incoming invoke requests to available containers • Worker - Provisions a secure environment for customer code execution • Placement Service - Places sandboxes on workers to maximize packing density without impacting customer experience or cold-path latency
  28. Synchronous Invoke AWS re:Invent 2018: [REPEAT 1] A Serverless Journey:

    AWS Lambda Under the Hood (SRV409-R1) Worker ਤী Firecracker о प೯ؽ = पઁ۽ ௏٘о ز੘ೞח ജ҃
  29. Asynchronous Invoke • Poller - Consumes events and ensures they

    are processed • State Manager/Stream Tracker - Handles scaling by managing Pollers and event or stream source resources • Leasing Service - Assigns Pollers to work on a specific event or streaming source
  30. Firecracker • AWS Lambda ੄ ղࠗ ਃࣗীࢲ Worker ח ਗې

    EC2 ੋझఢझ৓਺ • EC2 ੋझఢझח ੉޷ ߧਊ੸ੋ ݾ੸ਵ۽ о࢚ചغ঻Ҋ, ੉ٸޙী AWS Lambda ੄ ಌನݢझ ബਯࢿਸ ੷೧ೠ׮Ҋ ౸ױ • 2017֙ࠗఠ Firecracker ۄח ࢲߡܻझ ਕ௼۽٘ী ୭੸ചػ ୡ҃۝ചػ о࢚ ചӝࣿਸ AWS о ѐߊೞӝ द੘. • crosvm ࠳ے஖ীࢲ द੘೧ࢲ, PCI ա BIOS ৬ э੉ ࢲߡܻझ ਕ௼۽٘ܳ ҳഅ ೞחؘ ೙ਃহח ӝמٜਸ ल ઁѢ. • Ѿҗ੸ਵ۽ QEMU ௏٘ ۄੋࣻࠁ׮ 96% ੸਷ ۄੋࣻ۽ ҳഅؽ.
  31. Firecracker • AWS Lambda ੄ प೯ജ҃ (execution environments) ਷ 2015֙ࠗఠ

    EC2 ੋझఢझ ਤীࢲ MicroVM ਸ ৢ۰֬Ҋ प೯दఃח ߑध੉঻਺ • ૊, AWS Lambda ࣗਬ੄ EC2 ੋझఢझ ਤী MicroVM ਸ ৢܻҊ, Ӓ ਤী ࠂࣻѐ੄ AWS Lambda प೯ജ҃ਸ ࣁ౴ೞח ߑध • ೞա੄ EC2 ੋझఢझ ਤী ࠂࣻѐ੄ MicroVM ਸ ৢܻҊ, п MicroVM ਤী ࠂࣻѐ੄ प೯ജ҃ਸ ࣁ౴ - MicroVM ਷ ׮ܲ Ҋёٜҗ ҕਬغ૑ ঋ਺ • അ੤ח Firecracker ӝ߈ਵ۽, EC2 Bare Metal Instance ਤী KVM ߂ Firecracker ܳ ৢܻҊ, Firecracker ਤী MicroVM ਸ ৢ۰فҊ AWS Lambda प೯ജ҃ਸ ࣁ౴ • ૊, Bare Metal ਤী ഐझ౟OS ߂ KVM ਸ ৢܻҊ, ੉ ਤী ࠂࣻѐ੄ Firecracker ܳ ڸ਍׮. • ೞա੄ Firecracker ೐۽ࣁझח ׮ܲ Ҋёٜҗ ҕਬغ૑ ঋ਺
  32. ߊ಴ղਊ ੿ܻ • AWS Lambda ੄ Invocation Type ਷ ௼ѱ

    2о૑ (DryRun ઁ৻) • যڃ Invocation Type ਵ۽ Invoke ೞջী ٮۄࢲ ࢤп೧ঠೞח ನੋ౟о ׳ۄ૓׮. • Lambda ೩ٜ۞੄ ౵ۄ޷ఠ۽ ઱য૑ח event ৬ context ী ؀ೠ ޙࢲח ೠߣତ਷ ੿ة೧ف੗. • ૒੽ AWS Lambda ೣࣻܳ ஶ౟܀ ೡ ࣻ ੓ח Ѫٜਸ և൤Ҋ र׮ݶ, SQS ١ਸ ా ೧ RequestResponse ߑध੄ Invoke ܳ ځৢܻ੗. • AWS re:Invent, AWS Summit, AWS Whitepaper ١੄ ন૕੄ ஶబஎܳ ੸ӓ੸ਵ ۽ ࣗ࠺ೞ੗.