LoginSignup

This article is a Private article. Only a writer and users who know the URL can access it.

More than 5 years have passed since last update.

posted at

updated at

Scalaで貧者のServerless

1 / 11

準備するもの

  • sbt 0.13.13
  • Bluemixアカウント(※米国南部リージョン)
  • OpenWhisk CLI

new project

modalsoul/openwhisk-sbt.g8

sbt new modalsoul/openwhisk-sbt.g8

※ sbt 0.13.13からgiter8テンプレートが使えるようになったぞ


サンプル

package com.github.modalsoul.openwhiskproject
import com.google.gson.JsonObject
object OpenwhiskProject {
  def main(args:JsonObject):JsonObject = {
    val obj = new JsonObject()
    obj.addProperty("greeting", s"Hello openwhisk project!")
    obj
  }
}

gsonのJsonObjectを受け取って、JsonObjectを返すエントリーポイントを作る。


ビルド

> sbt assembly
...
[info] Packaging ./target/scala-2.12/[hoge].jar ...
[info] Done packaging.

sbt packageだと依存するライブラリをjarファイルに取り込んでくれないので注意。


デップローイ

wsk action create [ACTION_NAME] target/scala-2.12/[APPLICATION].jar --main [MAIN_CLASS_FULLNAME]

動作確認

REST APIを叩く

curl -d '{ "arg": "value" }' 
 'https://openwhisk.ng.bluemix.net/api/v1/namespaces/[NAMESPACE]/actions/[ACTION_NAME]?blocking=true' 
 -XPOST -H 'Content-Type: application/json' 
 -H 'Authorization: Basic [YOUR_AUTH_TOKEN]'

Bluemix DashboardからRESTエンドポイント・プロパティで確認しよう


レスポンス(抜粋)

{
  ...
  "response": {
    "result": {
      "greeting": "Hello World!"
    },
    "success": true,
    "status": "success"
  },
  ...
}


¥の話

400,000GByte-Sec/Monthが無料枠

メモリ256MB割当で実行時間1秒のアクションなら1,600,000回無料!!


これであなたも!

_人人人人人人人人人人人人人人人人人人人_
>  ScalaでServerlessやってます (ドヤ <
 ̄Y^Y^Y^Y^Y^Y^Y^Y^Y^Y^Y^Y^Y^Y^ ̄


Scalaで貧者のServerless

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
What you can do with signing up