How to Build Serverless Application Using AWS SAM: Complete Step by Step Guide
By Braincuber Team
Published on April 8, 2026
AWS Serverless Application Model (SAM) offers a rich set of tools that enable developers to build serverless applications quickly. While Python may be great for quick prototypes, many organizations use Java as their primary development language for large-scale production applications. This tutorial will walk you through building and deploying a serverless application that fetches the latest news from Google News using AWS SAM and Java.
What You'll Learn:
- AWS SAM installation and configuration setup
- Creating a Java-based serverless project structure
- Building Lambda functions with Java 11 runtime
- Local testing using SAM CLI with Docker
- Deploying applications to AWS using guided deployment
- Cleaning up AWS resources after deployment
Prerequisites and Setup
This tutorial requires an AWS account. If you don't have one already, create one. Our application will use only free-tier resources, so cost shouldn't be an issue. You also need to configure security and create users and roles for your access.
Configure AWS Credentials
SAM uses AWS CLI behind the scenes. Install AWS CLI and configure it with your AWS account credentials using 'aws configure' command.
Verify Java Installation
Ensure Java 11 is installed. Check with 'java --version' command. This tutorial uses OpenJDK 11.0.8 or higher.
Install Docker
SAM CLI uses Docker internally for local testing. Install Docker Desktop from docker.com for your operating system.
Installing AWS SAM CLI
The installation instructions for SAM CLI vary by operating system. This tutorial covers macOS installation using Homebrew package manager.
# Verify Homebrew installation
brew --version
# Install Homebrew if needed
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
# Install SAM CLI
brew tap aws/tap
brew install aws-sam-cli
# Verify SAM installation
sam --version
Windows/Linux Installation
For Windows, download the MSI installer from AWS SAM CLI GitHub releases. For Linux, use package managers like yum or apt, or download the binary directly.
Creating Your Serverless Project
Now let's create a new serverless project using SAM. We'll build a Java application that fetches news from Google News RSS feed.
Initialize SAM Project
Run 'sam init -r java11 -d maven --app-template hello-world -n daily-news-java' to create a Java 11 Maven project.
Explore Project Structure
SAM creates several files including App.java (Lambda function), template.yml (CloudFormation template), and pom.xml (Maven configuration).
Update Maven Configuration
Update pom.xml to set compiler source from 1.8 to 11 to use Java 11's HTTP library features.
Understanding the Project Structure
template.yml
CloudFormation template defining AWS Lambda (Java 11, 512MB memory) and API Gateway resources with GET /hello endpoint.
App.java
Lambda function code that returns JSON response with message and IP address. We'll modify this to fetch news.
Building the News Application
Let's modify the template to create a news fetching application. We'll create a NewsItem class and add RSS feed parsing functionality.
public class NewsItem {
private String title;
private String publicationDate;
// Constructor and getters
@Override
public String toString() {
return "{\"title\":\"" + title + "\", \"publicationDate\":\"" + publicationDate + "\"}";
}
}
Java 11 HTTP Library
We use Java's internal HTTP and XML parsing libraries, avoiding external dependencies. The toString method creates JSON representation without parsing libraries.
Building and Testing Locally
Build the Application
From the daily-news-java folder, run the sam build command to compile source code and build dependencies.
sam build
Local Testing Options
SAM CLI provides two ways to test locally: hosting the API locally or directly invoking the Lambda function. This is extremely helpful during development.
Local API Hosting
Use 'sam local start-api' to create local server exposing REST API endpoint. Access via curl http://127.0.0.1:3000/hello
Direct Function Invocation
Use 'sam local invoke "HelloWorldFunction" -e events/event.json' to directly call Lambda function with event payload.
Deploying to AWS
Once you're satisfied with local testing, deploy your application to AWS using the guided deployment command.
Guided Deployment
Run 'sam deploy --guided' and follow prompts. SAM creates S3 bucket, packages artifacts, and deploys via CloudFormation.
Access Deployed API
SAM provides API Gateway endpoint URL. Test your live application using the provided endpoint.
Redeploy Changes
Modify App.java and run 'sam deploy' again to update your application with changes.
Cleaning Up Resources
After testing, it's important to clean up AWS resources to avoid unnecessary charges. Use AWS CloudFormation to delete the stack.
aws cloudformation delete-stack --stack-name daily-news-java
Resource Management
The delete-stack command removes the CloudFormation stack and all resources created during deployment, including Lambda functions and API Gateway.
Frequently Asked Questions
What is AWS SAM used for?
AWS SAM is an open-source framework for building serverless applications on AWS. It simplifies defining serverless resources with shorthand syntax and local testing capabilities.
Do I need Docker for SAM?
Docker is required for local testing with 'sam local' commands. SAM uses Docker to simulate the Lambda execution environment on your local machine.
What programming languages does SAM support?
SAM supports all AWS Lambda runtimes including Java, Python, Node.js, Go, .NET, Ruby, and custom runtimes. You choose the runtime during project initialization.
How much does SAM deployment cost?
SAM itself is free. You only pay for AWS resources used (Lambda, API Gateway, S3). The free tier covers most small applications during development and testing.
What's the difference between SAM and Serverless Framework?
SAM is AWS-native with tight CloudFormation integration. Serverless Framework is cloud-agnostic supporting multiple providers. SAM is simpler for AWS-only projects.
Need Help with AWS Serverless Development?
Our experts can help you build and deploy serverless applications using AWS SAM, Lambda, and API Gateway.
