simple scala gcp cloud functions hello world example with sbt
mavenではなくsbtを使ってscalaのソースをgoogle cloud functionsにデプロイするサンプル
googleの公式ドキュメントではmavenを使ってfat .jarをビルドすることができると書かれているが、sbt-assembly pluginを使うことでsbtでもビルドすることができる
see https://cloud.google.com/functions/docs/concepts/jvm-langs
<dependency>
<groupId>com.google.cloud.functions</groupId>
<artifactId>functions-framework-api</artifactId>
<version>1.0.1</version>
<scope>provided</scope>
</dependency>
MavenのXMLはsbtのlibraryDependenciesと対応しているので以下のように変換する.
Maven's xml corresponds to sbt libraryDependencies.
Translate this like below.
libraryDependencies ++= Seq(
"com.google.cloud.functions" % "functions-framework-api" % "1.0.1",
)
To deploy functions on java 11 runtime, fat .jar file is required.
Java 11 ランタイムで動くソースをアップロードする際にはfat .jarファイルが必要なのでsbt-assembly pluginをproject/plugins.sbt
に追加する
- add project/plugins.sbt
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.15.0")
Run assembly
command in sbt shell to build fat .jar file at target/rootDirName-assebly-x.x.x-SNAPSHOT.jar
.
sbt shellでassembly
コマンドを実行するとtarget/
以下にfat .jarファイルが生成される.
※ここではCLIではなくGUIからアップロードする
Before uploading, zip target/rootDirName-assebly-x.x.x-SNAPSHOT.jar
.
アップロードする前に.jarファイルをzip圧縮する
In GCP cloud functions console,
- create a function
- set function name and region
- set function trigger[default: http]
- choose run type
- java 11
- upload zipped fat .jar file.
- enable cloud build api
- choose a bucket or create the new bucket if not exists.
エントリーポイントはパッケージ名とクラス名に対応させる. 以下のようなScala ソースがあるとき、エントリーポイントはfunctions.ScalaHelloWorldになる.
Set entrypoint as packageName.className.
With Scala source below, for example, set entrypoint as functions.ScalaHelloWorld
package functions
import com.google.cloud.functions.{HttpFunction, HttpRequest, HttpResponse}
class ScalaHelloWorld extends HttpFunction {
override def service(httpRequest: HttpRequest, httpResponse: HttpResponse):Unit = {
httpResponse.getWriter.write("hello world")
}
}