LoginSignup
16
14

More than 5 years have passed since last update.

Serverless Framework on Azure 試してみた

Last updated at Posted at 2017-10-02

ちょっと試しに、Serverless Framework を試してみた。

セットアップ方法

これはとても簡単。node v6.5.0以上が入っているなら次のでおっけー。なんの問題もなくインストールできた。(当方 Mac Book Pro)

npm install -g serverless 
npm install -g serverless-azure-functions

これで終了。簡単だ。

function を作成してデプロイ。

$ serverless create --template azure-nodejs --path my-service --name my-unique-name
$ cd my-service
$ npm install

これも手順の通りだ。テンプレートができているので、デプロイしよう。

serverless deploy
Serverless: Packaging service...
Serverless: Excluding development dependencies...
Serverless: Logging in to Azure
Serverless: Open a browser to https://aka.ms/devicelogin and provide the following code (which is copied to your clipboard!) to complete the login process: GYS4UB337

ログインのコードを求められるので、ブラウザで上記のURLを打ってコードを入れると、デプロイされる。

Serverless: Creating resource group: my-unique-name-rg
Serverless: Creating function app: my-unique-name
Serverless: Waiting for Kudu endpoint...
Serverless: Parsing Azure Functions Bindings.json...
Serverless: Building binding for function: hello event: httpTrigger
Serverless: Building binding for function: hello event: http
Serverless: Packaging function: hello
Serverless: Syncing Triggers....Response statuscode: 200
Serverless: Running Kudu command del package.json...
Serverless: Uploading pacakge.json...
Serverless: Running Kudu command npm install --production...
Serverless: Successfully created Function App

リソースグループが作られて、そこに、functions が作られて、デプロイされている。

Service Principal を使ったデプロイ

Azure に慣れている人だったら、なんでサービスプリンシパルを使わないんだろうと思うだろう。もちろん、サポートされている。次の環境変数をセットしてみよう。

export azureSubId='<subscriptionId>'
export azureServicePrincipalTenantId='<tenantId>'
export azureServicePrincipalClientId='<servicePrincipalName>'
export azureServicePrincipalPassword='<password>'

次に、serverless.yaml ができているのでそれを編集。自分の好きなFunctions の名前を指定できる。どうやらバインディングスも多少は設定できるっぽい。service と書いている項目が、FunctionsApp 名になっている。つまり、この中に functions をたくさん持てる単位。


# Welcome to Serverless!
#
# This file is the main config file for your service.
# It's very minimal at this point and uses default values.
# You can always add more config options for more control.
# We've included some commented out config examples here.
# Just uncomment any of them to get that config option.
#
# For full config options, check the docs:
#    docs.serverless.com
#
# Happy Coding!

service: my-unique-name

# You can pin your service to only deploy with a specific Serverless version
# Check out our docs for more details
# frameworkVersion: "=X.X.X"

provider:
  name: azure
  location: West US

plugins:
  - serverless-azure-functions

# you can add packaging information here
#package:
#  include:
#    - include-me.js
#    - include-me-dir/**
#  exclude:
#    - exclude-me.js
#    - exclude-me-dir/**

functions:
  hello:
    handler: handler.hello
    events:
      - http: true
        x-azure-settings:
          authLevel : anonymous
      - http: true
        x-azure-settings:
          direction: out
          name: res

# The following are a few examples of other events you can configure:
#
# events:
#   - queue: YourQueueName
#     x-azure-settings:
#       connection : StorageAppSettingName
#   - blob:
#     x-azure-settings:
#       name: bindingName
#       direction: in

ちなみにここで早まってはいけない。一つ重大なポイントがある。service に既存のFunctionsApp 名を指定してデプロイしてはいけない。

それを、私がやった結果w

Screen Shot 2017-10-02 at 8.58.41 PM.png

serverless deploy は、サービス全体のデプロイ(つまり、FunctionsApp) のデプロイなので、なんと、既存のがあったら、がっつり既存の functions を削除して作り直そうとしてしまう。(その上失敗するw)

だから、使われていない、Service 名を使って、デプロイすると、無事デプロイされる。

Screen Shot 2017-10-02 at 9.56.26 PM.png

まとめ

実際に触ってみると、相当軽快にデプロイできた。コマンドラインというのも嬉しい。ただし、serverless/serverless-azure-functions をみても、コマンドのサブコマンドが未実装なのが多い。これはコントリビュートするしかないだろう。しかし、コマンドが充実したら実に快適な感じなので、ちょっと貢献してみたいプロジェクトだ。

16
14
0

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
16
14