æ¦è¦
Lambdaã¯100msã®å®è¡æéåä½ã§ãªã³ããã³ãã«èª²éããããããç«ã¡ä¸ãã£ã±ãªãã®EC2ã¤ã³ã¹ã¿ã³ã¹ããããæéãå®ããªãå¯è½æ§ããããã¨ãä¸è¬ã«ç¥ããã¦ããã ãããã以ä¸ã®æ§è³ªãæºããã¢ããªã±ã¼ã·ã§ã³ã§ã¯ãEC2ã¤ã³ã¹ã¿ã³ã¹ä¸ã«æ§ç¯ããã±ã¼ã¹ã¨æ¯è¼ãã¦ãLambdaä¸ã«æ§ç¯ããã»ããã³ã¹ãå¹çãæªããªãã®ã§ã¯ãªããã¨èå¯ãã¦ã¿ãã
- Lambda functionã®å®è¡æéã®ãã¡ããããã¯ã¼ã¯I/Oæéãæ¯é çã§ãã
- Lambda functionã®å®è¡çµäºãåæçã«å¾ ããªããã°ãªããªã
- è¤æ°ã®ã¬ã³ã¼ããLambda functionã®å¼æ°ã«æ¸¡ããã¨ãã§ããªã
Lambdaã®åºæ¬ã³ã¹ãæ§é
ã¾ããLambdaã®ã³ã¹ãæ§é ãææ¡ããã Lambdaã®æé表[1]ã«ããã¨ããfunctionã«å¯¾ããåè¨ãªã¯ã¨ã¹ãæ°ãã¨ãfunctionã®åè¨å®è¡æéãã«å¿ãã¦æéãçºçããã
å¾è ã®åè¨å®è¡æéã¯ãéçã«å²ãå½ã¦ãã¡ã¢ãªéã«ãã100ããªç§åä½ã®ä¾¡æ ¼ãç°ãªãã ãããã£ã¦ãå®éã«ã³ã¹ãã試ç®ããã¨ãã¯ãåè¿°ã®2ã¤ã®æéé ç®ã«ããéçå²ãå½ã¦ã¡ã¢ãªéããå ãã3ã¤ã®æéé ç®ãããã¨èããã¨ããã Lambdaã®Pricing Calculator[2]ããããªã£ã¦ããã
Lambdaã¨EC2ã®æéãããã®ã©ã³ãã³ã°ã³ã¹ãæ¯è¼
Lambdaã¨EC2ã®æéãããã®ã©ã³ãã³ã°ã³ã¹ããæ¯è¼ããã functionã®å¹³åå®è¡æéãæéåä½ã¨åçã®100msã¨ä»®å®ãã¦ã1æéåã¤ã¾ã18000ãªã¯ã¨ã¹ããããããã®ã³ã¹ããã代表çãªã¡ã¢ãªéå¥ã«ç®åºããã¨ä»¥ä¸ã®ããã«ãªãã
- 128MB:
$0.015/hour
- 832MB:
$0.056/hour
- 1536MB:
$0.097/hour
ãã®ãã¡ããfunctionã«å¯¾ããåè¨ãªã¯ã¨ã¹ãæ°ãæéã«ã¤ãã¦ã¯ã$0.0072
ã§ããæ¯é
çã§ã¯ãªãã
EC2 ap-northeast-1ã®æé表[3]ãããLambdaã¨åãè«ç2ã³ã¢[4]ã®ä»£è¡¨çãªã¤ã³ã¹ã¿ã³ã¹ã®1æéãããã®ã³ã¹ãã以ä¸ã®ããã«ãªãã
- t2.medium:
$0.0608/hour
- t2.large:
$0.1216/hour
- m4.large:
$0.129/hour
- c4.large:
$0.126/hour
Lambdaã®FAQ[5]ã«ããããã«ãã¡ã¢ãªå²ãå½ã¦éã«å¯¾ãã¦CPUæ§è½ãæ¯ä¾ãããã¨ãããEC2ã¤ã³ã¹ã¿ã³ã¹ã¨ã®ç´æ¥çãªæ¯è¼ã¯é£ããããã ãããåç¨åº¦ãEC2ãå°ã å²é«ã§ãããã¨ããããã
Lambdaã«ä¸åããªã¢ããªã±ã¼ã·ã§ã³æ§è³ª
ã¢ããªã±ã¼ã·ã§ã³ä¾
åè¿°ã®ããã«ãLambdaã¨EC2ã®ã©ã³ãã³ã°ã³ã¹ãã«ããã»ã©å·®ã¯ãªããã¨ãèããã¨ãEC2ãããç´°ããç²åº¦ã§æ¶è²»ã³ã³ãã¥ã¼ãã£ã³ã°ãªã½ã¼ã¹ãå¶å¾¡ã§ããLambdaã®ã»ããæå©ãªã±ã¼ã¹ã¯ãæ°å¤ãããå¾ãã
ä¸æ¹ã§ãä¾ãã°ã次ã®ãããªã¢ããªã±ã¼ã·ã§ã³ãLambda functionã¨ãã¦å®è£ ãããã¨ãèãã¦ã¿ãã ç»åURLã渡ãã¨ãç»åããã§ããããç¹å®ã®ç»åå¤æãå®è¡ãããããªç»åå¤æãããã·ãèããã ãã®ãããã·ã«å¯¾ãã¦ãå¤æ°ã®ã¦ã¼ã¶ããã©ã¦ã¶ãããªã¯ã¨ã¹ããããããªã¦ã¼ã¹ã±ã¼ã¹ãæ³å®ããã ã¬ã¹ãã³ã¹ã¯åæçã«ç»åãè¿ãå¿ è¦ãããããã以ä¸ã®å³ã®ããã«API GatewayãLambdaã®å段ã«æããã¨ã«ãªãã ãã®å ´åãå®éã¯ããã«å段ã«CDNãæããã¨ã«ãªãã ããã
|-----------------------------------------------------------| | internet | | --> (CDN) --> API Gateway --> Lambda --------> image file | | | |-----------------------------------------------------------|
ãã®ãããªã¢ããªã±ã¼ã·ã§ã³ã¯ã以ä¸ã®2ç¹ã§ã³ã¹ãçã«ä¸å©ã«ãªãã ã¾ããLambda functionå ã§ã¤ã³ã¿ã¼ãããçµç±ã§ç»åããã§ãããããããfunctionå®è¡æéã®æéã¯åµ©ã¿ãã¡ã«ãªãã ç»åãã§ããå¦çã¨ç»åå¤æå¦çã並è¡å®è¡ããå®è¡æéãåæ¸ãããã¨ãé£ããã 次ã«ãI/Oå¤éåã«ããã1åã®functionå®è¡ã«ããã¦ãåæç»åãã§ããæ°ãå¢ãããåãå®è¡æéæéã§ã®å¦çå¹çãé«ãããã¨ãé£ããã ãªããªããã¦ã¼ã¹ã±ã¼ã¹ä¸ãåãHTTPãªã¯ã¨ã¹ãã«è¤æ°ã®ç»åURLãå«ãããããfunctionã®å¼æ°ã«è¤æ°ã®ç»åURLã渡ããªãããã ã
ä¸æ¹ãEC2ã¤ã³ã¹ã¿ã³ã¹ä¸ã§åããããªã¢ããªã±ã¼ã·ã§ã³ãå®è£ ããã¨ä»¥ä¸ã®å³ã®ããã«ãªãã
|----------------------------------------------------------| | internet | | --> (CDN) --> ALB(or NLB) --> EC2 ----------> image file | | | |----------------------------------------------------------|
EC2å®è£ ã§ã¯ãã¤ã³ã¹ã¿ã³ã¹ä¸ã§Webãµã¼ããåä½ãããLambdaå®è£ ã¨ã¯ç°ãªããè¤æ°ã®ãªã¯ã¨ã¹ãã1ã¤ã®ã¤ã³ã¹ã¿ã³ã¹ã§ä¸¦è¡ãã¦åãä»ããI/Oå¤éåã§ããã ãã¡ããã1ã¹ã¬ããã§åä½ããã·ãªã¢ã«ã¢ãã«ãªWebãµã¼ãã§ããã°ãåæã«1ã¤ã®ãªã¯ã¨ã¹ãããåãä»ããããªãããå¤å ¸çãªWebãµã¼ãã§ãã£ã¦ã並è¡ã¢ãã«ãæ¡ç¨ãã¦ãããã¨ãã»ã¨ãã©ã§ããã5 ãããã£ã¦ãEC2å®è£ (Webãµã¼ãå®è£ )ã§ã¯ãåãã©ã³ãã³ã°ã³ã¹ãã«å¯¾ãã¦ãè¤æ°ã®ç»åããã§ããã§ãããã¨ã«ãªããLambdaå®è£ ã¨æ¯è¼ãã¦ã³ã¹ãå¹çãããã
ã¢ããªã±ã¼ã·ã§ã³ä¾ã®ã³ã¹ã試ç®ä¾
å®éãã³ã¹ããã©ã®ç¨åº¦ã«ãªãã®ãã¨ããæè¦ãæ´ãããã«ãä»®ã®ãã©ã¡ã¼ã¿ã§è©¦ç®ããã¦ã¿ãã
å¹³åå®è¡æé 1000msãå¹³åãªã¯ã¨ã¹ãæ° 1000req/sã¨ãªãã±ã¼ã¹ãèããã ä¸çªå°ããã¡ã¢ãªå²ãå½ã¦éã§ãã128MBã®ã¨ããæé¡ã¯ç´$6000ã¨ãªãã å®è¡æéãå°ããããããã«ãã¡ã¢ãªå²ãå½ã¦éãNåã«å¢ããCPUæ§è½ã大ããè¨å®ããã¨ãç»åå¤æå¦çã¯éããªããããããã¯ã¼ã¯I/Oé¨åã®æéã¯ããããªããããå®è¡æéã¯1/Nã«ã¯ãªããªãã å¹³åãªã¯ã¨ã¹ãæ°ã10åã§ããã°ãã³ã¹ãã¯10åã«ãªããå¹³åãªã¯ã¨ã¹ãæ°ã1/10ã§ããã°ã³ã¹ãã1/10ã«ãªãã
ãã®è¨ç®ä¾ã軸ã«ãè¦è¦ãã®ããã¢ããªã±ã¼ã·ã§ã³ã®è² è·ãã©ã¡ã¼ã¿ããã¦ã¯ãã¦ãæ¯ä¾è¨ç®ããã¨ãã ãããã®ã³ã¹ãæè¦ãã¤ãããã¨æãã
èå¯
以ä¸ã®ä¾ããä¸è¬åããã³ã¹ã観ç¹ã§Lambdaã«ä¸åããªã¢ããªã±ã¼ã·ã§ã³æ§è³ªã¯ä»¥ä¸ã®ããã«ãªãã¨ããèå¯ããã¦ã¿ãã
- 1: Lambda functionã®å®è¡æéã®ãã¡ããããã¯ã¼ã¯I/Oæéãæ¯é çã§ãã
- 2: Lambda functionã®å®è¡çµäºãåæçã«å¾ ããªããã°ãªããªã
- 3: è¤æ°ã®ã¬ã³ã¼ããLambda functionã®å¼æ°ã«æ¸¡ããã¨ãã§ããªã
3ã«ã¤ãã¦ãåã«ãã«ã¯å¦çã§ããã¨ã³ããã¤ã³ããçããã ãã§è§£æ±ºããã®ã§ããã°ãåé¡ã«ãªããªãã 1ã¨3ã«ã¤ãã¦ããããã¯ã¼ã¯I/Oæéãæ¯é çã§ãã£ã¦ãã1ã¬ã³ã¼ãã®å¦çãI/Oå¤éåããå®è¡æéãå°ããã§ããã®ã§ããã°åé¡ã«ãªããªãã ããã«ã2.ã¨3.ã«ã¤ãã¦ã¯ãéåæã§ããã¨ãããã¨ã§ããã°ãKinesis Streamsã®ãããªãã¥ã¼ãæãã°ãLambda functionå´ã§ã¬ã³ã¼ããè¤æ°åæã«å¦çããI/Oå¤éåã§ããã
ããããã³ã¹ãçã«ä¸å©ãªæ§è³ªã®ã¢ããªã±ã¼ã·ã§ã³ã§ãã£ã¦ããLambdaã®æ¡ç¨ã«ããåææ§ç¯ã®æéã¨ãã®å¾ã®éç¨ã®æéãå¤§å¹ ã«åæ¸ãããããããå®éã«ã³ã¹ãã試ç®ãã¦ã¿ã¦æ¡ç¨å¯å¦ãå¤æãããã¨ãæã¾ããã åè¿°ã®Lambdaå®è£ ã§ã¯ãCDNã®ãã£ãã·ã¥ãããçãåä¸ããããã¨ã§è§£æ±ºã§ãããªã©ãã¢ã¼ããã¯ãã£å ¨ä½ã§ã³ã¹ããæããã°åé¡ãªãã±ã¼ã¹ãããã
Lambdaã®ãããªFaaSã«ããã¦ãå®æéã§ã¯ãªããCPUæéã§èª²éããã¢ã¼ããããã°ããããããããããªãã
ä»åã®è©±ã¯ãLambdaã¢ã¼ããã¯ãã£ã®åé¡ã§ã¯ãªããLambdaã®ã³ã¹ãã¢ãã«ã®åé¡ã¨ãããã EC2ä¸ã§Lambdaã®ãããªãã®ãèªåã§éç¨ããã¨ãã¦ãåãã¤ã³ã¹ã¿ã³ã¹ä¸ã«è¤æ°ã®functionã³ã³ãããåä½ããç¶æ ã¨ããã«ãããã»ã¹ã¢ãã«ã®Webãµã¼ããåä½ãã¦ããç¶æ ã¨ã§ã¯ãã©ã³ãã³ã°ã³ã¹ããããã®éç©åº¦ã¨ãã観ç¹ã§ã¯ãã»ã©å¤ãããªãã
ãã¨ãã
ãã®ãããªæ§è³ªããã¤ã¢ããªã±ã¼ã·ã§ã³ãç¾å®ã«ã©ãããããããããããªãããå®éãLambdaã§ã³ã¹ã試ç®ãã¦ã¿ãã¨é«ããªã£ãã¨ãã話ã社å ã§èããã®ã§ãå°ãä¸è¬åãã¦ãã®ãããªèå¯ããã¦ã¿ãã ããã社å wikiã§å ±æãã¦äººæ°ã ã£ãã®ã§ã社å¤å ±æãããã¨ã«ããã æè¿ã¯ãはてなWebオペレーションチームのテックリード - Hatena Developer Blog ã«ããããã«ã¢ã¼ããã¯ãã£ç¸è«ããã¦ããããããã®æ´»åã®ä¸ç°ã¨ãããã¨ã«ãªãã
é«è² è·ãªå¦çãå®ãããã®ã¯ä¸è¬ã«é£ããã®ã¯ãã¡ãããã®éãã§ãä»åæ¸ããã®ã¯ãEC2ã§æ®éã«ãã£ã¦ãã°å ¨ç¶å®ãã®ã«Lambdaã§æ®éã«ããã¨æããã«ã³ã¹ãå¹çãæªãã¨ããæ§é ã®è©±ããã¦ã¿ãã (ç»åå¦çã®ã³ã¹ãã®ç¨åº¦ã«ãããã©ãä»®ã«CPUã³ã¹ãã¯ããã»ã©ãªãã¨ãã¦)
— ãããã (@y_uuk1) 2017å¹´11æ6æ¥
Lambdaã§å®å¸¸çã«CPUãã¦ã³ããªãã¨ãããã¨ãEC2ã«æ¯ã¹ã¦é«ãã®ã§ã¯ã¨æã£ã¦ãã®ã ãã©ã1æéãããã®æéã ã¨ãããLambdaã®ã»ããå®ããããã(æ¬å½ã«? ãã¼ã¸ãã¢ã ã¨ã»ã¼ããªãããã
— ãããã (@y_uuk1) 2017å¹´11æ6æ¥
Webãµã¼ãã§ä»£è¡ã§ããã¨ãããã¨ã¯ããµã¼ãã¬ã¹ã¨ãã¦ã¯ãããã©FaaSã¨ãã¦ã¯ã¤ãã¤ããLambdaã¯ãDynamoDBã¨ãS3ã®ãããªãã¼ã¿ã¬ã³ã¼ãã«å¯¾ããã¤ãã³ããã¨ã«å¦çãããã¯ã§ããã¨ãããä¸çªãã£ãããã
— ãããã (@y_uuk1) 2017å¹´11æ6æ¥