以前の投稿に続いて、同じくServerless Architectureを支えるフレームワークとしてApexを触ってみたのでその所感をば。
SERVERLESS FRAMEWORKを使ってみた
http://apex.run/
インストール
公式サイトにありますが、これもかなりシンプルですね。
$ curl https://raw.githubusercontent.com/apex/apex/master/install.sh | sh
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 1646 100 1646 0 0 629 0 0:00:02 0:00:02 --:--:-- 629
awsコマンドのconfigure設定が必要だけど、こちらは省きます。
Apexの初期化を実施します。対話式に進むのはいいですね。
apex init
_ ____ _______ __
/ \ | _ \| ____\ \/ /
/ _ \ | |_) | _| \ /
/ ___ \| __/| |___ / \
/_/ \_\_| |_____/_/\_\
Enter the name of your project. It should be machine-friendly, as this
is used to prefix your functions in Lambda.
Project name: [[apex-first-sample]]
Enter an optional description of your project.
Project description: [[first sample project for Apex]]
[+] creating IAM apex-first-sample_lambda_function role
[+] creating IAM apex-first-sample_lambda_logs policy
[+] attaching policy to lambda_function role.
[+] creating ./project.json
[+] creating ./functions
Setup complete, deploy those functions!
$ apex deploy
[[]]
で囲んだ部分がユーザが入力すべきところです。コマンド実行が完了すると、project.jsonというファイルとfunctions/hello/index.jsというファイルが出来上がります。
{
"name": "apex-first-sample",
"description": "first sample project for Apex",
"memory": 128,
"timeout": 5,
"role": "arn:aws:iam::XXXXXXXXXXXX:role/apex-first-sample_lambda_function",
"environment": {}
}
console.log('starting function')
exports.handle = function(e, ctx, cb) {
console.log('processing event: %j', e)
cb(null, { hello: 'world' })
}
さて、これらをデプロイして実行してみます。
$ apex deploy
• creating function function=hello
• created alias current function=hello version=1
• function created function=hello name=apex-first-sample_hello version=1
$ apex invoke hello
{"hello":"world"}
ちなみにSERVERLESS FRAMEWORKではデフォルトでus-east-1へのデプロイでしたが、Apexの方はconfigureの設定と同じap-northeast-1にデプロイしてくれてました。
他のコマンド一覧
$ apex
Usage:
apex [command]
Available Commands:
version Print version of Apex
build Build a function
delete Delete functions
deploy Deploy functions and config
docs Output documentation
infra Infrastructure management
init Initialize a project
invoke Invoke functions
list Output functions list
logs Output function logs
metrics Output function metrics
rollback Rollback functions
upgrade Upgrade apex to the latest stable release
Flags:
-C, --chdir string Working directory
-D, --dry-run Perform a dry-run
-e, --env string Infastructure environment (default: "defaultEnvironment" from project.json)
-h, --help help for apex
-l, --log-level string Log severity level (default "info")
-p, --profile string AWS profile
-r, --region string AWS region
Use "apex [command] --help" for more information about a command.
rollbackあたりも気になりますが、ここは--dry-runオプションが気になるので使ってみます。
--dry-runオプションを試してみる
特に気になったので試してみます。deploy時につけることで、dry runが可能になるようです。
$ apex delete hello
Are you sure? (yes/no) yes
• deleting function=hello
• function deleted function=hello
一旦削除してからdry runを試してみます。
apex deploy --dry-run hello
+ function apex-first-sample_hello
runtime: nodejs4.3
memory: 128
timeout: 5
handler: _apex_index.handle
+ alias apex-first-sample_hello
alias: current
version: 1
「+」マークは「この名前のファンクションが追加されるよ」という意味のようです。project.jsonのメモリを192に書き換えると、memory部分も192に変わりました。
$ apex deploy --dry-run hello
+ function apex-first-sample_hello
runtime: nodejs4.3
memory: 192
timeout: 5
handler: _apex_index.handle
+ alias apex-first-sample_hello
alias: current
他のコマンドも考えると、デフォルトで用意されているものはSERVERLESSよりも多い感じがしますね。ただSERVERLESSの方は豊富なプラグインがあるので、どちらをどう選ぶかは、やりたいことと開発プロセスによりそうな気もしています。
metricsでファンクションの実行状況を探る
metricsコマンドを利用することで、そのファンクションの利用状況がわかるようです。これは地味に便利だぞ。
$ apex metrics hello
hello
total cost: $0.00
invocations: 2 ($0.00)
duration: 37ms ($0.00)
throttles: 0
errors: 0
memory: 128
total costまで出るのは嬉しいですね。これちょっとスクリプト組んどけば、costが変に跳ね上がった時とかに通知飛ばせるんじゃなかろうか。
まとめ
使いやすさ、導入しやすさという点ではSERVERLESS FRAMEWORK同様かなり簡単に感じました。またデフォルトで備えている機能やコマンドが豊富なのも魅力的ですね。SERVERLESSの方はプラグイン開発も活発なので、自分がやりたいことがそれぞれどのように実現できるのかを判断する必要がありそうです。