Skip to content

simple scala gcp cloud functions hello world example

Notifications You must be signed in to change notification settings

i10416/scala-functions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

scala-functions

simple scala gcp cloud functions hello world example with sbt

mavenではなくsbtを使ってscalaのソースをgoogle cloud functionsにデプロイするサンプル

googleの公式ドキュメントではmavenを使ってfat .jarをビルドすることができると書かれているが、sbt-assembly pluginを使うことでsbtでもビルドすることができる

guide

use maven google cloud functions library with 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",
  )

Install sbt-assembly plugin to build fat .jar file.

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ファイルが生成される.

upload zip file

※ここではCLIではなくGUIからアップロードする

Before uploading, zip target/rootDirName-assebly-x.x.x-SNAPSHOT.jar.

アップロードする前に.jarファイルをzip圧縮する

In GCP cloud functions console,

  1. create a function
  • set function name and region
  • set function trigger[default: http]
  1. choose run type
  • java 11
  1. upload zipped fat .jar file.
  2. enable cloud build api
  3. 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")
  }
}

About

simple scala gcp cloud functions hello world example

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published