AWS Lambda Managed Instances with Java 25 and AWS SAM - Part 1 Introduction and sample application

Published: (January 15, 2026 at 11:09 AM EST)
4 min read
Source: Dev.to

Source: Dev.to

Introduction

In this article series we’ll introduce the concepts behind AWS Lambda Managed Instances (LMI). After that we’ll develop a sample application that uses LMIs with Java 25 and AWS SAM. In this article we’ll explain the ideas behind LMI and introduce our sample application.

AWS Lambda Managed Instances

Lambda Managed Instances enable us to run Lambda functions on current‑generation Amazon EC2 instances—including Graviton4, network‑optimized instances, and other specialized compute options—without managing instance lifecycles, OS/runtime patching, routing, load balancing, or scaling policies.

With Lambda Managed Instances we benefit from EC2 pricing advantages, such as Savings Plans and Reserved Instances.

For a list of supported instance types, see the AWS Lambda Pricing page and select your AWS Region.

Key capabilities

  • Choose suitable instances – Select appropriate instances based on performance and cost requirements, including the latest CPUs (e.g., Graviton4), configurable memory‑CPU ratios, and high‑bandwidth networking.
  • Automatic provisioning – AWS automatically provisions suitable instances and spins up function execution environments.
  • Dynamic scaling – Instances scale dynamically based on your function traffic patterns.
  • Fully managed experience – AWS handles infrastructure management, scaling, patching, and routing, with the same extensive event‑source integrations you’re familiar with.

When to use Lambda Managed Instances

  • High‑volume, predictable workloads – Ideal for steady‑state workloads without unexpected traffic spikes. LMIs can double traffic within five minutes by default.
  • Performance‑critical applications – Access to the latest CPUs, flexible memory‑CPU ratios, and high network throughput.
  • Regulatory requirements – Granular governance needs with control over VPC and instance placement.

Sample Application

You can find the code example in the GitHub repository aws-lambda-java-25‑lmi.

Architecture

Sample application architecture

The application creates products and retrieves them by ID, using Amazon DynamoDB as the NoSQL persistence layer. It is exposed through Amazon API Gateway, executed by AWS Lambda, and deployed with AWS SAM (Serverless Application Model). A basic understanding of these services, serverless architectures on AWS, and SAM is assumed.

Prerequisites

To build and deploy the sample you need the following installed locally:

Example: GetProductByIdHandler

Below is the source code for the GetProductByIdHandler Lambda function. It extracts the product ID from the API‑Gateway request, looks up the product in DynamoDB, and returns the appropriate response.

@Override
public APIGatewayProxyResponseEvent handleRequest(
        APIGatewayProxyRequestEvent requestEvent,
        Context context) throws JsonProcessingException {

    var id = requestEvent.getPathParameters().get("id");
    var optionalProduct = productDao.getProduct(id);

    if (optionalProduct.isEmpty()) {
        return new APIGatewayProxyResponseEvent()
                .withStatusCode(HttpStatusCode.NOT_FOUND)
                .withBody("Product with id = " + id + " not found");
    }

    return new APIGatewayProxyResponseEvent()
            .withStatusCode(HttpStatusCode.OK)
            .withBody(objectMapper.writeValueAsString(optionalProduct.get()));
}

The handleRequest method receives an APIGatewayProxyRequestEvent (invoked by API Gateway). It retrieves the product ID via requestEvent.getPathParameters().get("id"), queries DynamoDB through productDao.getProduct(id), and then returns a JSON‑serialized response wrapped in an APIGatewayProxyResponseEvent. If the product does not exist, a 404 Not Found response is returned; otherwise a 200 OK response containing the product data is sent back to API Gateway.

Lambda Function Overview

The code of the Lambda function CreateProductHandler, which we use to create and persist products, looks similar to the example below.

Product Entity

The source code of the Product entity is very simple:

public record Product(String id, String name, BigDecimal price) {}

DynamoProductDao Persistence Layer

The implementation of the DynamoProductDao persistence layer uses the AWS SDK for Java 2.0 to write to or read from DynamoDB. Below is the source code of the getProduct method, which we used in the GetProductByIdHandler Lambda function described earlier:

public Optional getProduct(String id) {
    GetItemResponse getItemResponse = dynamoDbClient.getItem(
        GetItemRequest.builder()
            .key(Map.of("PK", AttributeValue.builder().s(id).build()))
            .tableName(PRODUCT_TABLE_NAME)
            .build()
    );

    if (getItemResponse.hasItem()) {
        return Optional.of(ProductMapper.productFromDynamoDB(getItemResponse.item()));
    } else {
        return Optional.empty();
    }
}

Here we:

  1. Use an instance of DynamoDbClient to build a GetItemRequest that queries the DynamoDB table.
  2. Retrieve the table name from an environment variable (System.getenv("PRODUCT_TABLE_NAME")), which we set in the AWS SAM template.
  3. If the product is found, map the DynamoDB item to a Product entity using the custom ProductMapper.

We’ll cover the Infrastructure as Code (IaC) part described in AWS SAM template.yaml in upcoming articles, as it contains the LMI‑specific configuration.

Building and Deploying

  1. Build the application:

    mvn clean package
  2. Deploy it with SAM:

    sam deploy -g

After deployment, SAM returns a customized Amazon API Gateway URL. Use this URL to create products and retrieve them by ID. The API is secured with an API key; include the following HTTP header in each request:

X-API-Key: a6ZbcDefQW12BN56WEV7LMI

(See the MyApiKey definition in template.yaml.)

Example: Create a Product

curl -X PUT \
     -d '{ "id": "1", "name": "Print 10x13", "price": 0.15 }' \
     -H "Content-Type: application/json" \
     -H "X-API-Key: a6ZbcDefQW12BN56WEV7LMI" \
     https://{API_GATEWAY_URL}/prod/products

Example: Retrieve a Product

curl -H "X-API-Key: a6ZbcDefQW12BN56WEV7LMI" \
     https://{API_GATEWAY_URL}/prod/products/1

Conclusion

In this article we explained the concepts behind AWS Lambda Managed Instances and introduced our sample application. In the next article we’ll discuss Lambda Capacity Providers and how to create one.

If you like my content, please follow me on GitHub and give my repositories a star!

Back to Blog

Related posts

Read more »

Rapg: TUI-based Secret Manager

We've all been there. You join a new project, and the first thing you hear is: > 'Check the pinned message in Slack for the .env file.' Or you have several .env...

Technology is an Enabler, not a Saviour

Why clarity of thinking matters more than the tools you use Technology is often treated as a magic switch—flip it on, and everything improves. New software, pl...