In my previous article, I talked about how AWS Chalice helps you quickly build a Python-based serverless application and deploy it on AWS within a few minutes.

While it was a quick and fun prototype, Python may not be the language of choice for many when it comes to running large scale production applications.

Many organisations use Java as their primary development language, and a lot of developers are also moving towards newer languages like Go.

In this article, I will walk you through the steps required to build and deploy the same serverless application that gets the latest news from Google News. But this time, we will use the AWS Serverless Application Model (SAM) and Java for our development.

Like Chalice, the AWS SAM CLI offers a rich set of tools that enable developers to build serverless applications quickly.

Prerequisites

This tutorial requires an AWS account. If you don’t have one already, go ahead and create one. Our application is going to use only the free-tier resources, so cost shouldn’t be an issue.

You also need to configure security and create users and roles for your access.

How to Configure AWS Credentials

Chalice uses the AWS Command Line Interface (CLI) behind the scenes to deploy the project. If you haven’t used AWS’s CLI before to work with AWS resources, you can install it by following the guidelines here.

Once installed, you need to configure your AWS CLI to use the credentials from your AWS account.

How to Install SAM

Next, you need to install SAM. We will be using Java in this tutorial, but you can use any language runtime supported by AWS Lambda.

Verify Java Installation

$ java --version

openjdk 11.0.8 2020-07-14
OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.8+10)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.8+10, mixed mode)

Install SAM CLI

Depending on your OS, the installation instructions for the SAM CLI will vary. This article covers the instructions for installing it on MacOS.

The recommended approach for installing the SAM CLI on macOS is to use the Homebrew package manager.

Verify that you have Homebrew installed, like this:

$ brew --version

Homebrew/homebrew-core (git revision fe68a; last commit 2020-10-15)
Homebrew/homebrew-cask (git revision 4a2c25; last commit 2020-10-15)

If not, you can install Homebrew using the following command:

$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

Next, install SAM using the following command:

brew tap aws/tap
brew install aws-sam-cli

Verify SAM Installation

$ sam --version

SAM CLI, version 1.6.2

How to Create a Project

Next, run the sam-init command to create a new project.

sam init -r java11 -d maven --app-template hello-world -n daily-news-java

By default, SAM creates a Python project. Since we want to create a Java project, we will need to pass some additional parameters.

Parameters:

  • -r java11: use the Java 11 runtime
  • -d maven: use maven as the dependency manager
  • --app-template hello-world: use the HelloWorld quick start template
  • -n daily-news-java: the name of our project

This will create a daily-news-java folder in your current directory. You can see that SAM has created several files in this folder.

Let’s take a look at the App.java file.

APIGatewayProxyResponseEvent response = new APIGatewayProxyResponseEvent()
        .withHeaders(headers);
try {
  final String pageContents = this.getPageContents("https://checkip.amazonaws.com");
  String output = String
      .format("{ \"message\": \"hello world\", \"location\": \"%s\" }", pageContents);

  return response.withStatusCode(200).withBody(output);
} catch (IOException e) {
  return response.withBody("{}").withStatusCode(500);
}

The sam-init command created a simple Lambda function that returns the JSON body {"message": "hello world"} and the machine’s IP address when called. We can now change this template and add more code to read news from Google.

Now let’s take a look at the template.yml file.

Resources:
  HelloWorldFunction:
    Type: AWS::Serverless::Function ## More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
    Properties:
      CodeUri: HelloWorldFunction
      Handler: helloworld.App::handleRequest
      Runtime: java11
      MemorySize: 512
      Environment: ## More info about Env Vars: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#environment-object
        Variables:
          PARAM1: VALUE
      Events:
        HelloWorld:
          Type: Api ## More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
          Properties:
            Path: /hello
            Method: get

#serverless #aws #java #programming #developer

How to Build a Serverless Application Using AWS SAM
10.05 GEEK