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

An use case and development tips of cloud funct...

Soichi Sumi
January 17, 2019

An use case and development tips of cloud functions at Ginco inc

GincoにおけるCloud Functionsの
利用事例と開発tipsの紹介

Firebase Meetup #9 Cloud Functions Day
presentation slide
https://firebase-community.connpass.com/event/110958/

Soichi Sumi

January 17, 2019
Tweet

More Decks by Soichi Sumi

Other Decks in Programming

Transcript

  1. A B O U T M Y S E L

    F ࣗݾ঺հ Soichi Sumi @soichisumi ɾ#BDLFOE&OHJOFFS!(JODP ɾ࢓ࣄ಺༰ ɹɾόοΫΤϯυͷ։ൃ 'JSFCBTF(P (,&  ɹɾϒϩοΫνΣʔϯΠϯϑϥͷӡ༻ ɹɾࣾ಺'JSFCBTF͓͡͞Μ
  2. ։ൃνʔϜ • ΫϥΠΞϯτ • ୲౰ΞϓϦɺ$MPVE'VODUJPOT • J04ਓ ۀ຿ҕୗਓ • "OESPJEਓ

    ۀ຿ҕୗਓ • 8FCਓ • όοΫΤϯυ • ୲౰औҾཤྺγεςϜɺ΢ΥϨοτϥΠϒϥϦɺ ϒϩοΫνΣʔϯϊʔυӡ༻ɺ$MPVE'VODUJPOT • $50 ਓ
  3. $MPVE'VODUJPOTͱ͸ • ϑϧϚωʔδυͳؔ਺ͷ࣮ߦ؀ڥ • ؔ਺ͱɺτϦΨʔͱͳΔΠϕϯτΛࢦఆͯ͠"1*Λ࣮૷ ར఺ • τϦΨʔʹର͢ΔॲཧΛॻ͚ͩ͘ɻ؆୯ • ݸʑͷؔ਺͸׬શʹҟͳΔ؀ڥͰ࣮ߦ͞ΕΔ

    • ͋Δؔ਺΁ͷมߋ͕ଞͷؔ਺΁ӨڹΛ༩͑ͳ͍ • ؔ਺ͷ਺͕૿͑ͯ΋ػೳ௥Ճɾमਖ਼͕༰қ • ϦΫΤετʹԠͯ͡Φʔτεέʔϧ • τϦΨʔͷछྨ͕๛෋
  4. Functions Cloud ΞʔΩςΫνϟ Messaging Cloud Firestore Cloud Storage Cloud BigQuery

    Engine Kubernetes ֎෦ API Node Blockchain Node Blockchain Blockchain Nodes …
  5. τϦΨʔͷछྨ • )551ؔ਺ • 0O3FRVFTU • 0O$BMM • όοΫάϥ΢ϯυؔ਺ •

    'JSFTUPSF • "OBMZUJDT • 1VC4VC • 4UPSBHF • $SBTIMZUJDT • 3FNPUF$POpH • "VUIFOUJDBUJPO
  6. ࣮૷ྫ0O$BMMτϦΨʔ JOEFYKT ؔ਺Λݺͼग़͢ TXJGU τϦΨʔΛPO$BMMʹࢦఆ let functions = Functions.functions() functions.httpsCallable(“Func1”).call([“data":

    “hoge”]) {(result, error) in } ؔ਺໊ͱύϥϝʔλΛࢦఆ const functions = require('firebase-functions') exports.Func1 = functions.https.onCall((data, ctx) => { // ...... })
  7. Ծ૝௨՟ͷૹۚ • ࢿ࢈͸ΞυϨεʹඥͮ͘ • ྫY'%&G###%D"BG"B⒎'" • ΞυϨεʹ͸ରԠ͢Δൿີ伴͕ଘࡏ͢Δ • ൿີ伴ʹΑΔॺ໊͕͋Ε͹ૹۚͰ͖Δ •

    ԼهखॱͰૹۚ • Ͳ͜΁͍͘ΒૹΔ͔Λࣔ͢τϥϯβΫγϣϯΛ࡞੒ • ൿີ伴Ͱॺ໊ • ॺ໊ࡁΈτϥϯβΫγϣϯΛωοτϫʔΫ΁ૹΔ
  8. Ծ૝௨՟ͷ྆ସ • Ծ૝௨՟ಉ࢜ͷ྆ସΛߦ͏αʔϏεΛར༻ • ྫDIBOHFMMZ LZCFSOFUXPSL • ࢦఆͷૹۚઌ΁௨՟ΛૹΔͱॴ๬ͷ௨՟ʹฦͬͯ͘Δ࢓૊Έ • ԼهखॱͰ྆ସ

    • ௨՟ϖΞͷϨʔτΛऔಘ ྫ#5$ˠ&5) &5)#5$  • ௨՟ϖΞͱฦۚઌΞυϨεΛૹΓɺૹۚઌΞυϨεΛൃߦ • ૹۚઌΞυϨε΁ૹۚ͢Δ • ฦۚઌΞυϨε΁ॴ๬ͷ௨՟͕ૹΒΕͯ͘Δ
  9. "OBMZUJDTτϦΨʔ • ࢦఆ͞ΕͨΠϕϯτ͕"OBMZUJDTʹૹΒΕΔͱؔ਺͕ىಈ • ྫBQQ@PQFO JO@BQQ@QVSDIBTFΠϕϯτͳͲ • ΫϥΠΞϯτ͕Πϕϯτʹ௥Ճͨ͠ύϥϝʔλ΍Ϣʔβ৘ ใΛ࢖ͬͨॲཧΛߦ͑Δ •

    ྫΞϓϦ಺ΞΠςϜΛߪೖͨ͠ϢʔβʹΫʔϙϯΛ෇༩ • ίϯόʔδϣϯΠϕϯτͷ"QQT'MZFS΁ͷૹ৴Λαʔό αΠυͰ·ͱΊͯߦ͏ͨΊʹར༻
  10. ίετ ࢖༻ྔ ྉۚ 'VODUJPOT ສDBMM = 'JSFTUPSF ԯ3FBE ສ8SJUF =ສ

    4UPSBHF  -PHHJOH FUD  =ສ 5PUBM  =ສ ˞݄ؒίετ'JSFCBTFͷΈ • ͔ͳΓ͍҆ɻಛʹ'VODUJPOTͱ'JSFTUPSF • ΋͠7.ͱ%#Λ਺୆ཱͯͯϚϧνϦʔδϣϯʹల։͍ͯͨ͠ Β਺ഒ͔͔Δ
  11. $PME4UBSUରࡦΛ͠Α͏ • ؔ਺͸͠͹Β͘࢖ΘΕ͍ͯͳ͍ͱεϦʔϓ͢Δ • εϦʔϓޙɺ࠷ॳͷϦΫΤετͷॲཧʹ͸͕͔͔࣌ؒΔ • ͜ΕΛ$PME4UBSUͱݺͿ • ԼهͭͷରࡦΛߦ͏ͱྑ͍ •

    άϩʔόϧείʔϓͰϞδϡʔϧΛϩʔυ͠ͳ͍ • )551ؔ਺Ͱ'JSFTUPSFΛͳΔ΂͘࢖Θͳ͍ • શͯͷؔ਺Λఆظతʹ࣮ߦ͢Δ ৄ͘͠͸IUUQTCJUMZ.VN9
  12. JOEFYKT const functions = require('firebase-functions'); const admin = require('firebase-admin'); admin.initializeApp(functions.config().firebase);

    const db = admin.firestore(); module.exports = functions.https .onRequest((req, res)=>{ // ...... }); GVODTGVODKT const functions = require('firebase-functions') const google = require('googleapis'); const rp = require('request-promise'); const projectId = process.env.GCLOUD_PROJECT; module.exports = functions.storage
 .bucket(`${projectId}-bucket`) .object() .onFinalize((obj)=>{ // ...... }); Ͳͷؔ਺Λ࣮ߦ͢Δ৔߹Ͱ΋ ͢΂ͯͷؔ਺͕ϩʔυ͞ΕΔ ࣮૷ྫमਖ਼લ exports.Func1 = require('./funcs/func1'); exports.Func2 = require('./funcs/func2'); exports.Func3 = require(‘./funcs/func3'); ... GVODTGVODKT
  13. JOEFYKT ؔ਺࣮ߦ࣌ʹ͸'6/$5*0/@/".&ʹؔ਺໊͕ೖΔ ͦΕҎ֎ͷ৔߹͸ۭ σϓϩΠ࣌ʹ͸͢΂ͯͷؔ਺͕ϩʔυ͞Εɺ ࣮ߦ࣌ʹ͸࣮ߦର৅ͷؔ਺ͷΈ͕ϩʔυ͞ΕΔʂ const funcName = process.env.FUNCTION_NAME; if

    (!funcName || funcName === 'Func1') { exports.Func1 = require('./funcs/func1'); } if (!funcName || funcName === 'Func2') { exports.Func2 = require('./funcs/func2'); } ... ࣮૷ྫमਖ਼ޙ
  14. UFTUJOEFYUFTUKT Ͳ ͷ ؔ ਺ Λ ࣮ ߦ ͢ Δ

    ৔ ߹ Ͱ ΋ ͢ ΂ ͯ ͷ ؔ ਺ ͕ ϩ ʔ υ ͞ Ε Δ ࣮૷ྫpSFCBTFGVODUJPOTUFTUͷॳظԽ const test = require('firebase-functions-test')({ databaseURL: 'https://my-project.firebaseio.com', storageBucket: 'my-project.appspot.com', projectId: 'my-project', }, ‘path/to/serviceAccountKey.json’); const mockData = { key: "mocked string" }; // function.config().key -> "mocked string" test.mockConfig(mockData);
  15. JOEFYKT UFTUJOEFYUFTUKT Ͳ ͷ ؔ ਺ Λ ࣮ ߦ ͢

    Δ ৔ ߹ Ͱ ΋ ͢ ΂ ͯ ͷ ؔ ਺ ͕ ϩ ʔ υ ͞ Ε Δ ࣮૷ྫ0O$BMMؔ਺ͷςετ describe("Callable function test sample", () => { const wrapped = test.wrap(myFuncs.CallableFunc); it("Success", () => { const data = { data: "testdata" }; const ctx = { auth: { uid: 'user1' } } const res = wrapped(data, ctx); assert.deepEqual(res, {data: JSON.stringify(data)}); }); }); exports.CallableFunc = functions.https.onCall((data, ctx) => { console.log(`req: ${JSON.stringify(data)}`) return {data: JSON.stringify(data)} });
  16. ·ͱΊ • Ծ૝௨՟΢ΥϨοτͰͲͷΑ͏ʹ$MPVE'VODUJPOT Λ׆༻͍ͯ͠Δ͔঺հͨ͠ • ΄΅͢΂ͯͷτϦΨʔΛ׆༻ͯ͠։ൃ͍ͯ͠Δ • ։ൃɺӡ༻͢ΔதͰಘΒΕͨUJQTΛ঺հͨ͠  $PME4UBSUରࡦ͠Α͏

     ςετʹ͸pSFCBTFGVODUJPOTUFTUΛ࢖͓͏  SFUSZ͸σϑΥϧτͰߦΘΕͳ͍  ಉҰ໊ͷؔ਺Λෳ਺ϦʔδϣϯʹσϓϩΠͰ͖ͳ͍ ࠓͷॴ