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

serverless-framework-workshop

 serverless-framework-workshop

堀家隆宏

March 05, 2017
Tweet

More Decks by 堀家隆宏

Other Decks in Technology

Transcript

  1. 4FSWFSMFTT'SBNFXPSLͷ جૅͱ։ൃख๏͔Β $*$%ύΠϓϥΠϯͷߏங·Ͱ

  2. ࣗݾ঺հ

  3. ࣗݾ঺հ  %JHJUBM$VCF$P-UEͷΤϯδχΞ  4FSWFSMFTT'SBNFXPSL։ൃ
 νʔϜϝϯόʔ  ओཁݴޠ /PEFKT 1)1

  4. 4FSWFSMFTT4UFQ'VODUJPOT IUUQTHJUIVCDPNIPSJLFTFSWFSMFTTTUFQGVODUJPOT

  5. 4IJGUFS IUUQTHFUTIJGUFSJP

  6. 2JJUB IUUQRJJUBDPNIPSJLF

  7. 4FSWFSMFTT'SBNFXPSLͷ֓ཁ

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

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

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

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

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

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

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

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

  16. Πϯετʔϧ OQNJOTUBMMHTFSWFSMFTT

  17. $SFEFOUJBMͷઃఆ TMTDPOpHDSFEFOUJBMTQSPWJEFS BXTLFZZPVSLFZŠTFDSFUZPVSTFDSFU

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

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

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

  21. ։ൃϑϩʔ

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

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

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

    "[VSF OPEFKT 0QFO8IJTL PQFOXIJTLOPEFKT
  25. TFSWFSMFTTZNM΁ͷΞʔΩςΫνϟఆٛ αʔϏεͷϓϩόΠμʔ΍ϥϯλΠϜ؀ڥͷఆٛ  ϑΝϯΫγϣϯͱͦΕΛൃՐ͢ΔͨΊͷΠϕϯτΛఆٛ  ࢖༻͢ΔΫϥ΢υϦιʔεͷఆٛ TFSWFS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F 4FSWFSMFTT

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  49. 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
  50. -BNCEBͰߦ͏Ϣχοτςετ class Hello { say() { return 'Your function executed

    successfully!'; } } module.exports = Hello; IFMMPDMBTTKT
  51. -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
  52. Ϣχοτςετʹ͓͚ΔBXTTELͷѻ͍ beforeEach(() => { awsStub = sinon.stub(aws.provider, 'request') .returns(BbPromise.resolve({})); });

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

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

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

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

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

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

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

    ςετ Ϣχοτ ςετ ΠϯςάϨʔ γϣϯςετ ΠϯςάϨʔ γϣϯςετ
  60. 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࣌ͷΈ
 ΠϯςάϨʔγϣϯςετ͕૸Δ ੒ޭ͢Ε͹σϓϩΠεΫϦϓτ͕૸Δ
  61. 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
  62. ͓ΘΓ