PHPでもサーバーレスしたい

 PHPでもサーバーレスしたい

2017PHPカンファレンス関西-PHPでもサーバーレスしたい

B51ca7a51ae1fd06bc536fe83e6113e2?s=128

Kaz Watanabe

July 15, 2017
Tweet

Transcript

  1. 1)1Ͱ΋αʔόʔϨε͍ͨ͠ 1)1ΧϯϑΝϨϯεؔ੢ ߹ಉձࣾEFDS୅දࣾһ ౉ลҰ޺ !LB[@

  2. WHO!? ౉ลҰ޺(@kaz_29) ߹ಉձࣾ decr ୅දࣾһ WebΞϓϦέʔγϣϯͷ։ൃ iOSΞϓϦέʔγϣϯͷ։ൃ Ϋϥ΢υΠϯϑϥͷߏஙɾӡ༻

  3. ຊ೔ͷ಺༰ w αʔόʔϨεΞʔΩςΫνϟʔʁ w "[VSF'VODUJPOTͷ֓ཁ w 1)1Ͱͷ։ൃͷྫ w ฐࣾͰͷར༻ྫ

  4. αʔόʔϨεΞʔΩςΫνϟʔʁ

  5. None
  6. r4FSWFSMFTT$POG50,:0
 IUUQUPLZPTFSWFSMFTTDPOGJP lαʔόʔϨεΞʔΩςΫνϟ͸ɺ։ൃऀͷ૑଄ੑΛ ૉૣ͘8FC΍ϞόΠϧΞϓϦέʔγϣϯͱ࣮ͯ͠ݱ ͠ɺεέʔϥϏϦςΟ΍ηΩϡϦςΟɺΠϯϑϥͷอ कͱ͍ͬͨଟ਺ͷྗ࢓ࣄ͔Βղ์͞ΕΔͨΊͷ৽ͨͳ ύϥμΠϜγϑτͰ͢ɻz

  7. αʔόʔ؀ڥͷมԽ w Ϋϥ΢υͷొ৔Ͱॊೈʹੜ੒ɾഁغ͕Ͱ͖Δ༷ʹͳͬͨɻ w ελϯυΞϩϯΞϓϦ͔Βͷ୤٫ϚΠΫϩαʔϏεͷོ੝ w αʔόʔ͕ͳ͘ͳΔͱ͍͏͜ͱͰ͸ͳ͍ɻ։ൃऀ͕αʔόʔʹ͍ͭͯ୔ࢁͷ ͜ͱΛߟ͑ͳͯ͘ྑ͘ͳΔͱ͍͏͜ͱɻ IUUQTSFBEXSJUFDPNXIZUIFGVUVSFPGTPGUXBSFBOEBQQTJTTFSWFSMFTT

  8. αʔόʔ؀ڥͷมԽ IUUQTCMPHTNTEONJDSPTPGUDPNB[VSFTFDVSJUZCFTUQSBDUJDFTGPSVQEBUJOHWJSUVBMNBDIJOFTJONJDSPTPGUB[VSFJBBTEFQMPZNFOUT Best Practices for Updating Virtual Machines in Microsoft

    Azure IaaS Deployments
  9. αʔόʔϨεͱ͸ʁ w αʔόʔΛҙࣝ͠ͳ͍Ͱྑ͍ w ؆୯ʹεέʔϧͰ͖Δ w ར༻ྔʹΑΔ՝ۚ

  10. Α͘ޠΒΕΔϢʔεέʔε w 8FCΞϓϦέʔγϣϯ w ϞόΠϧόοΫΤϯυ w *P5όοΫΤϯυ w ετϦʔϜϓϩηογϯά΍&5- w

    νϟοτϘοτɺίάχςΟϒαʔϏε w όονॲཧ w ӡ༻ͷࣗಈԽ
  11. ࣄྫ೔ܦ৽ฉ IUUQTBXTBNB[PODPNKQTPMVUJPOTDBTFTUVEJFTOJLLFJ

  12. ࣄྫGBVMUMJOF IUUQLMPXIBUFOBCMPHDPNFOUSZ 1)1ΧϯϑΝϨϯε෱ԬͰGBVMUMJOFʹ͍ͭͯൃද͖ͯ͠·ͨ͠QIQDPOGVL

  13. Α͘ޠΒΕΔϢʔεέʔε w 8FCΞϓϦέʔγϣϯ w ϞόΠϧόοΫΤϯυ w *P5όοΫΤϯυ w ετϦʔϜϓϩηογϯά΍&5- w

    νϟοτϘοτɺίάχςΟϒαʔϏε w όονॲཧ w ӡ༻ͷࣗಈԽ ࠷ॳʹࢼ͢ͷʹΦεεϝʂ
  14. Lambda

  15. rIUUQEPDTBXTBNB[PODPNKB@KQMBNCEBMBUFTUEHXFMDPNFIUNM lඞཁͳૢ࡞͸ɺ"84-BNCEB͕αϙʔτ͢Δ͍ͣ Ε͔ͷݴޠ ݱࡏ͸/PEFKTɺ+BWBɺ$͓Αͼ 1ZUIPO ͰίʔυΛࢦఆ͢Δ͚ͩͰ͢ɻ z

  16. lඞཁͳૢ࡞͸ɺ"84-BNCEB͕αϙʔτ͢Δ͍ͣ Ε͔ͷݴޠ ݱࡏ͸/PEFKTɺ+BWBɺ $͓Αͼ1ZUIPO ͰίʔυΛࢦఆ͢Δͩ ͚Ͱ͢ɻ z "84-BNCEB rIUUQEPDTBXTBNB[PODPNKB@KQMBNCEBMBUFTUEHXFMDPNFIUNM ͸ͳ͍

  17. "[VSF'VODUJPOT

  18. rIUUQTEPDTNJDSPTPGUDPNKBKQB[VSFB[VSFGVODUJPOTGVODUJPOTPWFSWJFX l$ɺ'ɺ/PEFKTɺ1ZUIPOɺ1)1ɺ#BUDIɺ #BTIɺͦͷଞ࣮ߦՄೳͳݴޠΛ࢖ͬͯؔ਺Λهड़Ͱ ͖·͢ɻz Ͱॻ͚Δ

  19. "[VSF'VODUJPO

  20. "[VSF'VODUJPOT w ΊͬͪΌ͍҆ w ೖྗɾग़ྗͷ࿈ܞػೳ͕๛෋ τϦΨʔɾόΠϯσΟϯά  w 7JTVBM4UVEJPΛ࢖ͬͯͷσόοά ϩʔΧϧɾϦϞʔτʣ

    w ΦϯϓϨͰ΋͍͚Δ "[VSF4UBDL  w ϥϯλΠϜ͕Φʔϓϯ IUUQTHJUIVCDPN"[VSF"[VSF'VODUJPOT  w (JUIVC͔ΒσϓϩΠͰ͖Δ
  21. τϦΨʔͱόΠϯσΟϯά IUUQTEPDTNJDSPTPGUDPNKBKQB[VSFB[VSFGVODUJPOTGVODUJPOTUSJHHFSTCJOEJOHT

  22. -PHJD"QQT

  23. -PHJD"QQ

  24. .JDSPTPGU'MPX IUUQTqPXNJDSPTPGUDPN

  25. "[VSF'VODUJPOͷػೳ

  26. "[VSF'VODUJPO

  27. Ϟχλʔ

  28. 'VODUJPO1SPYZ QSFWJFX w ෳ਺ͷ'VODUJPOΛ౷߹Ͱ͖Δ w "1*.BOBHFNFOUͷ୅ΘΓʹ࢖͑Δ w 'VODUJPOҎ֎΁ͷϦΫΤετ΋ઃఆͰ͖Δ "[VSF"1*NBOBHFNFOU "[VSF'VODUJPOT

    "[VSF'VODUJPOT1SPYZ "[VSF'VODUJPOT ͪΐͬͱ ͓ߴ͍
  29. σϓϩΠϝϯτεϩοτ QSFWJFX #MVF(SFFO%FQMPZNFOU͕؆୯ʹͰ͖Δ

  30. $POTPMF

  31. "QQMJDBUJPO*OTJUF QSFWJFX ೖΕΔ͔ʜ IUUQTB[VSFNJDSPTPGUDPNKBKQVQEBUFTB[VSFGVODUJPOTOPXJOUFHSBUFEXJUIBQQMJDBUJPOJOTJHIUT

  32. %VSBCMF'VODUJPOT Ћ൛ w ෳ਺ͷ'VODUJPOͷ࿈ܞΛൺֱత؆୯ʹ࣮૷Ͱ͖Δ w 'VODUJPODIBJOJOH w 'BOPVU'BOJO w "TZOD)551"1*T

    IUUQTB[VSFHJUIVCJPB[VSFGVODUJPOTEVSBCMFFYUFOTJPOJOEFYIUNM IUUQRJJUBDPN5TVZPTIJ6TIJP!HJUIVCJUFNTFBDCCCGEC
  33. 'VODUJPO$IBJOJOH public static async Task<object> Run(DurableOrchestrationContext ctx) { try {

    var x = await ctx.CallFunctionAsync<object>("F1"); var y = await ctx.CallFunctionAsync<object>("F2", x); var z = await ctx.CallFunctionAsync<object>("F3", y); return await ctx.CallFunctionAsync<object>("F4", z); } catch (Exception) { // error handling/compensation goes here } }
  34. public static async Task Run(DurableOrchestrationContext ctx) { var parallelTasks =

    new List<Task<int>>(); // get a list of N work items to process in parallel object[] workBatch = await ctx.CallFunctionAsync<object[]>("F1"); for (int i = 0; i < workBatch.Length; i++) { Task<int> task = ctx.CallFunctionAsync<int>("F2", workBatch[i]); parallelTasks.Add(task); } await Task.WhenAll(parallelTasks); // aggregate all N outputs and send result to F3 int sum = parallelTasks.Sum(t => t.Result); await ctx.CallFunctionAsync("F3", sum); } 'BOPVU 'BOJO
  35. "TZOD)551"1*T public static async Task<HttpResponseMessage> Run( HttpRequestMessage req, DurableOrchestrationClient starter,

    string functionName, TraceWriter log) { // Function name comes from the request URL. // Function input comes from the request content. dynamic eventData = await req.Content.ReadAsAsync<object>(); string instanceId = await starter.StartNewAsync(functionName, eventData); log.Info($"Started orchestration with ID = '{instanceId}'."); return starter.CreateCheckStatusResponse(req, instanceId); }
  36. -JHIUXFJHIU"DUPST public static async Task Run(DurableOrchestrationContext ctx) { int counterState

    = ctx.GetInput<int>(); string operation = await ctx.WaitForExternalEvent<string>("operation"); if (operation == "incr") { counterState++; } else if (operation == "decr") { counterState--; } ctx.ContinueAsNew(counterState); }
  37. )VNBO*OUFSBDUJPOBOE5JNFPVUT public static async Task Run(DurableOrchestrationContext ctx) { await ctx.CallFunctionAsync("RequestApproval");

    using (var timeoutCts = new CancellationTokenSource()) { DateTime dueTime = ctx.CurrentUtcDateTime.AddHours(72); Task durableTimeout = ctx.CreateTimer(dueTime, timeoutCts.Token); Task<bool> approvalEvent = ctx.WaitForExternalEvent<bool>("ApprovalEvent"); if (approvalEvent == await Task.WhenAny(approvalEvent, durableTimeout)) { timeoutCts.Cancel(); await ctx.CallFunctionAsync("HandleApproval", approvalEvent.Result); } else { await ctx.CallFunctionAsync("Escalate"); } } }
  38. 'VODUJPOTͷ࡞Γํ

  39. όΠϯσΟϯάͷ࢖͍ํ <?php // ೖྗόΠϯσΟϯάͷಡࠐΈ $data = file_get_contents(getenv(‘req’)); // ग़ྗόΠϯσΟϯά΁ͷॻࠐΈ file_put_contents(getenv(‘res’),

    $data); <?php var_dump(getenv('res')); string(72) “D: \local\Temp\Functions\Binding\[UUID]\res” ੲջ͔͍͠$(*෩
  40. ࡞Γํ w 'VODUJPO"QQΛ࡞Δ w 'VODUJPO࡞Δ w ίʔυΛॻ͘ w ΞϓϦͷઃఆ w

    ؀ڥม਺Λઃఆ w ࣮ߦ͢Δ
  41. ࡞Γํ w 'VODUJPO"QQΛ࡞Δ w 'VODUJPO࡞Δ w ίʔυΛॻ͘ w ΞϓϦͷઃఆ w

    ؀ڥม਺Λઃఆ w ࣮ߦ͢Δ
  42. ࡞Γํ w 'VODUJPO"QQΛ࡞Δ w 'VODUJPO࡞Δ w ίʔυΛॻ͘ w ΞϓϦͷઃఆ w

    ؀ڥม਺Λઃఆ w ࣮ߦ͢Δ
  43. ࡞Γํ w 'VODUJPO"QQΛ࡞Δ w 'VODUJPO࡞Δ w ίʔυΛॻ͘ w ΞϓϦͷઃఆ w

    ؀ڥม਺Λઃఆ w ࣮ߦ͢Δ
  44. ࡞Γํ w 'VODUJPO"QQΛ࡞Δ w 'VODUJPO࡞Δ w ίʔυΛॻ͘ w ΞϓϦͷઃఆ w

    ؀ڥม਺Λઃఆ w ࣮ߦ͢Δ
  45. ࡞Γํ w 'VODUJPO"QQΛ࡞Δ w 'VODUJPO࡞Δ w ίʔυΛॻ͘ w ΞϓϦͷઃఆ w

    ؀ڥม਺Λઃఆ w ࣮ߦ͢Δ
  46. Έ͍ͨͳͷ͸ɺ΍ͬͯΈͨܥͰ Α͘ॻ͔Ε͍ͯ·͢ɻ

  47. Ͱ΋ʜ

  48. ςετ͸⁉

  49. 1PSUBMͰ௚઀ ίʔυमਖ਼⁉

  50. None
  51. 'VODUJPOͷσόοάςετ w 8JOEPXTͳΒʜ w ୯ମςετ΋͍ͨ͠

  52. 'VODUJPOͷσόοάςετ ೖɾग़ྗνΣοΫͳΒͰ͖ͦ͏

  53. αϯϓϧ

  54. 0VUHPJOH8FCIPPLT IUUQ5SJHHFS IUUQPVUCJOEJOH

  55. 'VODTJPOͷߏ੒  ᵓᴷᴷIPTUKTPO ᵓᴷᴷGVOD ᴹᵓᴷᴷGVODUJPOKTPO ᴹᵋᴷᴷSVOQIQ ᵋᴷᴷGVOD ᵓᴷᴷGVODUJPOKTPO ᵋᴷᴷSVOQIQ EJSFDUPSJFT

    pMFT 'VODUJPOຖʹσΟϨΫτϦ τϦΨʔɾόΠϯσΟϯάͳͲͷఆٛ 'VODUJPOͷΤϯτϦϙΠϯτ ϥϯλΠϜݻ༗ͷߏ੒ IUUQTEPDTNJDSPTPGUDPNKBKQB[VSFB[VSFGVODUJPOTGVODUJPOTUSJHHFSTCJOEJOHT
  56. αϯϓϧ'VODUJPOͷߏ੒ . ├── composer.json ├── composer.lock ├── slackbot │ ├──

    function.json │ └── run.php ├── src │ ├── Functions │ │ └── SlackBot.php │ └── Lib │ └── AzureFunction.php ├── tests │ ├── Lib │ │ └── TestCase.php │ └── TestCase │ └── Functions │ └── SlackBotTest.php ├── tmp └── vendor ├── autoload.php … 'VODUJPOͷσΟϨΫτϦ τϦΨʔɾόΠϯσΟϯάͳͲͷఆٛ 'VODUJPOͷΤϯτϦϙΠϯτ 'VODUJPOຊମ ؆୯ͳϥούʔΫϥε ςετΫϥε IUUQTHJUIVCDPNLB[QIQDPOLBOTBJB[VSFGVODUJPO
  57. GVODUJPOKTPO { "bindings": [ { "type": "http", "direction": "out", "name":

    "res" }, { "type": "httpTrigger", "name": "req", "authLevel": "function", "methods": [ "post" ], "direction": "in" } ], "disabled": false }
  58. SVOQIQ <?php require dirname(__DIR__) . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR

    . 'autoload.php'; use App\Functions\SlackBot; $proc = new SlackBot(); $proc->run();
  59. 4MBDL#PUQIQ <?php namespace App\Functions; use App\Lib\AzureFunction; class SlackBot extends AzureFunction

    { public function __construct($req = 'req', $res = 'res') { $this->init(getenv($req), getenv($res)); } … }
  60. 4MBDL#PUQIQ public function run() { $params = $this->httpParams(); if (!isset($params['user_name'])

    || $params['user_name'] === 'slackbot') { return; } $response = [ 'text' => '', 'username' => 'phpconkansai2017demo', 'icon_emoji' => ':phpkansai:', 'attachments' => [[ "color" => '#00BFFF', // blue "title" => 'ϝοηʔδΛड৴͠·ͨ͠', "fields" => [[ … ]] ]] ]; $this->writeResponse(json_encode($response)); }
  61. 4-BDL#PU5FTUQIQ /** * @test */ public function ௨ৗͷॻ͖ࠐΈͷςετ() { $params

    = [ 'token' => 'DUMMY', 'user_name' => 'userA', 'text' => 'Hello Azure Function' ]; $this->makeHttpRequest($params); $this->makeResponse(); …
  62. 4-BDL#PU5FTUQIQ $this->slackBot = new SlackBot(); $this->slackBot->run(); $result = json_decode($this->getResponse(), true);

    $expected = [ 'username' => 'phpconkansai2017demo', 'icon_emoji' => ':phpkansai:', 'text' => '', 'attachments' => [[ "color" => '#00BFFF', "title" => 'ϝοηʔδΛड৴͠·ͨ͠', "fields" => [[ // লུ ]] ]] ]; $this->assertEquals($expected, $result); }
  63. 1)16OJUͰςετ

  64. None
  65. ฐࣾͰͷࣄྫ

  66. ӡ༻πʔϧ w J04ΞϓϦͷόοΫΤϯυ w 4MBTI$PNNBOEͰεςʔδϯάαʔόͷىಈɾఀࢭ w λΠϚʔͰ࣌ʹࣗಈఀࢭ w ࣗಈఀࢭͷԆ௕

  67. ߏ੒ 4MBTI$PNNBOE IUUQ5SJHHFS 2VFVF "1*࣮ߦ ىಈఀࢭ "1*࣮ߦ ঢ়ଶऔಘ UJNFS5SJHHFS ݁Ռ௨஌

    ॲཧ։࢝௨஌ ஗Ԇ2VFVF
  68. ࣾ಺޲͚πʔϧ w ݄ॳʹΫϥ΢υܥͷ݄࣍ར༻ྉۚΛ4MBDLʹ௨஌

  69. ·ͱΊ

  70. 'VODUJPOΛ࢖ͬͯΈͯ w όονͱ͔ॏ͍ॲཧΛͤ͞Δʹ͸ͱͯ΋ศར దࡐదॴ w ΞϓϦΛ1)1Ͱॻ͍͍ͯΔͳΒಉ͡ݴޠͰ͔͚Δͷ͸ָ w .BDͰ΋ී௨ʹ։ൃͰ͖Δ w %VSBCMF'VODUJPOT͸ظ଴େ

  71. ՝୊ w 4FSWFSMFTT'SBNFXPSLͰσϓϩΠ͍ͨ͠ w IUUQ5SJHHFS͸ɺैྔ՝ۚϓϥϯͩͱਏ͍ ࠓճͷྫͩͱɺ4MBDLଆͰλΠϜΞ΢τ͢Δ͜ͱ͕͋Δ

  72. ࠓ೔ͷαϯϓϧ IUUQTHJUIVCDPNLB[ QIQDPOLBOTBJB[VSFGVODUJPO

  73. "[VSF'VODUJPO݁ߏ͍͍Ͱ͢ IUUQTGVODUJPOTB[VSFDPNUSZ

  74. 'JO