Share
Embed
AWS Lambda 내부동작 파헤치기
Taewoo Kim
November 18, 2020
Transcript
54 slides
-
ߊ ࣗѐ • 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/ ೠҴয & ੌࠄয ӝࣿ࠶۽Ӓ -
ࠄ ࣁ࣌ীࢲ ঠӝ ೡ ղਊ • ݒפ٘ ۨ߰ীࢲ ࠄ AWS
Lambda • ٘ ۨ߰ীࢲ ࠄ AWS Lambda • ੋۄ ۨ߰ীࢲ ࠄ AWS Lambda • ߊղਊ ܻ -
AWS Lambda Managed Console • ߆ীب Custom Runtime, Lambda
Layer, Event Destinations ١ ݆ ӝמٜ . • ୭Ӕীח Lambda Extensions ө ١! • Ӕؘ….. • ॶ ࣻ ח ӝמ ؘ݆, ޥ ݽܰѷ!!! ઁ۽ ঌҊ ॳҊ र!!!! • Ӓ۠ ٜ࠙ਸ ਤೠ ࣁ࣌ੑפ :) -
AWS Lambda Managed Console • ߆ীب Custom Runtime, Lambda
Layer, Event Destinations ١ ݆ ӝמٜ . • ୭Ӕীח Lambda Extensions ө ١! • Ӕؘ….. • ॶ ࣻ ח ӝמ ؘ݆, ޥ ݽܰѷ!!! ઁ۽ ঌҊ ॳҊ र!!!! • Ӓ۠ ٜ࠙ਸ ਤೠ ࣁ࣌ੑפ :) ֎ ݏणפƃƃ য়ט ݒפ٘ ۨ߰ীࢲ Lambda ࠁח, Lambda ղࠗ زী ખ ؊ ୡਸ ݏ ঠӝೞ۰Ҋ פ! ݒפ٘ ۨ߰ীࢲ ޙ ࣁ࣌ղਊҗ ߹ѐ۽ ޙ ೞ࣊ب જणפ :) -
AWS Lambda ೩ٜ۞ ೣࣻ event ৬ context ী ೧ ೠߣତ
ઁ۽ ҕࠗ೧ نद! -
AWS Lambda - event • AWS Lambda о Invoke ؼ
ٸ ܲ ࢲ࠺झীࢲ ߮ ( ؘఠ) ܳ ׳߉ח. • ٸ, যڃ ࢲ࠺झ۽ࠗఠ Invoke غחী ٮۄ event ҳઑо ׳ۄ ! • event ח ԙ JSON ഋకо ইפ؊ۄب list, string, number ١ ੌ߈ੋ ఋੑٜۄب ҡଳ! -
AWS Lambda - event • ࢎप, event ё “ܴ” ਵ۽
ੋ೧ Invoke ߑधী ೧ ഒز ৢ ࣻ . • Invoke ߑधীח ѱ 2о ߑध ( Event / RequestResponse ) ਵ۽ ա. • RequestResponse ߑध ੋ : API Gateway -> Lambda • RequestResponse ߑधਵ۽ invoke غ؊ۄب ؘఠח event ёܳ ా೧ Lambda ೣࣻী ׳ؽ • ޙ!! Lambda о Event ߑधਵ۽ ഐػ ҃, प೯ Ѿҗ۽ ߈ജغח ؘఠо ਵݶ ؘ ఠח যڌѱ Invoke ܳ ਃೠ ۄীѱ द ׳ؼө? -
AWS Lambda - context • AWS Lambda Runtime ࠁܳ
೩ٜ۞ীѱ ઁҕೞӝ ਤ೧ ࢎਊ. -
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() о ॶݽо ਸب..?! -
۠Ѣ ঌইࢲ য٣ ॄݡযਃ? • п ࢲ࠺झٜ ߊࢤदఃח Event ఋੑਸ
ܻ ೧فҊ, ਬపझী ഝਊ ೞѢա ߮ ఋੑী ٮܲ ࠙ӝܻ, ؊ աইоࢲ AWS Lambda ܳ ؊ ੜ ഝਊೡ ࣻ ח ա݅ ۄ࠳۞ܻ/ۨਕܳ ݅٘ח ؘীب ഝਊೡ ࣻ ! • ۽Ӧ ߂ X-Ray ١ਸ ాೠ ࠙ ۨयਸ ਤ೧ࢲ যڃ ݫఋؘఠܳ թӝݶ જਸ, ژח ػ ۽Ӓ ӝ۾ਸ যڌѱ Ѩ࢝ೞݶ જਸী ೠ ൦ܳ ਸ ࣻ ! • ޖٚ ࢶ ੜ ঌҊ যঠ ೡ ٸী ഝਊೡ ࣻ ח ߨ! -
Ӓېࢲ חਃ… (Ӓ ࠄੋ ۽ં ഘࠁ۽ !!) • AWS AppSync
ীࢲ ؘఠࣗझ۽ AWS Lambda ܳ ࢸ೮ਸٸ ߊࢤೞח event ҳઑܳ ঈ೧ࢲ, औҊ ബҗਵ۽ GraphQL API ܳ ѐߊೡ ࣻ ח ࢲߡܻ झ GraphQL ۨਕ • ই шդইӝ ࣻળ • द ҙबਵन ٜ࠙ ೠߣ ॄࠁदҊ ೖ ٘ߔ ࠗఌ٘݀פ :) -
Ӓ ߆ীب… ## Global Scope • ೩ٜ۞ ೣࣻ ߄Ӵ
٘ झఋद (init) ী݅ प ೯غח ࠗ࠙. • ೣࣻ ઙܐറীب Worker ֢٘о ઙܐغ ঋח ࢚, ݫݽܻী թই. • ୡӝച(Initialization) ী ҙೠ కझܳ ী ߓ ೧ࢲ ഝਊೞݶ AWS Lambda ಌನݢझ ೱ ࢚ী ب ؽ. • BUT, ൔ ഝਊೞח RDB ழ֏࣌ী ഝਊೞח ҃ ױب ਵפ ! -
AWS Lambda Invoke • AWS Lambda ܳ ࢎਊೞݶ ੋۄ ѣহ
࠺ૉפझ ۽ਸ ҳഅೞחؘ ೡ ࣻ যࢲ ࢤࢿ ֫ইҊ, উੋ ࢲ࠺झ ਸ ೡ ࣻ ! • Ӕؘ, ѱ যڌѱ оמೠѢ?! • AWS Lambda ೣࣻо Invoke غݶ, AWS ղࠗਵ۽ যڃ ੌ ੌযաח Ѣ?! -
AWS Lambda Invoke • InvocationType ࣁо۽ ա (Invoke द
যڃ ߑधਵ۽ प೯ೡ Ѿ оמ) • Event | RequestResponse | DryRun • Event ח Asynchronous ߑध, RequestResponse ח Synchronous ߑ ध, DryRun ۄఠ ߂ ӂೠਸ Ѩࢎ೧ࢲ प೯ оמೠী ೠ ݅ ਸ ࣻ೯ • ৈӝࢲ ԝԝ ࠊঠೡ ղਊ! (ّীࢲ ҅ࣘ) -
AWS Lambda Invoke Invocation Type Event ݶ Lambda ೣࣻо
ࢿҕ೮ח ৈࠗܳ ঌ ࣻ হ! -
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. -
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 -
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 दெঠೣ -
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 (ݵ١ࢿ) ܳ Ҋ۰ೞৈ ࢿغযঠೠ -
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 ܳ ా೧ पಁ ঌܿਸ ߉ਸ ࣻ -
AWS Lambda Security Whitepaper • Event ߑधਵ۽ ഐغח ҃ :
Amazon S3, Amazon SNS, EventBridge (CloudWatch Events) ١ • RequestResponse ߑधਵ۽ ഐغח ҃ : Amazon Kinesis, DynamoDB Streams, SQS, ALB, API Gateway -
Invocation Type ী ٮܲ AWS Lambda ۞࠶ग • ۠ Ѫٜਸ
ঌݶ غݶ, ইې ࢚ടীࢲ ޖਸ ഛੋ೧ঠೡ ౸ױೡ ࣻ Ҋ, झझ۽ ۞࠶गਸ ೡ ࣻ ѱػ. • दաܻয়1) ؘఠܳ SQS ী ֍Ҋ Lambda ܳ Invoke दఃח दա ܻয়ীࢲ Lambda о पಁ೮. ؘఠח য٣۽ тө? Lambda ೣࣻח ӝܻҊ ਵݶ द ژ प೯ؼө? • दաܻয়2) ੌਸ S3 ী স۽٘೧ࢲ Lambda ܳ Invoke दఃח द աܻয়ীࢲ Lambda о पಁ೮. ߮ח য٣۽ тө? Lambda ೣࣻח ӝܻҊ ਵݶ द ژ प೯ؼө? -
Invocation Type Event ੌ ٸ ! • Invocation Type
Event ੋ ҃, Invoke Ѿҗо HTTP STATUS 2XX о ߈ജؽ. • , पઁ Lambda प೯Ѿҗ৬ ҙ۲হ Invoke ೡ ߮ܳ ੜ ׳೮ ח ۽ 2XX ܳ ߈ജೞਵ۽ पઁ Lambda ٘о ੜ ز೮ח ഛ ੋೡ ࣻ ח ߑߨ হ. • ۠ ҃ী ਃೠ Ѫ AWS Lambda Dead Letter Queue -
AWS Lambda ইఃఫ৬ ҙ۲ػ ࣁ࣌ • AWS re:Invent 2018: [REPEAT
1] A Serverless Journey: AWS Lambda Under the Hood (SRV409-R1) Synchronous Invocation زী ೠ Өࣼೠ ࢸݺਸ ೧ח ࣁ࣌! -
AWS Lambda ইఃఫ৬ ҙ۲ػ ࣁ࣌ • AWS re:Invent 2019: [REPEAT
1] A serverless journey: AWS Lambda under the hood (SVS405-R1) Asynchronous Invocation زী ೠ Өࣼೠ ࢸݺਸ ೧ח ࣁ࣌! -
AWS Lambda ইఃఫ৬ ҙ۲ػ ࣁ࣌ • AWS Lambda ղࠗ ز
ߑध ߂ ഝਊ ߑߨ ࣁ ಝࠁӝ- ӣੌഐ ࣛܖ࣌ૉ ইఃఫ ݒפ(AWS) AWS Summit Seoul 2019 ীࢲ ӣੌഐ SA ש Synchronous Invocation ী ೠ ೠҴয ࣁ࣌ -
AWS Lambda Under The Hood AWS re:Invent 2019: [REPEAT 1]
A serverless journey: AWS Lambda under the hood (SVS405-R1) -
AWS Lambda Under The Hood • ۽٘ߖ۠य (Load Balancing) •
झாੌ স & (Scaling Up and Down) • पಁ ܖӝ (Handling Failures) • ஏоמೠ ծ ۨఢद (Predictable Low Latency) • द௸ܻ౭ܳ ਤೠ ജ҃ Ҋ݀ (Security Isolation) • ࢎਊܫ ҙܻ (Managing Utilization) • … (and many other things) -
Synchronous Invoke AWS re:Invent 2019: [REPEAT 1] A serverless journey:
AWS Lambda under the hood (SVS405-R1) -
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 -
Synchronous Invoke AWS re:Invent 2018: [REPEAT 1] A Serverless Journey:
AWS Lambda Under the Hood (SRV409-R1) -
Synchronous Invoke AWS re:Invent 2018: [REPEAT 1] A Serverless Journey:
AWS Lambda Under the Hood (SRV409-R1) Worker ਤী Firecracker о प೯ؽ = पઁ۽ ٘о زೞח ജ҃ -
Asynchronous Invoke AWS re:Invent 2019: [REPEAT 1] A serverless journey:
AWS Lambda under the hood (SVS405-R1) -
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 -
Asynchronous Invoke AWS re:Invent 2019: [REPEAT 1] A serverless journey:
AWS Lambda under the hood (SVS405-R1) -
Firecracker • AWS Lambda ղࠗ ਃࣗীࢲ Worker ח ਗې
EC2 ੋझఢझ • EC2 ੋझఢझח ߧਊੋ ݾਵ۽ о࢚ചغҊ, ٸޙী AWS Lambda ಌನݢझ ബਯࢿਸ ೧ೠҊ ౸ױ • 2017֙ࠗఠ Firecracker ۄח ࢲߡܻझ ਕ۽٘ী ୭ചػ ୡ҃ചػ о࢚ ചӝࣿਸ AWS о ѐߊೞӝ द. • crosvm ࠳ےীࢲ द೧ࢲ, PCI ա BIOS ৬ э ࢲߡܻझ ਕ۽٘ܳ ҳഅ ೞחؘ ਃহח ӝמٜਸ ल ઁѢ. • Ѿҗਵ۽ QEMU ٘ ۄੋࣻࠁ 96% ۄੋࣻ۽ ҳഅؽ. -
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 ۽ࣁझח ܲ Ҋёٜҗ ҕਬغ ঋ -
ߊղਊ ܻ • AWS Lambda Invocation Type ѱ
2о (DryRun ઁ৻) • যڃ Invocation Type ਵ۽ Invoke ೞջী ٮۄࢲ ࢤп೧ঠೞח ನੋо ׳ۄ. • Lambda ೩ٜ۞ ۄఠ۽ যח event ৬ context ী ೠ ޙࢲח ೠߣତ ة೧ف. • AWS Lambda ೣࣻܳ ஶ܀ ೡ ࣻ ח Ѫٜਸ ևҊ रݶ, SQS ١ਸ ా ೧ RequestResponse ߑध Invoke ܳ ځৢܻ. • AWS re:Invent, AWS Summit, AWS Whitepaper ١ ন ஶబஎܳ ӓਵ ۽ ࣗ࠺ೞ.