Skip to content

Instantly share code, notes, and snippets.

@johnjansen
Last active November 24, 2017 16:43
Show Gist options
  • Save johnjansen/73f2ad84efb82a83d8dc to your computer and use it in GitHub Desktop.
Save johnjansen/73f2ad84efb82a83d8dc to your computer and use it in GitHub Desktop.
AWS Lambda Microservice — Scala
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
}
package blahblah;
import scala.collection.JavaConverters._
import java.net.URLDecoder
import com.amazonaws.services.lambda.runtime.Context
import java.io.{InputStream, OutputStream, PrintStream}
class Main {
def api(input: InputStream, output: OutputStream, context:Context): Unit = {
val logger = context.getLogger()
logger.log("Hi")
// oh yeah, you can do this too, thanks to that PrintStream import
System.out.println("WHAT")
System.err.println("WHO?")
output.write("HELLO WORLD".getBytes("UTF-8"))
}
// another option for the following is to do this,
// HOWEVER, i havent tried this yet
// see this if you think this looks interesting http://docs.aws.amazon.com/lambda/latest/dg/java-handler-using-predefined-interfaces.html
// and i havent looked at the scala equivalent to the following ...
// public class Hello implements RequestHandler<Request, Response>
/*
import com.amazonaws.services.lambda.runtime.RequestHandler;
def api(request: Request, context: Context):Response {
val response = new Response("Hello World")
}
*/
}
@johnjansen
Copy link
Author

Just enough Scala to build an AWS Lambda based Microservice
also note the reference to logger, see this for more http://docs.aws.amazon.com/lambda/latest/dg/java-context-object.html
you should probably read this too http://docs.aws.amazon.com/lambda/latest/dg/java-lambda.html
and maybe this https://aws.amazon.com/blogs/compute/writing-aws-lambda-functions-in-scala/
and maybe even take a look at snowplow http://snowplowanalytics.com/blog/2015/08/20/aws-lambda-scala-example-project-0.1.0-released/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment