Let's stay in contact

Welcome! and thank you for visiting my little home on the web. I would love to tell you more about my journeys in tech and running. I mostly write about what I learn from running long distances and building large scale web applications. I also enjoy reading your email responses.

Thank you, Chris

Click Here to Join Me

In this tutorial I show you how to package and deploy a simple Scala project to AWS Lambda. Then I show you how to add the lambda to API Gateway to run in response to GET requests.

Build the Scala Project

  • Create the build.sbt file
  • Write the main scala program, src/main/scala/example/Main.scala
  • In the terminal run sbt then in the sbt prompt run assembly.
  • This creates the jar which will be located at target/scala-2.11/lambda-demo-assembly-1.0.jar

build.sbt

javacOptions ++= Seq("-source", "1.8", "-target", "1.8", "-Xlint")

lazy val root = (project in file(".")).
  settings(
    name := "lambda-demo",
    version := "1.0",
    scalaVersion := "2.11.4",
    retrieveManaged := true,
    libraryDependencies += "com.amazonaws" % "aws-lambda-java-core" % "1.0.0",
    libraryDependencies += "com.amazonaws" % "aws-lambda-java-events" % "1.0.0"
  )

mergeStrategy in assembly :=
{
  case PathList("META-INF", xs @ _*) => MergeStrategy.discard
  case x => MergeStrategy.first
}

src/main/scala/example/Main.scala

package example;

class Main {
  import java.io.{InputStream, OutputStream, PrintStream}

  def greeting(input: InputStream, output: OutputStream): Unit = {
    val result = s"Yo!"
    output.write(result.getBytes("UTF-8"))
  }
}

Deploy the Scala Project to AWS Lambda

  • Select Java 8
  • Upload the jar which will be located at /target/scala-2.11/lambda-demo-assembly-1.0.jar
  • Test the Lambda Function from the AWS Lambda UI

At this point you could setup API Gateway to use the Scala Lambda in a similar way that I set it up for the node.js Lambda Function.

Chris Larson

I fell in love with running after I committed to running a 5k everyday during the month of September. Now I'm training for my first Marathon on October 1st, 2017.

chrylarson chrylarson


Published