the internal of serverless plugins
TRANSCRIPT
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
3
Agenda
4
Serverless Pluginとは
Pluginの作り方・基本編
Pluginの作り方・実例からみる応用編
Pluginの作り方・Tips
おまけ・まとめ
5
Serverless Plugin
6
Serverless Frameworkを拡張する、または挙動を置き換えるプラグイン
本体と同じNode.js(v4以上)で記述されたnpm module
大枠として以下の2つの役割を持つ
Commandの拡張・置き換え
各CommandのLife Cycle EventsにHookして処理を行う
7
基本形
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
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※定義の書き方は一緒
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
緑の下線部のメソッドに実際の処理を記述していく
11
12
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
14
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
16
Tips
17
とにかくServerless Frameworkのソースコードを読みまくろう
各コマンドは全て同じ方式で書かれているので色々参考になる
わりとよくできてる感
開発中はPJディレクトリで ‘npm link ./‘ しておくとデバッグしやすい
‘npm install serverless’ すると開発中のPluginを実際に動かせる
18
おまけ①
19
Pluginが増えると管理が面倒
本体に突っ込めるものは突っ込む
Serverless Teamはやさしい、レビューがとても丁寧
‘rollback function’ はどう考えても本体でやってほしいのでプルリク中
+1 オナシャス!!
https://github.com/serverless/serverless/pull/2672
おまけ②
20
これからはServerless Framework? Lamveryは?
Lamveryも使ってますし、メンテも続けます
Function単位でGlue Codeを管理するには我ながら便利
私的な使い分け
Glue CodeをFunction単位で楽に良い感じで管理したい → Lamvery
Multi Functionでガッツリ → Serverless Framework(+Plugins)
おまけ③
21
正直、Serverless Frameworkの気に物足りない所はまだまだ一杯ある 根本的にライブラリの依存関係がFunctionで独立してない Micro-servicesとは? (この辺は考えはあるようではある) Function毎でも全部でもない論理的なグループでのDeploy (+Rollback) 全体の方のRollbackが引数でタイムスタンプ指定するとか・・・「お前はいつ何をDeployしたか覚えているのか!?」と言いたい などなど
22
まとめ
23
Pluginを作るのは簡単
v1.0から本当に作りが良くなった(前がアレという話も・・・w)
正直、まだまだ物足りないので色々作っていきたい
アイデア次第で色々できる
みんなでPlugin作ろう!!
24
・・・
25