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

Serverless Frameworkの基礎と開発手法

Serverless Frameworkの基礎と開発手法

堀家隆宏

June 11, 2017
Tweet

More Decks by 堀家隆宏

Other Decks in Programming

Transcript

  1. 4FSWFSMFTT'SBNFXPSLͷ جૅͱ։ൃख๏

  2. ࣗݾ঺հ

  3. ࣗݾ঺հ  %JHJUBM$VCF$P-UEͷΤϯδχΞ  4FSWFSMFTT'SBNFXPSL$PSF$POUSJCVUPS  ओཁݴޠ /PEFKT 1)1

  4. 4FSWFSMFTT4UFQ'VODUJPOT IUUQTHJUIVCDPNIPSJLFTFSWFSMFTTTUFQGVODUJPOT

  5. 4IJGUFS IUUQTHFUTIJGUFSJP

  6. 2JJUB IUUQRJJUBDPNIPSJLF

  7. αʔόϨεΞʔΩςΫνϟͱ͸

  8. αʔόϨεΞʔΩςΫνϟͱ͸  'BB4 -BNCEB (PPHMF$MPVE'VODUJPOT "[VSF'VODUJPOTFUD ͱ
 ϚωʔδυαʔϏεΛ࢖ͬͯߏ੒͞ΕͨγεςϜ  Ϣʔεέʔεɿ


    XFC ϞόΠϧ *P5ͳͲͷ"1*ͷόοΫΤϯυ
 σʔλϓϩηογϯά  Πϕϯτۦಈܕ ϓϩηε͕ৗற͠ͳ͍   αʔό͕ແ͍ͱ͍͏͜ͱͰ͸ͳ͍ɻ
 αʔόͷ؅ཧΛΫϥ΢υϕϯμʔʹΞ΢τιʔε
  9. ͳʹ͕͏Ε͍͠ͷ͔ʁ

  10. ΠϕϯτυϦϒϯͰ͋Δ͜ͱ

  11. ίετ

  12. ϓϩηε͸ଘࡏ͠ͳ͍

  13. εςʔτϨε

  14. εέʔϥϏϦςΟ

  15. None
  16. Ͳ͏΍Δͷ͔ʁ

  17. None
  18. 4FSWFSMFTT'SBNFXPSLͷ֓ཁ

  19. 4FSWFSMFTT'SBNFXPSLͱ͸  αʔόʔϨεΞʔΩςΫνϟʹ͓͚Δ։ൃ΍σϓϩΠΛ
 αϙʔτͯ͘͠ΕΔίϚϯυϥΠϯπʔϧ  ZBNMʹΑΔΞʔΩςΫνϟͷߏ੒؅ཧ  ϚϧνΫϥ΢υϓϩόΠμʔαϙʔτ  044ͱͯ͠(JU)VC্Ͱ։ൃ

     ϓϥάΠϯʹΑΔ֦ு
  20. ࣮૷͞Ε͍ͯΔओͳίϚϯυ TMTDSFBUF ϓϩδΣΫτͷ࡞੒ TMTEFQMPZ ϩʔΧϧ؀ڥ͔ΒΫϥ΢υ΁ͷσϓϩΠ TMTJOWPLF ϑΝϯΫγϣϯͷ࣮ߦ TMTJOGP σϓϩΠ͞Εͨߏ੒ͷ৘ใΛදࣔ TMTMPHT

    ϑΝϯΫγϣϯͷ࣮ߦϩάͷ֬ೝ TMTSFNPWF σϓϩΠ͞Εͨߏ੒ͷ࡟আ
  21. ϓϩδΣΫτͷϑΝΠϧߏ੒؅ཧ /PEFKT ᵓᴷTFSWFSMFTTZNM ᵋᴷIBOEMFSKT ΞʔΩςΫνϟશମͷߏ੒Λ ఆٛ͢ΔϑΝΠϧ ϑΝϯΫγϣϯͰ࣮ߦ͞ΕΔ ϓϩάϥϜ

  22. :".-ϑΝΠϧʹΑΔߏ੒؅ཧ QSPWJEFS OBNFBXT SVOUJNFOPEFKT JBN3PMF4UBUFNFOUT &⒎FDU"MMPX "DUJPO EZOBNPEC  GVODUJPOT

    DSFBUF IBOEMFSUPEPTDSFBUFDSFBUF FWFOUT IUUQ QBUIUPEPT NFUIPEQPTU DPSTUSVF ϑΝϯΫγϣϯʹΞλον͢Δ ݖݶͷઃఆ ࢖༻͢ΔΫϥ΢υϓϩόΠμʔٴͼ ϥϯλΠϜ؀ڥͷࢦఆ ϑΝϯΫγϣϯͷ࣮ߦϑΝΠϧͷ ࢦఆ ϑΝϯΫγϣϯΛ࣮ߦͤ͞Δ Πϕϯτͷࢦఆ
  23. ϚϧνΫϥ΢υϓϩόΠμʔαϙʔτ QSPWJEFS OBNFBXT QSPWJEFS OBNFPQFOXIJTL QSPWJEFS OBNFB[VSF QSPWJEFS OBNFHPPHMF

  24. 044ͱͯ͠(JU)VC্Ͱ։ൃ

  25. ϓϥάΠϯʹΑΔػೳ֦ு QMVHJOT TFSWFSMFTTTUFQGVODUJPOT TFSWFSMFTTP⒐JOF IUUQTHJUIVCDPNTFSWFSMFTTQMVHJOT

  26. Πϯετʔϧ͔Βॳظઃఆ·Ͱ

  27. Πϯετʔϧ OQNJOTUBMMHTFSWFSMFTT

  28. $SFEFOUJBMͷઃఆ TMTDPOpHDSFEFOUJBMTQSPWJEFS BXTLFZZPVSLFZŠTFDSFUZPVSTFDSFU

  29. $SFEFOUJBMͷઃఆ "84ͷ৔߹ɺ"ENJOJTUSBUPS"DDFTTͷϙϦγʔΛ BUUBDI͢Δ͜ͱ͕ެࣜυΩϡϝϯτʹهࡌ͞Ε͍ͯΔ IUUQTTFSWFSMFTTDPNGSBNFXPSLEPDTQSPWJEFSTBXTHVJEFDSFEFOUJBMT

  30. $SFEFOUJBMͷઃఆ &⒎FDU"MMPX  "DUJPO< JBN(FU3PMF  JBN$SFBUF3PMF  JBN%FMFUF3PMF 

    JBN1VU3PMF1PMJDZ  JBN%FMFUF3PMF1PMJDZ  JBN1BTT3PMF  MPHT   T   MBNCEB   DMPVEGPSNBUJPO   BQJHBUFXBZ  >  3FTPVSDF<   > ΦϑΟγϟϧʹ͸ެ։͞Εͯͳ͍͕ɺ࠷௿ ݶ͜ͷ͋ͨΓͷϦιʔεʹ ڐՄ͕͋Ε͹ಈ࡞͢Δ
  31. $SFEFOUJBMͷઃఆ IUUQTHJUIVCDPNTFSWFSMFTTTFSWFSMFTTJTTVFT "ENJO$SFEFOUJBMΛ࢖Θͳ͍ηοτΞοϓํ๏͸ݱࡏٞ࿦த

  32. ։ൃϑϩʔ

  33. αʔϏεͷ࡞ ੒ TFSWFSMFTTZNM ΁ͷΞʔΩςΫνϟ ఆٛ σϓϩΠ Ϋϥ΢υ্Ͱ ͷςετ ։ൃͷϫʔΫϑϩʔ ϩʔΧϧͰͷ

    GVODUJPO։ൃ
  34. αʔϏεͷ࡞੒ TMTDSFBUFUFNQMBUFBXTOPEFKT ֤ϓϩόΠμʔͷϥϯλΠϜ؀ڥʹ߹Θͤͨ ελʔλʔςϯϓϨʔτΛࣗಈͰ࡞੒ͯ͘͠ΕΔ

  35. ରԠ͍ͯ͠ΔϥϯλΠϜ؀ڥ  "84 BXTOPEFKT BXTQZUIPO BXTKBWBHSBEMF BXTKBWBNBWFO BXTTDBMBTCU BXTDTIBSQ 

    "[VSF OPEFKT 0QFO8IJTL PQFOXIJTLOPEFKT
  36. TFSWFSMFTTZNM΁ͷΞʔΩςΫνϟఆٛ αʔϏεͷϓϩόΠμʔ΍ϥϯλΠϜ؀ڥͷఆٛ  ϑΝϯΫγϣϯͱͦΕΛൃՐ͢ΔͨΊͷΠϕϯτΛఆٛ  ࢖༻͢ΔΫϥ΢υϦιʔεͷఆٛ TFSWFSMFTTZNM͕αʔϏεશମͷઃܭਤͱͳΔ

  37. TFSWFSMFTTZNM΁ͷΠϕϯτఆٛ GVODUJPOT VTFS$SFBUF IBOEMFSMJCVTFS$SFBUFIBOEMFS FWFOUT IUUQ QBUIVTFST NFUIPEQPTU IUUQΠϕϯτͷఆٛ

  38. TFSWFSMFTTZNM΁ͷΠϕϯτఆٛ GVODUJPOT SFTJ[F IBOEMFSSFTJ[FIBOEMFS FWFOUT T CVDLFUQIPUPT FWFOUT0CKFDU$SFBUFE  SVMFT

    QSFpYVQMPBET TV⒏YKQH 4Πϕϯτͷఆٛ
  39. TFSWFSMFTTZNM΁ͷΠϕϯτఆٛ GVODUJPOT NZ4LJMM IBOEMFSNZ4LJMMIBOEMFS FWFOUT BMFYB4LJMM "MFYB4LJMMΠϕϯτͷఆٛ

  40. ରԠ͍ͯ͠ΔΠϕϯτ  "84 "1*(BUFXBZ ,JOFTJT%ZOBNP%# 4 4DIFEVMF 4/4 "MFYB4LJMM *P5

    $MPVE8BUDI&WFOU  "[VSF )551 5JNFS 2VFVF4UPSBHF 4FSWJDF#VT &WFOU)VCT #MPC4UPSBHF 0UIFS#JOEJOHT 0QFO8IJTL "1*(BUFXBZ 5SJHHFST 4DIFEVMF
  41. ϩʔΧϧͰͷ։ൃ TMTJOWPLFMPDBMŠGVODUJPOϑΝϯΫγϣϯ໊ ϩʔΧϧ؀ڥͰ-BNCEB౳ͷϑΝϯΫγϣϯΛ ٖࣅతʹ࣮ߦͯ͘͠ΕΔίϚϯυ

  42. ϩʔΧϧ࣮ߦͷରԠঢ়گ  "84 BXTOPEFKT BXTQZUIPO 0QFO8IJTL PQFOXIJTLOPEFKT

  43. ϩʔΧϧ։ൃΛαϙʔτ͢ΔϓϥάΠϯ΍πʔϧ 4FSWFSMFTT TJNVMBUJPO QMVHJO "1*(BUFXBZͱ-BNCEBΛ ϩʔΧϧͰγϡϛϨʔτ IUUQTHJUIVCDPN HFSUKWSTFSWFSMFTT QMVHJOTJNVMBUF 4FSWFSMFTT

    %ZOBNPEC -PDBM %ZOBNP%#ΛϩʔΧϧͰγ ϡϛϨʔτ IUUQTHJUIVCDPN YUTFSWFSMFTT EZOBNPECMPDBM 4FSWFSMFTT 0⒐JOF "1*(BUFXBZΛϩʔΧϧͰ γϡϛϨʔτ IUUQTHJUIVCDPN EIFSBVMUTFSWFSMFTT P⒐JOF ,JOFTBMJUF ,JOFTJTΛϩʔΧͰγϡϛϨ ʔτ IUUQTHJUIVCDPN NIBSULJOFTBMJUF
  44. Ϋϥ΢υ؀ڥ΁ͷσϓϩΠ TMTEFQMPZŠTUBHFεςʔδ໊WFSCPTF TFSWFSMFTTZNMͰఆٛͨ͠ΞʔΩςΫνϟ ٴͼϑΝϯΫγϣϯͷιʔεΛσϓϩΠ͢Δ

  45. εςʔδͷ࢓૊Έ EFWεςʔδ ϩʔΧϧ Q SPEVDUJPOεςʔ δ  ςετ؀ڥͱຊ൪؀ڥͱ͍ͬͨ࢖͍෼͚͕࣮ݱͰ͖Δ ֤εςʔδ͸ޓ͍ʹӨڹΛٴ΅͠߹Θͳ͍ ඞཁͳ͚ͩεςʔδ͸࡞੒Մೳ

  46. σϓϩΠͷॲཧͷྲྀΕ TMTEFQMPZίϚϯυͷൃߦ 4FSWFSMFTT͕TFSWFSMFTTZNM Λύʔε σϓϩΠύοέʔδ ΛTFSWFSMFTT഑Լʹग़ྗ ֤ϓϩόΠμʔͷํࣜʹैͬͯ σϓϩΠ "84ͳΒ$MPVE'PSNBUJPO

  47. σϓϩΠࣦഊ࣌ͷσόοά TMTEFQMPZQBDLBHF TFSWFSMFTT഑ԼʹσϓϩΠύοέʔδͷΈग़ྗ ͜ΕΛݩʹௐࠪՄೳ

  48. σϓϩΠޙͷ࣮ߦ TMTJOWPLFŠGVODUJPOϑΝϯΫγϣϯ໊ŠMPHT Ϋϥ΢υʹσϓϩΠࡁΈͷϑΝϯΫγϣϯͷ࣮ߦ

  49. ϩάͷ֬ೝ TMTMPHTŠGVODUJPOϑΝϯΫγϣϯ໊ ϑΝϯΫγϣϯͷ࣮ߦϩάͷ֬ೝ

  50. σϓϩΠޙमਖ਼ TMTEFQMPZGVODUJPOŠGVODUJPOϑΝϯΫγϣϯ໊ ϑΝϯΫγϣϯ୯ମͷσϓϩΠ͕Մೳ

  51. σϓϩΠޙͷϩʔϧόοΫ TMTEFQMPZMJTU TMTSPMMCBDLUJNFTUBNQUJNFTUBNQ TMTEFQMPZΛલճ࣮ࢪͨ͠ঢ়ଶ·ͰϩʔϧόοΫ ͯ͘͠ΕΔ

  52. $*$%ύΠϓϥΠϯࣗಈԽ

  53. ͳͥɺ$*$%  σϓϩΠͷଐਓԽɺϛεΛແ͘͢  13ۦಈͷ։ൃʹ͓͚Δ඼࣭ͷ୲อ ςετͷࣗಈԽʹΑΔޮ཰Խ

  54. $*$%ύΠϓϥΠϯߏ੒

  55. ܧଓతΠϯςάϨʔγϣϯ $*

  56. $*ύΠϓϥΠϯߏ੒  -JOUʹΑΔιʔείʔυͷ੩తղੳ  Ϣχοτςετ ΠϯςάϨʔγϣϯςετ

  57. -JOUʹΑΔιʔείʔυͷ੩తղੳ  ίʔυ࣮ߦલʹγϯλοΫεΤϥʔΛݕ஌Ͱ͖Δ  ίʔυͷελΠϧ͕౷Ұ͞ΕΔͨΊɺ
 ෳ਺ਓͰͷϝϯςφϯεੑ͕޲্͢Δ

  58. Ϣχοτςετ  ϓϩάϥϜΛߏ੒͢Δݸʑͷػೳ͕ਖ਼͘͠ಈ͘͜ͱΛݕূ͢Δςετ  ςετίʔυΛಡΉͱͦͷιϑτ΢ΣΞ͕Ͳ͏ಈ͘΂͖͔
 ͦͷ࢓༷͕Θ͔Δ  ͩΕ͕ϓϧϦΫΤετΛૹͬͯ΋ಉ͡ج४Ͱࣗಈςετ͕ߦ͑Δ ɹ඼࣭͕୲อ͞ΕΔ

  59. -BNCEBͰߦ͏Ϣχοτςετ ᵓᴷIBOEMFSKT ᵋᴷMJC ᵓᴷIFMMPDMBTTKT ᵋᴷIFMMPDMBTTUFTUKT -BNCEBͰ࣮ߦ͞ΕΔ ϝΠϯͷϑΝΠϧ ϏδωεϩδοΫ Ϣχοτςετͷίʔυ

  60. const Hello = require(‘./lib/hello.class.js'); const hello = new Hello(); module.exports.hello

    = (event, context, callback) => { const response = { statusCode: 200, body: JSON.stringify({ message: hello.say(), input: event, }), }; callback(null, response); }; -BNCEBͰߦ͏Ϣχοτςετ ςετ͕΍Γ΍͍͢Α͏ʹ-BNCEBΛ࣮ߦ ͢ΔϝΠϯͷϑΝΠϧ͔ΒΫϥεͰ੾Γग़ ͯ͋͛͠Δ IBOEMFSKT
  61. -BNCEBͰߦ͏Ϣχοτςετ class Hello { say() { return 'Your function executed

    successfully!'; } } module.exports = Hello; IFMMPDMBTTKT
  62. -BNCEBͰߦ͏Ϣχοτςετ const expect = require('chai').expect; const Hello = require('./hello.class.js'); describe('Hello',

    () => { let hello; beforeEach(() => { hello = new Hello(); }); describe('say', () => { it('should return the value', () => expect(hello.say()) .to.be.equal('Your function executed successfully!')); }); }); ΫϥεͰ੾Γग़ͨ͠ϏδωεϩδοΫ ʹରͯ͠ςετίʔυΛఆٛ IFMMPDMBTTUFTUKT
  63. Ϣχοτςετʹ͓͚ΔBXTTELͷѻ͍ beforeEach(() => { awsStub = sinon.stub(aws.provider, 'request') .returns(BbPromise.resolve({})); });

     ςετ಺ͰΫϥ΢υ؀ڥʹ͸ΞΫηε͠ͳ͍  ελϒԽɻBXTTEL͸ϢχοτςετͷൣғͰ͸ͳ͍ IUUQTHJTUHJUIVCDPNIPSJLFBCECGFDGDBCFG
  64. ΠϯςάϨʔγϣϯςετ  TFSWFSMFTTZNMͰఆٛͨ͠ߏ੒ٴͼϑΝϯΫγϣϯΛ
 ࣮ࡍͷΫϥ΢υ؀ڥ΁σϓϩΠͯࣗ͠ಈͰ݁߹ςετΛߦ͏

  65. "1*(BUFXBZΠϕϯτͷςετͷϑϩʔ "1*(BUFXBZΠϕϯ τͷσϓϩΠ BXTTELͷ EFTDSJCF4UBDLTʹΑΓ"1* ΤϯυϙΠϯτΛऔಘ "1*ͷฦΓ஋ ΛνΣοΫ "1*ʹ IUUQϦΫΤετ

    IUUQTHJUIVCDPNIPSJLFTFSWFSMFTTBQJJOUFHSBUJPOUFTUTBNQMFCMPCNBTUFSJOUFHSBUJPOUFTUUFTUKT--
  66. 4Πϕϯτͷςετͷϑϩʔ 4Πϕϯτͷ σϓϩΠ σϓϩΠࡁΈͷόέοτ ΁ͷΦϒδΣΫτૢ࡞ $MPVE8BUDI-PHT ͰΠϕϯτ͕ൃՐ͔ͨ͠ νΣοΫ IUUQTHJUIVCDPNIPSJLFTFSWFSMFTTBQJJOUFHSBUJPOUFTUTBNQMFCMPCNBTUFSJOUFHSBUJPOUFTUUFTUKT--

  67. ܧଓతσϦόϦʔ $%

  68. $%ύΠϓϥΠϯߏ੒  EFWFMPQNFOUϒϥϯνQVTI࣌ͷςετͷ࣮ߦ
 ੒ޭ࣌ʹςετεςʔδʹσϓϩΠ  NBTUFSϒϥϯνΛ࠷৽ιʔεͷू໿৔ॴͱͯ͠ɺ
 QVTI࣌ʹςετͷΈ࣮ߦ  UBHΛ੾ͬͯQVTI͢ΔͱϦϦʔε ςετ࣮ߦͯ͠੒ޭ͢Ε͹ຊ൪εςʔδʹσϓϩΠ

  69. $%ύΠϓϥΠϯߏ੒ (JU %FQMPZNFOU CSBODINBTUFS CSBODIEFWFMPQNFOU UBHYYY EFWFMPQNFOUTUBHF QSPETUBHF

  70. $%ύΠϓϥΠϯߏ੒ CSBODINBTUFS CSBODIEFWFMPQNFOU UBHYYY -JOU -JOU -JOU Ϣχοτς ετ Ϣχοτς

    ετ Ϣχοτς ετ ΠϯςάϨʔ γϣϯςετ ΠϯςάϨʔ γϣϯςετ
  71. language: node_js node_js: - '4.3' env: global: - AWS_REGION=us-east-1 -

    SLS_DEBUG=true - secure: lSxO7tZ0….BEVm - secure: LIh0lkl….7fM= before_install: - chmod +x ./bin/deploy.sh - npm i -g serverless install: - travis_retry npm install script: - npm run lint - npm run test - if [[ $TRAVIS_BRANCH == "master" ]] || [[ $TRAVIS_TAG ]]; then npm run integration-test; fi after_success: - ./bin/deploy.sh USBWJTZNMͷઃఆ "84ͷDSFEFOUJBMΛ҉߸Խͯ͠ઃఆ 5SBWJT͔Β"84؀ڥ΁઀ଓΛՄೳͱ͍ͯ͠Δ NBTUFSϒϥϯνͱUBH΁ͷQVTI࣌ͷΈ
 ΠϯςάϨʔγϣϯςετ͕૸Δ ੒ޭ͢Ε͹σϓϩΠεΫϦϓτ͕૸Δ
  72. EFQMPZTI QVTIઌʹԠͯ͡TUBHFΛઃఆ σϓϩΠޠ͸ෆཁͳOQNύοέʔδͷ૟আ σϓϩΠ #!/bin/bash set -e if [[ $TRAVIS_TAG

    ]]; then STAGE="production" elif [[ $TRAVIS_BRANCH == 'dev' ]]; then STAGE="development" fi echo "Deploying from branch $BRANCH to stage $STAGE" npm prune --production #remove devDependencies sls deploy --stage $STAGE --region $AWS_REGION
  73. ͓ΘΓ