Apexã使ã£ã¦AWS Lambdaã楽ã«ç®¡çããã
AWS Lambdaã§ãµã¼ãã¼ã¬ã¹æ¥½ããã§ãã¿ãªããã
ç§ã®æå±ããJXé信社ã§ã¯ãAWS Lambdaãçµæ§æ´»ç¨ãã¦ãã¾ããLambdaã®ãããã¤ç®¡çã®ããã«ãå人çã«ã¯Apexã¨ãããã¼ã«ã使ã£ã¦ããã®ã§ãä»åã¯ãã®ç´¹ä»ããããã¨æãã¾ãã
TL; DR
Lambda使ããªãApex使ãã
Lambdaãã£ã¦ã¦çºçããè¾ã¿
Lambdaãç¶ç¶ãã¦ä½¿ã£ã¦ããã¨ãã ããããããªè¾ã¿ãã§ã¦ãã¾ãã
- Lambdaé¢æ°ãå¢ãã¦ããã®ã§ç®¡çã大å¤
- Lambdaé¢æ°ãæ´æ°ããããã«å¿ è¦ãªã¹ããããå¤ããã
Lambdaé¢æ°ãæ´æ°ããããã«ã¯ãã ããã次ã®ä½æ¥ãçºçãã¦ãã¾ãã
- pip install ã¨ã npm install ã¨ããã¦ãä¾åã©ã¤ãã©ãªãã¤ã³ã¹ãã¼ã«
- ã½ã¼ã¹ã³ã¼ãã¨ä¾åã©ã¤ãã©ãªãä¸ã¤ã®zipã«æ¢±å
- S3ã«ã¢ãããã¼ã
- Lambdaé¢æ°ãæ´æ°
- å¿ è¦ã§ããã°ãã¨ã¤ãªã¢ã¹ãæ´æ°
- å®è¡ãã¦åä½ã確èª
- CloudWatch Logsãè¦ã¦åä½ã確èª
ã¨ããã¨ããã§ããããèªååããããªã£ã¦ããããã§ããããããèªååãã¦ãããOSSãæ¢ã«åå¨ãã¦ãã¾ããæ¬ç¨¿ã§ç´¹ä»ããApexããã®ãããªãã¼ã«ã®ä¸ã¤ã§ããããã§ã¯ããããã¤ç®¡çãã¼ã«ãã¨å¼ç§°ãã¾ãã
ãããã¤ç®¡çãã¼ã«ã®åé¡
ãããã¤ç®¡çãã¼ã«ã¯ããã¤ãããã¾ããawesome-servelessã¨ãããªãã¸ããªãåèã«ãªãã¾ããããããã¤ç®¡çãã¼ã«ãåé¡ããã¨ã
- ç¹å®ã®è¨èª(ã©ã³ã¿ã¤ã )ã«ä¾åããããå ¨ã¦ã®è¨èªã«å¯¾å¿ããã
- ã¢ãããã¼ãã ããããã以å¤ã®æ©è½ãåãã£ã¦ããã
- ã¤ã³ãã©ç®¡çãããæ©è½ãåãã£ã¦ããã
- API Gatewayã®ç®¡çãããæ©è½ãåãã£ã¦ããã
- ä¾åã©ã¤ãã©ãªã®ç®¡çãããæ©è½ãåãã£ã¦ããã
- é¢æ°ãå¼ã³åºãããããã°ã確èªãããããã¼ã«ããã¯ã®æ©è½ãããã
Apexã¯ãã®ä¸ã§ãããå ¨ã¦ã®è¨èªã«å¯¾å¿(ã¨ãããGoã«ãã対å¿ï¼)ããã¢ãããã¼ã以å¤ã«ããã°ç¢ºèªãå¼ã³åºãæ©è½ãå«ãããTerraformã¨é£æºãã¦ã¤ã³ãã©ç®¡çãã§ããããAPI Gatewayã®ç®¡çãä¾åã©ã¤ãã©ãªç®¡çã®æ©è½ã¯åãã£ã¦ãªããã¨ããåé¡ã®ãã¼ã«ã§ãã
åèã«ããã¹ããã¼ã¸
- å ¬å¼ãã¼ã¸
- Markdownã«ããåæ©è½è§£èª¬
- ãµã³ãã«
- ã¯ã©ã¹ã¡ã½ããããã®ããã°
ä½ãå°ã£ãããGithubã§æ¤ç´¢ããã¨è¯ãã§ãã
使ãæ¹ã®ç´¹ä»
Apexã«ã¯initã¹ã¯ãªãããå ¥ã£ã¦ããã®ã§ãç°¡åã«å§ãããã¨ãã§ãã¾ãã使ãæ¹ãç¥ãããã«ãã¾ãã¯ç©ºã®ããã¸ã§ã¯ãããå§ããã¨è¯ãã§ãã
# ã¤ã³ã¹ãã¼ã« curl https://raw.githubusercontent.com/apex/apex/master/install.sh | sh # åæå apex init
apex init
ããã¨ã次ã®ãããªãã£ã¬ã¯ããªãã§ãã¾ãã
. âââ functions â  âââ hello â  âââ index.js âââ infrastructure â  âââ dev â  â  âââ main.tf â  âââ modules â  âââ iam â  âââ iam.tf â  âââ outputs.tf âââ project.json
ãã®ãã¡ãproject.jsonãè¨å®ãã¡ã¤ã«ã§ãinfrastructureã¯terraformã§æ±ãã³ã¼ããfunctionsãLambdaé¢æ°ã§ããfunctionsã®ä¸ã®ãã£ã¬ã¯ããªã¨ããããã¤ãããLambdaé¢æ°ããä¸å¯¾ä¸ã«å¯¾å¿ãã¾ã(ã¤ã¾ãã {ããã¸ã§ã¯ãå}_hello
ã¨ããLambdaé¢æ°ããããã¤ããã)ã
ããã©ã«ãã®helloé¢æ°ã¯ index.js
ãå
¥ã£ã¦ããã®ã§ãå½ç¶nodeã©ã³ã¿ã¤ã ãé¸æããã¾ããmain.py
ã ã£ããPythonãé¸æããã¾ããã©ã³ã¿ã¤ã ã®æ¨è«ã«ã¼ã«ã¯ã½ã¼ã¹ã³ã¼ãã§ç¢ºèªã§ãã¾ãããfunction.json
ãå¤æ´ãããã¨ã§ãã©ã³ã¿ã¤ã ãããã³ãã©ã¼é¢æ°ãæ示çã«æå®ãããã¨ãã§ãã¾ã(ãã®ãããã¯ãGithubã§æ¤ç´¢ããã¨ãããããããã)ã
Terraformã¯ãterraform hogehoge
ã®ä»£ããã«ã apex infra hogehoge
ã®ããã«æ±ãã¾ãããããããã¨ã§ãé¢æ°åã®ARNã¨ãããã¾ãæãã«Terraformã«å¼ã渡ããã¾ãã
ãããã¤
apex deploy
ããããã¨ãããã©ã«ã㧠current
ã¨ããã¨ã¤ãªã¢ã¹ãè²¼ããã¦ãå
¨ã¦ã®é¢æ°ããããã¤ããã¾ã(ã³ã¼ããå¤æ´ããã¦ãªããã°ããã¡ãããããã¤ãããªã)ã--alias
ãã©ã°ã¨ããã¤ããã¨ãã¨ã¤ãªã¢ã¹ãæ示çã«å¤ãããã¨ãåºæ¥ã¾ãã
å¼ã³åºã
apex invoke hello < event.json
渡ãJSONãæå®ãã¦ãä¸è¨ã®ããã«å¼ã³åºãã¾ãã
ãã°ã®ç¢ºèª
å¼ã³åºããéãå½ç¶ãã°ã確èªãããã§ãããã¨ãããã¨ã§ããã°ã®ç¢ºèªã¯ä¸è¨ã®ããã«ãã¾ããtail -f
ã®ããã«ãããå ´åã¯ã-f
ãã©ã°ãã¤ãã¾ã
apex logs hello # ã¾ã㯠apex logs -f hello
Apexã®æªãã¨ãã
- dev, prod ã®ãããªç°å¢ç®¡çããå¥ã¢ã«ã¦ã³ãã§ãããã¨ãåæã«ãªã£ã¦ãããã¤ã¾ããLambdaã®ã¨ã¤ãªã¢ã¹ã§ãã£ãããAPI Gatewayã®ã¹ãã¼ã¸ã§ç°å¢ç®¡çããããã¨ããã¾ãæ³å®ããã¦ããªã
- Terraformã使ã£ããã¨ããªã人ã«ã¯ãTerraformã®å¦ç¿ã³ã¹ããé«ãããã ããTerraformã使ããã«GUIã§ããããã¤ã³ãã©ç®¡çããããã¨ããã¡ããã§ãã
- Pythonã®pipã®ãµãã¼ããç¹ã«ãªã(ã¨ãããnode以å¤ã®ãµãã¼ããèãï¼ ã¡ãªã¿ã«ä½è ã¯JSçéã§æå㪠@tj ãã)
Apexã®Tips
Apexã®æªãã¨ãããããã¦ã¿ãã®ã§ãããåé¿çãããã¾ãããããªTipsãç´¹ä»ãã¾ãã
å¼·å¶çã«ãããã¤ãã
ãããã¤ããéã--alias
ã¨ãããã©ã°ãã¤ããã¨ã¨ã¤ãªã¢ã¹ãcurrent
以å¤ã«ã§ããã¨ç´¹ä»ãã¾ããããããã次ã®ããã«ããã¨ãã³ã¼ããæ´æ°ããã¦ããªãããã«ãããã¤ããã¾ããã
# currentãæ´æ° apex deploy # devãæ´æ°ããããããªãï¼ apex deploy --alias dev
ããããã¨ãã¯ã--set
ã¨ããç°å¢å¤æ°ãå¤æ´ããããã®ãã©ã°ã使ã£ã¦ãå¼·å¶çã«ã³ã¼ããå¤ãã£ãç¶æ
ãä½ã£ã¦ãããã¤ããã¨è¯ãã§ãã
# currentãæ´æ° apex deploy # ç°å¢å¤æ°ãå ¥ããããã«ãã³ã¼ããå¤æ´ãããã®ã§ãdevãæ´æ°ãããï¼ apex deploy --alias dev --set tekitounahensuu=dev
Pythonã§ä½¿ã
å ã»ã©ãè¿°ã¹ãã¨ãããpipã¨ãrequirements.txt ã®ããã®ä»çµã¿ã¯ç¹ã«ããã¾ããã
Apexã§ã¯ã functions
ã¨ãããã£ã¬ã¯ããªè¦ç´ã§ãé¢æ°ããããã¤ãã¾ããã¤ã¾ããfunctions ã¨ãããã£ã¬ã¯ããªããã¾ã使ã£ã¦ãããã¨ããããã§ãã¾ããè¨ãæããã¨ãfunctionsãã£ã¬ã¯ããªã¯ãã«ãç¨ãã£ã¬ã¯ããª(ã³ãããããªããã£ã¬ã¯ããª)ã¨ãã¦å²ãããã¾ããã
ä¾ãã°ã次ã®ãããªãã£ã¬ã¯ããªæ§æãèãã¾ãã
. âââ functions â  âââ hello â  âââ .gitkeep âââ infrastructure âââ project.json âââ src âââ hello âââ main.py âââ requirements.txt
src
ã¨ãããã£ã¬ã¯ããªãä½ãã¾ãããä¾ãã°ãapex deploy
ããåã«ã次ã®ãããªã¹ããããè¸ãã§ãããã°ããrequirements.txtã®ä¾åé¢ä¿ãã¤ã³ã¹ãã¼ã«ããã½ã¼ã¹ã³ã¼ãã®ãã£ã¬ã¯ããªã綺éºã«ãããã¨ããï¼ã¤ã®è¦æãããªãããã¾ãã
1. pip install -r requirements.txt -t functions/hello ãã¦ãä¾åã©ã¤ãã©ãªãã¤ã³ã¹ãã¼ã« 2. cp -r hello functions/ ãã¦ãã½ã¼ã¹ã³ã¼ããfunctionsä¸ã«ã³ãã¼ 3. apex deploy ããã¨ãfunctions/hello ã®ä¸èº«ããããã¤ãã¦ããã
ã¡ãªã¿ã«ãApexã«ã¯hookã®ä»çµã¿ãããã®ã§ãæ´»ç¨ããã¨è¯ãã¨æãã¾ãã
Javaã§ä½¿ã
Javaã®å ´åã¯ãapex.jar
ã¨ãããã¡ã¤ã«ãããã°ãããããããã¤ãã¾ããã¤ã¾ãããããªæãã
. âââ functions â  âââ hello â  âââ apex.jar âââ infrastructure âââ project.json âââ src âââ hello âââ é©å½ã«Javaããã¸ã§ã¯ãã¨ãä½ãã°ãããããªã
åãããã«ãã½ã¼ã¹ã³ã¼ã㯠functions
以å¤ã§ç®¡çãã¾ããããjarããä½ãã°è¯ãã®ã§ãScalaã ããã¨Closureã ããã¨Groovyã ããã¨(ãã¶ã)åãã¾ã(Scalaãããã£ããã¨ãªããã©)ã
APIã®ç®¡çã«ã¯Terraformã使ããªã
çªç¶ã§ãããTerraformã®API Gatewayã®ç®¡çã¯è²§å¼±ã§ãã貧弱ã¨ãããããã¹ãã¼ã¸ã¸ãããã¤ããããã¨ãããã¨ãæ³å®ãã¦ããªãä½ããã ã¨æãã¾ãã
API Gatewayã¯å¹¸ããªãã¨ã«Swaggerã«ãã管çãã§ããããã«ãªã£ã¦ãã¾ãããããã¤ã³ãã¼ãããããã®å ¬å¼ãã¼ã«ãããã®ã§ãããã使ãã¨è¯ãã§ããã(awslabs/aws-apigateway-importer: Tools to work with Amazon API Gateway, Swagger, and RAML)ã
ã¡ãªã¿ã«Swaggerã«ããã¤ã³ãã¼ãã«å¯¾å¿ããäºå®ã¯ããã¿ããã§ãã
ã¾ã¨ã
ããããæ¸ãã¦ãã¾ããããApexã¨ãããã¼ã«èªä½ãé«éã«åããã便å©ã ããç¸ããå°ãªãããããããã§ãã
æ¥æ¬ã§ä¸çªApexã§è¦ããã§ãããããããªãã®ã§ãä½ã質åãªã©ããã°Twitterã¨ãã§ã¡ã³ã·ã§ã³ãã ããã
ã¨ããã§ãJXé信社ã§ã¯ç´ æµãªPythonistaãç´ æµãªiOSã¨ã³ã¸ãã¢åéä¸ã§ãã