the internal of serverless plugins

26
cz! Nbtbtij!Ufsvj!bu!Tfswfsmftt!Nffuvq!Uplzp!$2 Uif!Joufsobm!pg!Tfswfsmftt!Qmvhjot Tfswfsmftt!Gsbnfxpsl!Qmvhjo ؊∽ئ

Upload: terui-masashi

Post on 08-Jan-2017

1.090 views

Category:

Technology


4 download

TRANSCRIPT

Page 1: The Internal of Serverless Plugins
Page 2: The Internal of Serverless Plugins

Masashi Terui @ marcy_teruiI’m a Developer and Cloud Architect.

I’m a Remote-Multi-Worker at Section-9 / Serverworks Co., Ltd. / Freelance I’m an author of the serverless deployment tool “Lamvery” and Serverless Plugins.

I’m around 30 years old. I’m a father of my son and my daughter. https://willy.works/

2

Page 3: The Internal of Serverless Plugins

3

Page 4: The Internal of Serverless Plugins

Agenda

4

Serverless Pluginとは

Pluginの作り方・基本編

Pluginの作り方・実例からみる応用編

Pluginの作り方・Tips

おまけ・まとめ

Page 5: The Internal of Serverless Plugins

5

Page 6: The Internal of Serverless Plugins

Serverless Plugin

6

Serverless Frameworkを拡張する、または挙動を置き換えるプラグイン

本体と同じNode.js(v4以上)で記述されたnpm module

大枠として以下の2つの役割を持つ

Commandの拡張・置き換え

各CommandのLife Cycle EventsにHookして処理を行う

Page 7: The Internal of Serverless Plugins

7

Page 8: The Internal of Serverless Plugins

基本形

8

class PluginName { constructor(serverless, options) { this.serverless = serverless; this.options = options || {}; this.provider = this.serverless.getProvider('aws');

this.commands = {snip};

this.hooks = {snip}; } }

module.exports = PluginName;

対応するプロバイダを取得

本体とやりとりするやつ

Commandの定義

Life Cycle hookの定義

おまじない

Command-line Options

Page 9: The Internal of Serverless Plugins

Commands

9

this.commands = { command: { usage: 'Usage of the command', lifecycleEvents: [ 'initialize', 'execute', ], options: { option: { usage: 'Usage of the option', shortcut: 'o', required: true, }, }, commands: {}, }, };

Command名

Life Cycle定義

Command Options

Sub Commands※定義の書き方は一緒

Page 10: The Internal of Serverless Plugins

Life Cycle Hooks

10

this.hooks = { 'before:command:lifeCycle': () => BbPromise.bind(this) .then(this.validate) .then(this.beforeJob), ‘command:subCommand:lifeCycle': () => BbPromise.bind(this) .then(this.validate) .then(this.job), 'after:command:lifeCycle': () => BbPromise.bind(this) .then(this.validate) .then(this.afterJob), };

Life Cycle Eventの前にHook

Life Cycle EventにHook

Life Cycle Eventの後にHook

緑の下線部のメソッドに実際の処理を記述していく

Page 11: The Internal of Serverless Plugins

11

Page 12: The Internal of Serverless Plugins

12

Page 13: The Internal of Serverless Plugins

Rollback

13

‘before:deploy:function’ にHook デプロイの直前にバックアップを取らせる Lambda:PublishVersion -> Lambda:CreateAlias(UpdateAlias)

‘rollback function’ コマンドを追加 バックアップを取得してそれをアップロードする Lambda:getFunction(Identifier=backupAlias)

https://github.com/marcy-terui/serverless-rollback-function

Page 14: The Internal of Serverless Plugins

14

Page 15: The Internal of Serverless Plugins

Crypt

15

‘encrypt’,‘decrypt’ コマンドの追加 KMSで暗号化された情報をJSONに吐き出して保存させる

‘before:deploy:function’,’before:deploy:createDeploymentArtifacts’ にHook 暗号化されたJSONからKMSで復号化するためのmoduleを一時的に配置 moduleがデプロイパッケージに含まれる

‘after:deploy:function’,’after:deploy:deploy’ にHook 一時的に配置したmoduleを削除して元の状態に戻す

https://github.com/marcy-terui/serverless-crypt

Page 16: The Internal of Serverless Plugins

16

Page 17: The Internal of Serverless Plugins

Tips

17

とにかくServerless Frameworkのソースコードを読みまくろう

各コマンドは全て同じ方式で書かれているので色々参考になる

わりとよくできてる感

開発中はPJディレクトリで ‘npm link ./‘ しておくとデバッグしやすい

‘npm install serverless’ すると開発中のPluginを実際に動かせる

Page 18: The Internal of Serverless Plugins

18

Page 19: The Internal of Serverless Plugins

おまけ①

19

Pluginが増えると管理が面倒

本体に突っ込めるものは突っ込む

Serverless Teamはやさしい、レビューがとても丁寧

‘rollback function’ はどう考えても本体でやってほしいのでプルリク中

+1 オナシャス!!

https://github.com/serverless/serverless/pull/2672

Page 20: The Internal of Serverless Plugins

おまけ②

20

これからはServerless Framework? Lamveryは?

Lamveryも使ってますし、メンテも続けます

Function単位でGlue Codeを管理するには我ながら便利

私的な使い分け

Glue CodeをFunction単位で楽に良い感じで管理したい → Lamvery

Multi Functionでガッツリ → Serverless Framework(+Plugins)

Page 21: The Internal of Serverless Plugins

おまけ③

21

正直、Serverless Frameworkの気に物足りない所はまだまだ一杯ある 根本的にライブラリの依存関係がFunctionで独立してない Micro-servicesとは? (この辺は考えはあるようではある) Function毎でも全部でもない論理的なグループでのDeploy (+Rollback) 全体の方のRollbackが引数でタイムスタンプ指定するとか・・・「お前はいつ何をDeployしたか覚えているのか!?」と言いたい などなど

Page 22: The Internal of Serverless Plugins

22

Page 23: The Internal of Serverless Plugins

まとめ

23

Pluginを作るのは簡単

v1.0から本当に作りが良くなった(前がアレという話も・・・w)

正直、まだまだ物足りないので色々作っていきたい

アイデア次第で色々できる

みんなでPlugin作ろう!!

Page 24: The Internal of Serverless Plugins

24

・・・

Page 25: The Internal of Serverless Plugins

25

Page 26: The Internal of Serverless Plugins