This post is Private. Only a writer or those who know its URL can access this post.

Improve article
Show article in Markdown
Report article
Help us understand the problem. What is going on with this article?

Scalaで貧者のServerless

More than 3 years have passed since last update.

Scalaで貧者のServerless

by modal_soul
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

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away