A fast and easy way to deploy scala based lambdas to AWS using sbt

11 Apr, 2018
Xebia Background Header Wave

The development cycle of AWS lambdas in Scala is hell. There are so many packaging and configuration steps that
it destroys my productivity. To solve this, we created a Scala Build Tool Plugin called sbt-sam.
The plugin makes deploying serverless applications as easy as compiling your code.
In this blog we will show you how easy it is to create a REST service using Scala, AWS API Gateway and AWS Lambda.
We assume the following:

  • you are using OSX
  • you have SBT installed
  • you have an AWS account
  • the AWS account has enough privileges to deploy serverless applications
    You can create a serverless application in Scala using AWS Lambda in 3 easy steps:

  • Create a SBT project

  • Deploy the application
  • Call the application

Create a SBT project

To create a new serverless application project, type:

sbt new dnvriend/sam-seed.g8

The project asks some questions, press enter to accept the defaults:

name [sam-seed]:
package [com.github.dnvriend]:
organization [com.github.dnvriend]:
author_name [dnvriend]:
stage [dev]:

Template applied in ./sam-seed

To sbt in the created project type:

$ cd sam-seed
$ sbt

The plugin adds the following commands to SBT:

  • samInfo – to get information from your serverless application
  • samValidate – to view the generated CloudFormation template
  • samDeploy – to deploy the serverless application
  • samRemove – to remove the serverless application
    The template lambda implementation is show below:
package com.github.dnvriend

import com.github.dnvriend.lambda.annotation.HttpHandler
import com.github.dnvriend.lambda._
import play.api.libs.json._

@HttpHandler(path = "/hello", method = "get")
 class HelloLambda extends ApiGatewayHandler {
    override def handle(
      request: HttpRequest, 
      ctx: SamContext
    ): HttpResponse = {
          .withBody(Json.toJson("Hello world"))

The HelloLambda class extends ApiGatewayHandler, which makes it an AWS Lambda. The lambda is annotated
with HttpHandler with the path ‘/hello’ and the method ‘get’. This means the lambda is accessible via API Gateway.

Deploy the application

Deploy the application by typing samDeploy:

sbt:sam-seed> samDeploy
[info] Creating cloud formation stack
[info] CREATE_IN_PROGRESS - AWS::S3::Bucket - SbtSamDeploymentBucket - CREATE_IN_PROGRESS -
[info] CREATE_IN_PROGRESS - AWS::S3::Bucket - SbtSamDeploymentBucket - CREATE_IN_PROGRESS - Resource creation Initiated
[success] Total time: 102 s, completed Apr 11, 2018 7:04:00 PM

After the service is deployed, use samInfo to get the URL to call the service:

sbt:sam-seed> samInfo
[info] ====================
[info] Name: com-github-dnvriend-dnv-sam-seed
[info] content: com-github-dnvriend-dnv-sam-seed
[info] Status: UPDATE_COMPLETE
[info] Endpoints:
[info] GET -

Call the application

Call the endpoint by typing:

$ curl
"Hello world"

Change the text ‘Hello World’ to ‘Goodbye World’ and type samDeploy. The new code will be deployed to AWS
and call the same endpoint again:

$ curl
"Goodbye World"


With the SAM plugin for SBT, developing serverless applications is as easy as compiling code.
A quick way to create serverless applications is to make use of template projects. But it does not end here.
The plugin support many more AWS services and features. We will demonstrate how to use these in future blogs.
Next time we’ll look how to secure the web service using Amazon Cognito User Pools. We will create a Scala application that
authenticates with Cognito and calls the web service.
If you have any questions, do not hesitate to contact me.


Get in touch with us to learn more about the subject and related solutions

Explore related posts