LoginSignup
7
6

More than 5 years have passed since last update.

SERVERLESS FRAMEWORKを使ってみた

Posted at

よくAWS Lambda、API Gatewayなどのいわゆるサーバレスアーキテクチャをいじる機会があり、SERVERLESS FRAMEWORKを触ってみたのでその所感を。

Node.jsのバージョンはv4.2.4以上が必要?

どうもNode.jsのv4.2.3でコマンドを実施したらインストールに必要なファイルが入っていないっぽい。で、導入用の動画ではv4.2.4でコマンドを実施していたので、Node.jsのバージョンが古い場合はバージョンアップが必要そう。

動かしてみる

aws configureでまず認証情報を設定します。

$ aws configure
AWS Access Key ID [****************xxxx]: 
AWS Secret Access Key [****************xxxx]: 
Default region name [ap-northeast-1]: 
Default output format [json]: 

設定が完了したら、コマンドでプロジェクトを作成してみます。

$ serverless create --template aws-nodejs
Serverless: Creating new Serverless service...
 _______                             __
|   _   .-----.----.--.--.-----.----|  .-----.-----.-----.
|   |___|  -__|   _|  |  |  -__|   _|  |  -__|__ --|__ --|
|____   |_____|__|  \___/|_____|__| |__|_____|_____|_____|
|   |   |             The Serverless Application Framework
|       |                           serverless.com, v1.0.0-beta.1.1
 -------'

Serverless: Successfully created service in the current directory
Serverless: with template: "aws-nodejs"
Serverless: NOTE: Please update the "service" property in serverless.yml with your service name

すると以下のファイルが出来上がります。

event.json
handler.js
serverless.env.yml
serverless.yml

中身はサンプルのNode.jsによるAWS Lambdaの設定群になります。これをデプロイしてみます。helloという関数が出来上がっているので、それもクライアントから実行してみます。

handler.js
'use strict';

// Your first function handler
module.exports.hello = (event, context, cb) => cb(null,
  { message: 'Go Serverless v1.0! Your function executed successfully!', event }
);

// You can add more handlers here, and reference them in serverless.yml
$ serverless deploy
Serverless: Creating Stack...
Serverless: Checking stack creation progress...
..............
Serverless: Stack successfully created.
Serverless: Zipping service...
Serverless: Uploading .zip file to S3...
Serverless: Updating Stack...
Serverless: Checking stack update progress...
..
Serverless: Deployment successful!

S3にzipファイルがアップロードされているようです。で、設定が漏れましたがserverless.env.ymlファイルにはデフォルトでリージョンがus-east-1に設定されているので、ここを変更しておかないとリージョンが普段使っているap-northeast-1とは違ってしまうわけですね。これは注意ポイント。
で、無事Lambdaにも登録されていました。実行してみましょう。

スクリーンショット 2016-08-14 21.23.10.png

$ serverless invoke --function hello
{
    "message": "Go Serverless v1.0! Your function executed successfully!",
    "event": {}
}

無事実行できたみたいです。CloudWatchにも記録が残っていました。

START RequestId: 20211702-621a-11e6-be4a-************ Version: $LATEST 
END RequestId: 20211702-621a-11e6-be4a-************ 
REPORT RequestId: 20211702-621a-11e6-be4a-************  Duration: 2.84 ms   Billed Duration: 100 ms Memory Size: 1024 MB    Max Memory Used: 35 MB  

ちなみにコマンドの一覧を見てみましたが、一通り管理するためのコマンドは備えているようですね。

$ serverless

Commands
* Serverless documentation: http://docs.serverless.com
* You can run commands with "serverless" or the shortcut "sls"
* Pass "--help" after any <command> for contextual help

create ................... Create new Serverless Service.
deploy ................... Deploy Service.
deploy function .......... Deploys a single function from the service
info ..................... Displays information about the service.
invoke ................... Invokes a deployed function.
logs ..................... Outputs the logs of a deployed function.
remove ................... Remove resources.
tracking ................. Enable or disable usage tracking.

Plugins
AwsCompileApigEvents, AwsCompileFunctions, AwsCompileS3Events, AwsCompileSNSEvents, AwsCompileScheduledEvents, AwsDeploy, AwsDeployFunction, AwsInfo, AwsInvoke, AwsLogs, AwsRemove, Create, Deploy, Info, Invoke, Logs, Package, Remove, Tracking

まとめ

AWS Lambdaの関数を管理するフレームワークとしては、導入の手軽さやコマンドの簡易さが使いやすいと思いました。あとはDryRUNあたりがオプションで付いてくるとローカル開発をしていて嬉しいかな、という感じです。(どうやらGithub上では追加の方向で話が進んでいるようですが)他にもサーバレスアーキテクチャを支えるフレームワークはあるので、そちらに負けじと開発を頑張ってもらいたいです。

おまけ

ちなみにデフォルトのus-east-1以外にデプロイしようとするとどうなるのか試してみました。ap-northeast-1にデプロイを試みます。

まずはserverless.env.ymlファイルの中身を書き換えます。

serverless.env.yml
vars:
stages:
  dev:
    vars:
    regions:
      ap-northeast-1:
        vars:

regionsのところを書き換えました。デプロイを試みますが、どうにも引数なしではエラーが発生してしまうようです。

$ serverless deploy

  Serverless Error ---------------------------------------

     Region "us-east-1" doesn't exist in stage "dev"

  Get Support --------------------------------------------
     Docs:          v1.docs.serverless.com
     Bugs:          github.com/serverless/serverless/issues

デフォルト以外のリージョンにアップロードしたい場合は、--regionという引数が必要になるようです。こうすればアプロードされました。

$ serverless deploy --region ap-northeast-1
Serverless: Creating Stack...
Serverless: Checking stack creation progress...
...............
Serverless: Stack successfully created.
Serverless: Zipping service...
Serverless: Uploading .zip file to S3...
Serverless: Updating Stack...
Serverless: Checking stack update progress...
..
Serverless: Deployment successful!

これはGitHubのQuickStartにも載せておいてもらいたいかも。

7
6
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
6