Serverless Framework
Serverless Framework - Build applications on AWS Lambda, Google CloudFunctions, Azure Functions, AWS Flourish and moreはあまたあるクラウドベンダーのサーバーレスアプリケーションを効率よく開発するためのスキャフォールド(足場)やワークフローの自動化などを提供するCLIツールです。イメージとしてはDockerHubのLambda版という感じだと思います。
serverless/serverless: Serverless Framework – Build web, mobile and IoT applications with serverless architectures using AWS Lambda, Azure Functions, Google CloudFunctions & more! –
インストール
インストールはnpmコマンドで実行します。
npm install -g serverless
インストールが完了したらバージョンを確認します。
$ sls -v
1.27.2
createコマンド
Serverless Frameworkでは以下の通りServerlessアプリケーションの雛形を生成することができます。こうしてみるといろいろな雛形があります。
sls create --help
Plugin: Create
create ........................ Create new Serverless service
--template / -t .................... Template for the service. Available templates: "aws-nodejs", "aws-nodejs-typescript", "aws-nodejs-ecma-script", "aws-python", "aws-python3", "aws-groovy-gradle", "aws-java-maven", "aws-java-gradle", "aws-kotlin-jvm-maven", "aws-kotlin-jvm-gradle", "aws-kotlin-nodejs-gradle", "aws-scala-sbt", "aws-csharp", "aws-fsharp", "aws-go", "aws-go-dep", "azure-nodejs", "fn-nodejs", "fn-go", "google-nodejs", "kubeless-python", "kubeless-nodejs", "openwhisk-java-maven", "openwhisk-nodejs", "openwhisk-php", "openwhisk-python", "openwhisk-swift", "spotinst-nodejs", "spotinst-python", "spotinst-ruby", "spotinst-java8", "webtasks-nodejs", "plugin" and "hello-world"
--template-url / -u ................ Template URL for the service. Supports: GitHub, BitBucket
--template-path .................... Template local path for the service.
--path / -p ........................ The path where the service should be created (e.g. --path my-service)
--name / -n ........................ Name for the service. Overwrites the default name of the created service.
最初のプロジェクト
今回は試しにAWS LabmdaへPython3.6を利用したサンプルを作成してみたいと思います。なお、利用する環境ではAWSのアクセスキーとアクセスキーIDはあらかじめ設定されていてaws cli でAWSへアクセスできることを確認してください。
sample
プロジェクトを作成するためcreate
コマンドを利用します。
$ sls create -t aws-python3 -p sample
Serverless: Generating boilerplate...
Serverless: Generating boilerplate in "/Users/ymanabe/projects/sandbox/sample"
_______ __
| _ .-----.----.--.--.-----.----| .-----.-----.-----.
| |___| -__| _| | | -__| _| | -__|__ --|__ --|
|____ |_____|__| \___/|_____|__| |__|_____|_____|_____|
| | | The Serverless Application Framework
| | serverless.com, v1.27.2
-------'
Serverless: Successfully generated boilerplate for template: "aws-python3"
これでサンプルプロジェクトが作成されましたので、サンプルプロジェクトへ移動します。
$ cd sample
$ ls -al
total 24
drwxr-xr-x 5 ymanabe staff 170 5 13 16:27 .
drwxr-xr-x 4 ymanabe staff 136 5 13 16:27 ..
-rw-r--r-- 1 ymanabe staff 192 5 13 16:27 .gitignore
-rw-r--r-- 1 ymanabe staff 497 5 13 16:27 handler.py
-rw-r--r-- 1 ymanabe staff 2839 5 13 16:27 serverless.yml
このようにhandler.py
とserverless.yml
というファイルが作成されています。
- handler.py:Lambda関数として処理を記述するファイル
- serverless.yml:Serverless FrameworkでLambdaを利用するための設定を記述するファイル
Lambda関数を作成する
ここまでで作成された内容をそのままAWS LambdaへLambda関数として作成してみたいと思いますが、このまま実行するとus-east-1
にLambda関数が作成されます。できれば最初はap-northeast-1
がいいという場合には以下の通り設定を変更してください。
@@ -23,7 +23,7 @@ provider:
# you can overwrite defaults here
# stage: dev
-# region: us-east-1
+ region: ap-northeast-1
# you can add statements to the Lambda function's IAM Role here
# iamRoleStatements:
実際に実行してみます。実行はdeploy
コマンドで実行します。(下記はus-east-1
で実行したログです。)
$ sls deploy -v -s dev
Serverless: Packaging service...
Serverless: Excluding development dependencies...
Serverless: Creating Stack...
Serverless: Checking Stack create progress...
CloudFormation - CREATE_IN_PROGRESS - AWS::CloudFormation::Stack - sample-dev
CloudFormation - CREATE_IN_PROGRESS - AWS::S3::Bucket - ServerlessDeploymentBucket
CloudFormation - CREATE_IN_PROGRESS - AWS::S3::Bucket - ServerlessDeploymentBucket
CloudFormation - CREATE_COMPLETE - AWS::S3::Bucket - ServerlessDeploymentBucket
CloudFormation - CREATE_COMPLETE - AWS::CloudFormation::Stack - sample-dev
Serverless: Stack create finished...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Uploading service .zip file to S3 (390 B)...
Serverless: Validating template...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
...
Serverless: Stack update finished...
Service Information
service: sample
stage: dev
region: us-east-1
stack: sample-dev
api keys:
None
endpoints:
None
functions:
hello: sample-dev-hello
Stack Outputs
HelloLambdaFunctionQualifiedArn: arn:aws:lambda:us-east-1:123456789:function:sample-dev-hello:1
ServerlessDeploymentBucketName: sample-dev-serverlessdeploymentbucket-123456789
上記の通り
Serverless: Stack update finished...
と出力されればリリースは完了となります。
コンソールで確認する
正しくLambda関数が作成されたかをAWS コンソールで確認してみます。
Pythonのライブラリも含められる
Pythonを利用するときにはたいていNumPy — NumPyやPillow — Pillow (PIL Fork) 5.1.1 documentationを利用します。これらはAWS Lambdaの実行環境には含められていないため、Lambda関数を作成するときにパッケージとして含める必要があります。
このような処理についてもServerless Frameworkは対応していてDockerコンテナを利用してAWS Lambdaで動作するパッケージを作成してデプロイまで自動化することができます。
この辺はまた別な機会にまとめたいと思いますが、とても便利なツールです。
さいごに
マイクロサービスの一環としてServerlessなAPIを構築することが結構多くなってきました。対象とする機能ごとにそれぞれ得意な言語を利用してAPIから呼び出せる仕組みは適材適所でありより一層利用する機会が増えてくると思います。
そんな中でServerless FrameworkのようなCLIツールが有ることはとてもありがたいことです。このお陰で処理を開発することに注力できるようになります。素晴らしいことですね。
補足
AWS LambdaがよくわからないときはいきなりServerless Frameworkとか利用しないでこちらの書籍を読むといいと思います。
言語ごとの説明もいくつかあるので実際にはNodeかPythonのサンプルをみて試してみて、実際に幾つかコンソールへ貼り付けたりS3からデプロイしてみてからのほうがServerless Frameworkの便利さはわかると思います。いずれにしてもAWS LambdaやAzure Functionsはとてもおもしろいので食わず嫌いせずに試してみてください!