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

Implementing PHP as a Custom Runtime on AWS Lambda

Implementing PHP as a Custom Runtime on AWS Lambda

A fifteen minute presentation I gave at the 24th Japan AWS User's Group in Sapporo (on 2019/9/12) describing the requirements and necessary steps to fully implement a custom runtime on AWS Lambda. From building a PHP binary from source using the same AMI/runtime environment as Lambda, to writing a complete event loop handler is presented in detail.

(Note: This presentation is only in Japanese, sorry.)

3a488be96753f7e2232f3a3d79b83083?s=128

Michael Tedder

September 12, 2019
Tweet

Transcript

  1. "84-BNCEBΧελϜϥϯλΠϜ 1)1ݴޠͷ࣮૷ גࣜձࣾίʔϧυɾϑϡʔδϣϯ ςομʔɹϚΠέϧ
 5FDIOJDBM%JSFDUPS$&0 ୈճ+"846(ࡳຈษڧձ ೥݄೔ ໦ KBXTVH

  2. $ whoami KBXTVH  ςομʔɹϚΠέϧ ࡀ   ೥Ҏ্ήʔϜ։ൃ ίϯγϡʔϚήʔϜػ


    ϞόΠϧ୺຤1$0DVMVT3JGU73ʣ  Ͱ͖Δ͜ͱΞϓϦ όοΫΤϯυ։ൃήʔϜΤϯδϯ։ൃ
 Πϯϑϥઃܭ%FW0QT $*$% "1*ΠϯςάϨʔγϣϯ  Α͘࢖͏"84αʔϏε-BNCEB"1*(BUFXBZ3%4 %ZOBNP%#(BNF-JGU&$&$34*".  michael.tedder42 !@'BMLFO
  3. ࠓ೔࿩͢͜ͱ KBXTVH  -BNCEBͷΧελϜϥΠϯλΠϜΛ࣮૷͢Δʹ
 ඞཁͳ͜ͱʢݴޠؔ܎ͳ͘ʣ  গ͚ͩ͠1)1ݴޠͷιʔε͸ग़ͯ͘Δ  ςΫχΧϧͳ෦෼͕एׯଟ͍ͷͰɺΘ͔Βͳ͍͜ͱ ͋Ε͹࣭໰λΠϜຢ͸4/4Ͱ͓౴͑͠·͢ʂ

  4. -BNCEBͱ͸ʁ KBXTVH  ιʔείʔυʢ σʔλʣΛ[JQϑΝΠϧʹ·ͱΊͯɺ
 ؔ਺ͱ࣮ͯ͠ߦͰ͖ΔʢαʔόʔϨεʣ  ؔ਺ʹ౉͢ύϥϝʔλͱ໭Γ஋͸+40/  "1*(BUFXBZͱ࿈ܞ͢Ε͹)5514Ͱ-BNCEBݺͼग़ͤΔ

     ݱࡏରԠϥϯλΠϜ1ZUIPO/PEFKT+BWB3VCZ (P/&5$PSF $
  5. ϨΠϠʔͱΧελϜϥϯλΠϜ৽ػೳ KBXTVH  IUUQTBXTBNB[PODPNKQBCPVUBXTXIBUT OFXBXTMBNCEBOPXTVQQPSUT DVTUPNSVOUJNFTBOEMBZFST

  6. ϨΠϠʔͱΧελϜϥϯλΠϜ৽ػೳ KBXTVH  ϨΠϠʔ͸ؔ਺ͱಉ༷ɺιʔείʔυʢ σʔλʣΛ[JQ ϑΝΠϧʹ·ͱΊΔʢϥΠϒϥϦ༻ͳͲʣ  -BNCEBؔ਺ͭʹରͯ͠ϨΠϠʔͭ·Ͱ෇͚ΒΕΔ  ϨΠϠʔͷղౚॱҐ͸ࢦఆͰ͖ΔʢϑΝΠϧ্ॻ͖༻ʣ

     -BNCEBϥϯλΠϜ͕ݱࡏରԠ͍ͯ͠ͳ͍ݴޠͰ΋ɺ
 ΧελϜϥϯλΠϜͷϨΠϠʔ࡞੒ͰͲΜͳݴޠ͸ରԠՄೳ
  7. ཧ૝ͳ1)1ؔ਺ϋϯυϥʔ KBXTVH  ͳΔ΂͘ରԠϥϯλΠϜͷݴޠͱಉ༷ /PEFKTྫ 
 
 
 
 

    ؔ਺ʹΠϕϯτͱίϯςΩετΦϒδΣΫτ͕
 ύϥϝʔλͰ౉͞Εɺ໭Γ஋ͰϨεϙϯεΛฦ͢
  8. ཧ૝ͳ1)1ؔ਺ϋϯυϥʔ KBXTVH  ͜Μͳײ͡Ͱ͢ʂ

  9. ΧελϜϥϯλΠϜͷϑϩʔ KBXTVH  ॳظԽ " -BNCEBଆͷॳظԽ # ϥϯλΠϜͷॳظԽ  Πϕϯτॲཧʢແݶϧʔϓʣ

    " )551ͰΠϕϯτΛಡΈࠐΉ # ΠϕϯτΛॲཧ͢Δ $ )551ͰϨεϙϯεΛૹΔ ίʔϧυελʔτ ^ ϫʔϜελʔτ ^
  10. ΧελϜϥϯλΠϜͷϑϩʔ KBXTVH  ॳظԽ " -BNCEBଆͷॳظԽ # ϥϯλΠϜͷॳظԽ  Πϕϯτॲཧʢແݶϧʔϓʣ

    " )551ͰΠϕϯτΛಡΈࠐΉ # ΠϕϯτΛॲཧ͢Δ $ )551ͰϨεϙϯεΛૹΔ ίʔϧυελʔτ ^ ϫʔϜελʔτ ^
  11. -BNCEBଆͷॳظԽ KBXTVH  -BNCEBؔ਺ͱϨΠϠʔ͕શͯPQUʹղౚ͞ΕΔ  TUEPVUͱTUEFSSΛ$MPVE8BUDI-PHT΁ϦμΠϨΫτ  -BNCEBؔ਺ͱϥϯλΠϜ༻ͷ؀ڥม਺ΛॳظԽ  PQUCPPUTUSBQ͕࣮ߦ͞ΕΔʢγΣϧεΫϦϓτɺ


    όΠφϦ࣮ߦϑΝΠϧͰ΋Մೳʣ
  12. -BNCEBଆͷॳظԽ KBXTVH  -BNCEB͕༻ҙͨ͠؀ڥม਺ͷ಺༰ΛҰؾʹௐ΂ΕΔ  PQUCPPUTUSBQΛ͜ͷߦʹ͢Δ͚ͩ
 
 
#!/bin/sh env

  13. ϥϯλΠϜͷॳظԽ KBXTVH  PQUCPPUTUSBQͰ1)1όΠφϦΛ࣮ߦ͢Δ   ʂ

  14. 1)1Λιʔε͔ΒϏϧυ লུ KBXTVH  Ϗϧυ͢Δ04ͷϥΠϒϥϦ MJCD౳ ͕-BNCEB؀ڥͱ ౷Ұ͠ͳ͚Ε͹όΠφϦ࣮ߦϑΝΠϧ͸࣮ߦ͞Εͳ͍  IUUQTEPDTBXTBNB[PODPNMBNCEBMBUFTU

    EHMBNCEBSVOUJNFTIUNM
  15. 1)1Λιʔε͔ΒϏϧυ লུ KBXTVH  ".*࢖ͬͯ&$ΠϯελϯεΛ্ཱͪ͛Δ  "NB[PO-JOVY".*
 BN[OBNJIWNY@HQ  %PDLFSΛར༻ʢ͓͢͢Ίʣ

     '30.BNB[POMJOVY  QIQ NZTRMJ DVSM KTPO NCTUSJOH NIBTIΛDMBOH ͰαΠζ࠷దԽ 0[ ͔͚ͯ.#ͷͭͷ1)1όΠφϦ
  16. ϥϯλΠϜͷॳظԽʢଓ͖ʣ KBXTVH  PQUCPPUTUSBQͰ1)1όΠφϦΛ࣮ߦ͢Δʢ࣮ߦ ͢Δ1)1ϑΝΠϧ໊ΛҾ਺Ͱ౉͢ʣ #!/bin/sh /opt/php-runtime/bin/php /opt/php- runtime/bootstrap.php

  17. ϥϯλΠϜͷॳظԽʢଓ͖ʣ KBXTVH  -BNCEBଆͷ؀ڥม਺͔ΒඞཁͳύϥϝʔλΛར༻  @)"/%-&3-BNCEBؔ਺ϋϯυϥʔ໊ʢจࣈྻʣ  "84@-".#%"@36/5*.&@"1*-BNCEBͷΠϕϯτ΍Γ ͱΓ͢ΔͨΊͷ*1Wͱϙʔτ൪߸ )5514Ͱ͸ͳ͍ʂ

      -BNCEBΤϯυϙΠϯτͷ)551ϦΫΤετ63*
 IUUQ"84@-".#%"@36/5*.&@"1*
 ‎IUUQ
  18. ϥϯλΠϜͷॳظԽʢଓ͖ʣ KBXTVH  @)"/%-&3Ͱࢦఆ͞ΕͯΔιʔεϑΝΠϧಡΈࠐΜͰ ؔ਺ϋϯυϥʔ͕ଘࡏ͢Δ͔֬ೝ  ྫ@)"/%-&3lIFMMPXPSMENBJO)BOEMFSz  1)1Ͱ
 GVODFYQMPEF

    b` @&/7<b@)"/%-&3`> 
 JODMVEF@PODF l\GVOD<>^QIQz 
 JG GVODUJPO@FYJTUT l\GVOD<>^z FSSPS 
  19. ΧελϜϥϯλΠϜͷϑϩʔ KBXTVH  ॳظԽ " -BNCEBଆͷॳظԽ # ϥϯλΠϜͷॳظԽ  Πϕϯτॲཧʢແݶϧʔϓʣ

    " )551ͰΠϕϯτΛಡΈࠐΉ # ΠϕϯτΛॲཧ͢Δ $ )551ͰϨεϙϯεΛૹΔ ίʔϧυελʔτ ^ ϫʔϜελʔτ ^
  20. Πϕϯτॲཧʢແݶϧʔϓʣ KBXTVH

  21. )551ͰΠϕϯτΛಡΈࠐΉ KBXTVH  1)1ͷDVSM@FYFD ؔ਺౳Λར༻  DVSM@TFUPQU@BSSBZ ͰΦϓγϣϯࢦఆ͢Δ  $63-015@5*.&065


    ωοτϫʔΫλΠϜΞ΢τΛແݶʹ͢Δ ॏཁʂ   $63-015@5$1@/0%&-":USVF
 /BHMFΞϧΰϦζϜΛແޮʹ͢Δ EFMBZ͸ඞཁͳ͍
  22. )551ͰΠϕϯτΛಡΈࠐΉ KBXTVH  -BNCEB΁)551ϦΫΤετΛૹ৴
 (&5ΤϯυϙΠϯτSVOUJNFJOWPDBUJPOOFYU
 ‎IUUQSVOUJNFJOWPDBUJPOOFYU  ड৴ͨ͠Ϩεϙϯε͔Β)551ϔομʔ͸ز͔ͭ෇͍ͯ͘Δ  େࣄͳͷ͸͜ͷͭʢΠϕϯτ͝ͱʹมಈ͢Δʂʣ


    -BNCEB3VOUJNF"XT3FRVFTU*ECGG⒎GDDEBB BCBFDG  ͜ͷϦΫΤετ*%͸Πϕϯτॲཧޙ-BNCEBʹฦ৴ͨΊʹඞཁ  )551CPEZ͸-BNCEB͕ड৴ͨ͠Πϕϯτʢ+40/σʔλʣ
  23. ΠϕϯτΛॲཧ͢Δ KBXTVH  $POUFYUͱ-PHHFSΦϒδΣΫτΛ࡞੒ʢ೚ҙʣ  ྫ֎͕ରԠͯ͠Δݴޠ͸USZDBUDIΛ༻ҙ͢Δ  ؔ਺ϋϯυϥʔΛݺͼग़͢  1)1Ͱ͸DBMM@VTFS@GVOD

    ͸ؔ਺໊Λࢦఆ͢Δ
 จࣈྻͰؔ਺Λݺͼग़ͤΔ
  24. )551ͰϨεϙϯεΛૹΔ KBXTVH  Πϕϯτॲཧ͔Βͷ+40/σʔλ໭Γ஋Λ-BNCEBʹฦ͢  -BNCEB΁)551ϦΫΤετΛૹ৴
 1045ΤϯυϙΠϯτSVOUJNFJOWPDBUJPO3FR*%SFTQPOTF
 ‎IUUQSVOUJNFJOWPDBUJPO
 CGG⒎GDDEBBBCBFDGSFTQPOTF 

    )551CPEZ͸จࣈྻʹม׵ͨ͠+40/σʔλ  ׬ྃʂϧʔϓͰ࣍ʹड৴ͨ͠ΠϕϯτॲཧΛߦ͏
  25. ͔͠͠ KBXTVH ࣍ͷΠϕϯτ͕·ͩଘࡏ͠ͳ͍ͳΒͲ͏ͳΔ

  26. Πϕϯτ͕·ͩଘࡏ͠ͳ͍ͱͲ͏ͳΔ KBXTVH  ࣍ͷΠϕϯτΛड৴͢ΔͨΊ-BNCEB΁ͷ)551઀ଓ ͸੒ޭ͢Δ  ΧελϜϥϯλΠϜͷ(&5ϦΫΤετૹ৴͸੒ޭ͢Δ  $63-015@5*.&065͕ઃఆ͞ΕͯΔͷͰ
 -BNCEB͔Βฦ৴͕དྷΔ·Ͱແݶʹ଴ͭʂ

  27. Πϕϯτ͕·ͩଘࡏ͠ͳ͍ͱͲ͏ͳΔ KBXTVH  IUUQTHJUIVCDPNBXTMBCTBXTMBNCEBDQQCMPC NBTUFSTSDSVOUJNFDQQ͔Βͷίϝϯτ
 
  Πϕϯτ͕ଘࡏ͠ͳ͍৔߹͸ίϯςφ͕Ұ࣌ఀࢭ͞ΕΔ  ͨͩ͠ɺ͙͢ʹఀࢭ͞Εͳ͍ͷͰड৴λΠϜΞ΢τͯ͠


    ͠·͏ͱΤϥʔॲཧ͠ͳ͚Ε͹ͳΒͳ͍  ແݶλΠϜΞ΢τઃఆ͢Ε͹λΠϜΞ΢τͷճආ͸Ͱ͖Δ
  28. ΧελϜϥϯλΠϜΛϨΠϠʔʹ KBXTVH  CPPUTUSBQ͕ϧʔτʹ͋ΔΑ͏ʹ[JQΛ࡞੒
 
 
  [JQ͔ΒϨΠϠʔΛ࡞੒ͯ͠-BNCEB΁Ξοϓϩʔυ $ zip

    -v -9 -r -D ../php-7.3.9-runtime.zip * adding: bootstrap (in=66) (out=46) (deflated 30%) adding: php-runtime/bin/php (in=6105792) (out=2471517) (deflated 60%) adding: php-runtime/bootstrap.php (in=6380) (out=2451) (deflated 62%) adding: php-runtime/Context.inc.php (in=1209) (out=628) (deflated 48%) adding: php-runtime/Logger.inc.php (in=1853) (out=784) (deflated 58%) total bytes=6115300, compressed=2475426 -> 60% savings $ aws lambda publish-layer-version --layer-name php-7_3_9-runtime --zip-file fileb://php-7.3.9-runtime.zip
  29. 1)1Ͱ-BNCEBʂ KBXTVH  -BNCEBؔ਺Λ࡞੒ʢίϯιʔϧͷΤσΟλʔͰ΋ʣ  Ξοϓϩʔυͨ͠1)1ϨΠϠʔΛؔ਺ʹ௥Ճ  ࣮ߦʂ

  30. ެ։ͨ͠1)1ϥϯλΠϜΛੋඇʂ KBXTVH  ίʔϧυɾϑϡʔδϣϯ͸-BNCEBͰ1)1Λࠓޙͱ ΋׬શʹαϙʔτ͍ͨ͠ͷͰ৽نͷ1)1ϦϦʔε΋ ެ։͠ଓ͚·͢ʂ QIQBSOBXTMBNCEBBQOPSUIFBTUMBZFSQIQ@@SVOUJNF QIQBSOBXTMBNCEBBQOPSUIFBTUMBZFSQIQ@@SVOUJNF QIQBSOBXTMBNCEBBQOPSUIFBTUMBZFSQIQ@@SVOUJNF QIQBSOBXTMBNCEBBQOPSUIFBTUMBZFSQIQ@@SVOUJNF

  31. ͢΂ͯΦʔϓϯιʔε KBXTVH  "84-BNCEB1)13VOUJNF-BZFS
 IUUQTHJUMBCDPNDPMEGVTJPOKQBXTMBNCEBQIQSVOUJNF  "844%,GPS1)1-BNCEB-BZFS
 IUUQTHJUMBCDPNDPMEGVTJPOKQBXTTELQIQMBNCEBMBZFS  "NB[PO-JOVY

    DMBOHMMWN%PDLFSJNBHF
 IUUQTIVCEPDLFSDPNSDPMEGVTJPOKQBNB[POMJOVYDMBOH
 IUUQTHJUMBCDPNDPMEGVTJPOKQCVJMEDMBOHMMWN  .*5ϥΠηϯε(JU-BC$*ʹΑΔࣗಈϏϧυ ςετ
  32. ͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·͢ʂ
 
 KBXTVH