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ͷ
    جૅͱ։ൃख๏͔Β
    $*$%ύΠϓϥΠϯͷߏங·Ͱ

    View Slide

  2. ࣗݾ঺հ

    View Slide

  3. ࣗݾ঺հ
    %JHJUBM$VCF$P-UEͷΤϯδχΞ
    4FSWFSMFTT'SBNFXPSL։ൃ

    νʔϜϝϯόʔ
    ओཁݴޠ
    /PEFKT
    1)1

    View Slide

  4. 4FSWFSMFTT4UFQ'VODUJPOT
    IUUQTHJUIVCDPNIPSJLFTFSWFSMFTTTUFQGVODUJPOT

    View Slide

  5. 4IJGUFS
    IUUQTHFUTIJGUFSJP

    View Slide

  6. 2JJUB
    IUUQRJJUBDPNIPSJLF

    View Slide

  7. 4FSWFSMFTT'SBNFXPSLͷ֓ཁ

    View Slide

  8. 4FSWFSMFTT'SBNFXPSLͱ͸
    αʔόʔϨεΞʔΩςΫνϟʹ͓͚Δ։ൃ΍σϓϩΠΛ

    αϙʔτͯ͘͠ΕΔίϚϯυϥΠϯπʔϧ
    ZBNMʹΑΔΞʔΩςΫνϟͷߏ੒؅ཧ
    ϚϧνΫϥ΢υϓϩόΠμʔαϙʔτ
    044ͱͯ͠(JU)VC্Ͱ։ൃ
    ϓϥάΠϯʹΑΔ֦ு

    View Slide

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

    View Slide

  10. ϓϩδΣΫτͷϑΝΠϧߏ੒؅ཧ
    /PEFKT

    ᵓᴷTFSWFSMFTTZNM
    ᵋᴷIBOEMFSKT
    ΞʔΩςΫνϟશମͷߏ੒Λ
    ఆٛ͢ΔϑΝΠϧ
    ϑΝϯΫγϣϯͰ࣮ߦ͞ΕΔ
    ϓϩάϥϜ

    View Slide

  11. :".-ϑΝΠϧʹΑΔߏ੒؅ཧ
    QSPWJEFS
    OBNFBXT
    SVOUJNFOPEFKT
    JBN3PMF4UBUFNFOUT
    &⒎FDU"MMPX
    "DUJPO
    EZOBNPEC
    GVODUJPOT
    DSFBUF
    IBOEMFSUPEPTDSFBUFDSFBUF
    FWFOUT
    IUUQ
    QBUIUPEPT
    NFUIPEQPTU
    DPSTUSVF
    ϑΝϯΫγϣϯʹΞλον͢Δ
    ݖݶͷઃఆ
    ࢖༻͢ΔΫϥ΢υϓϩόΠμʔٴͼ
    ϥϯλΠϜ؀ڥͷࢦఆ
    ϑΝϯΫγϣϯͷ࣮ߦϑΝΠϧͷ
    ࢦఆ
    ϑΝϯΫγϣϯΛ࣮ߦͤ͞Δ
    Πϕϯτͷࢦఆ

    View Slide

  12. ϚϧνΫϥ΢υϓϩόΠμʔαϙʔτ
    ($1
    ։ൃதɻۙ೔ϦϦʔε༧ఆ
    QSPWJEFS
    OBNFBXT
    QSPWJEFS
    OBNFPQFOXIJTL
    QSPWJEFS
    OBNFB[VSF

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  16. Πϯετʔϧ
    OQNJOTUBMMHTFSWFSMFTT

    View Slide

  17. $SFEFOUJBMͷઃఆ
    TMTDPOpHDSFEFOUJBMTQSPWJEFS
    BXTLFZZPVSLFZŠTFDSFUZPVSTFDSFU

    View Slide

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

    View Slide

  19. $SFEFOUJBMͷઃఆ
    &⒎FDU"MMPX
    "DUJPO
    JBN(FU3PMF
    JBN$SFBUF3PMF
    JBN%FMFUF3PMF
    JBN1VU3PMF1PMJDZ
    JBN%FMFUF3PMF1PMJDZ
    JBN1BTT3PMF
    MPHT
    T
    MBNCEB
    DMPVEGPSNBUJPO
    BQJHBUFXBZ
    >
    3FTPVSDF

    >
    ΦϑΟγϟϧʹ͸ެ։͞Εͯͳ͍͕ɺ
    ࠷௿ݶ͜ͷ͋ͨΓͷϦιʔεʹ
    ڐՄ͕͋Ε͹ಈ࡞͢Δ

    View Slide

  20. $SFEFOUJBMͷઃఆ
    IUUQTHJUIVCDPNTFSWFSMFTTTFSWFSMFTTJTTVFT
    "ENJO$SFEFOUJBMΛ࢖Θͳ͍ηοτΞοϓํ๏͸ݱࡏٞ࿦த

    View Slide

  21. ։ൃϑϩʔ

    View Slide

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

    View Slide

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

    View Slide

  24. ରԠ͍ͯ͠ΔϥϯλΠϜ؀ڥ
    "84
    BXTOPEFKT
    BXTQZUIPO
    BXTKBWBHSBEMF
    BXTKBWBNBWFO
    BXTTDBMBTCU
    BXTDTIBSQ
    "[VSF
    OPEFKT
    0QFO8IJTL
    PQFOXIJTLOPEFKT

    View Slide

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

    View Slide

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

    View Slide

  27. TFSWFSMFTTZNM΁ͷΠϕϯτఆٛ
    GVODUJPOT
    SFTJ[F
    IBOEMFSSFTJ[FIBOEMFS
    FWFOUT
    T
    CVDLFUQIPUPT
    FWFOUT0CKFDU$SFBUFE
    SVMFT
    QSFpYVQMPBET
    TV⒏YKQH
    4Πϕϯτͷఆٛ

    View Slide

  28. TFSWFSMFTTZNM΁ͷΠϕϯτఆٛ
    GVODUJPOT
    NZ4LJMM
    IBOEMFSNZ4LJMMIBOEMFS
    FWFOUT
    BMFYB4LJMM
    "MFYB4LJMMΠϕϯτͷఆٛ

    View Slide

  29. ରԠ͍ͯ͠ΔΠϕϯτ
    "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

    View Slide

  30. ϩʔΧϧͰͷ։ൃ
    TMTJOWPLFMPDBMŠGVODUJPOϑΝϯΫγϣϯ໊
    ϩʔΧϧ؀ڥͰ-BNCEB౳ͷϑΝϯΫγϣϯΛ
    ٖࣅతʹ࣮ߦͯ͘͠ΕΔίϚϯυ

    View Slide

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

    View Slide

  32. ϩʔΧϧ։ൃΛαϙʔτ͢ΔϓϥάΠϯ΍πʔϧ
    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

    View Slide

  33. Ϋϥ΢υ؀ڥ΁ͷσϓϩΠ
    TMTEFQMPZŠTUBHFεςʔδ໊WFSCPTF
    TFSWFSMFTTZNMͰఆٛͨ͠ΞʔΩςΫνϟ
    ٴͼϑΝϯΫγϣϯͷιʔεΛσϓϩΠ͢Δ

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    ෳ਺ਓͰͷϝϯςφϯεੑ͕޲্͢Δ

    View Slide

  47. Ϣχοτςετ
    ϓϩάϥϜΛߏ੒͢Δݸʑͷػೳ͕ਖ਼͘͠ಈ͘͜ͱΛݕূ͢Δςετ
    ςετίʔυΛಡΉͱͦͷιϑτ΢ΣΞ͕Ͳ͏ಈ͘΂͖͔

    ͦͷ࢓༷͕Θ͔Δ
    ͩΕ͕ϓϧϦΫΤετΛૹͬͯ΋ಉ͡ج४Ͱࣗಈςετ͕ߦ͑Δ
    ɹ඼࣭͕୲อ͞ΕΔ

    View Slide

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

    View Slide

  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

    View Slide

  50. -BNCEBͰߦ͏Ϣχοτςετ
    class Hello {
    say() {
    return 'Your function executed successfully!';
    }
    }
    module.exports = Hello;
    IFMMPDMBTTKT

    View Slide

  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

    View Slide

  52. Ϣχοτςετʹ͓͚ΔBXTTELͷѻ͍
    beforeEach(() => {
    awsStub = sinon.stub(aws.provider, 'request')
    .returns(BbPromise.resolve({}));
    });
    ςετ಺ͰΫϥ΢υ؀ڥʹ͸ΞΫηε͠ͳ͍
    ελϒԽɻBXTTEL͸ϢχοτςετͷൣғͰ͸ͳ͍
    IUUQTHJTUHJUIVCDPNIPSJLFBCECGFDGDBCFG

    View Slide

  53. ΠϯςάϨʔγϣϯςετ
    TFSWFSMFTTZNMͰఆٛͨ͠ߏ੒ٴͼϑΝϯΫγϣϯΛ

    ࣮ࡍͷΫϥ΢υ؀ڥ΁σϓϩΠͯࣗ͠ಈͰ݁߹ςετΛߦ͏

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  57. $%ύΠϓϥΠϯߏ੒
    EFWFMPQNFOUϒϥϯνQVTI࣌ͷςετͷ࣮ߦ

    ੒ޭ࣌ʹςετεςʔδʹσϓϩΠ
    NBTUFSϒϥϯνΛ࠷৽ιʔεͷू໿৔ॴͱͯ͠ɺ

    QVTI࣌ʹςετͷΈ࣮ߦ
    UBHΛ੾ͬͯQVTI͢ΔͱϦϦʔε
    ςετ࣮ߦͯ͠੒ޭ͢Ε͹ຊ൪εςʔδʹσϓϩΠ

    View Slide

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

    View Slide

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

    View Slide

  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࣌ͷΈ

    ΠϯςάϨʔγϣϯςετ͕૸Δ
    ੒ޭ͢Ε͹σϓϩΠεΫϦϓτ͕૸Δ

    View Slide

  61. EFQMPZTI
    QVTIઌʹԠͯ͡TUBHFΛઃఆ
    σϓϩΠޠ͸ෆཁͳOQNύοέʔδͷ૟আ
    σϓϩΠ
    #!/bin/bash
    set -e
    if [[ $TRAVIS_TAG ]]; then
    STAGE="production"
    elif [[ $TRAVIS_BRANCH == 'dev' ]]; then
    STAGE="development"

    echo "Deploying from branch $BRANCH to stage $STAGE"
    npm prune --production #remove devDependencies
    sls deploy --stage $STAGE --region $AWS_REGION

    View Slide

  62. ͓ΘΓ

    View Slide