Starter Projects For AWS Lambda Using NodeJS And Java

Today I want to show you three starter projects for AWS Lambda using CloudFormation and SAM – Serverless Application Model. I always like if I have some boilerplate code and can get started quickly without copying code or project structures from an existing (and mature) project. Therefore I thought it’s good to have them in one repository. You can find them on GitHub. The projects can be used for NodeJS and Java. Also one project contains both: usage of Java and NodeJS Lambdas in one CloudFormation template.

Note: All projects contain a deploy.sh file which you can run. Each deploy file is using the AWS CLI to package and deploy the CloudFormation template using aws cloudformation package and aws cloudformation deploy, so you first have to add an existing S3 bucket (where you have access to!) to deploy.sh.

 

NodeJS Starter Project: Spins up a simple NodeJS Lambda function which is available under the Api path /hello. Link

Java Starter Project: Spins up a simple Java Lambda function using the RequestStreamHandler interface. The project has added Jackson to parse input and output from/to a stream. Link

NodeJS and Java Starter Project: Spins up two simple AWS Lambda functions: one for NodeJS and one for Java. The neat point is here that it’s possible to declare both AWS Lambda functions for NodeJS and Java in one CloudFormation template. You just have to use different values for Handler, Runtime and CodeUri. In the background, the AWS CLI packages and uploads both artifacts to S3. The functions are available under the Api path /node and /java. Link

Example code how to use NodeJS and Java in one project:

# Declare other stuff...

Resources:
  NodeFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: index.handler
      Runtime: nodejs6.10
      Timeout: 10
      CodeUri: ./node-backend/target
      Policies:
        - AWSLambdaBasicExecutionRole
      Events:
        GetNodeResource:
          Type: Api
          Properties:
            Path: /node
            Method: get

  JavaFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: de.sebastianhesse.examples.JavaHelloWorldHandler
      Runtime: java8
      Timeout: 10
      CodeUri: ./java-backend/target/target.jar
      Policies:
        - AWSLambdaBasicExecutionRole
      Events:
        GetJavaResource:
          Type: Api
          Properties:
            Path: /java
            Method: get