Select your cookie preferences

We use cookies and similar tools to enhance your experience, provide our services, deliver relevant advertising, and make improvements. Approved third parties also use these tools to help us deliver advertising and provide certain site features.

Effectively running Java on Serverless AWS SAM 2 min
Sonali Jena
Sonali Jena Territory SA, Partners

The AWS Serverless Application Model (AWS SAM) is a toolkit that improves the developer experience of building and running serverless applications on AWS. AWS SAM consists of two primary parts:

  • AWS SAM template specification – An open-source framework that you can use to define your serverless application infrastructure on AWS.
  • AWS SAM command line interface (AWS SAM CLI) – A command line tool that you can use with AWS SAM templates and supported third-party integrations to build and run your serverless applications.

SAM Transformation

AWS SAM does the complex work of transforming your template into the code necessary to provision your infrastructure through AWS CloudFormation.

This is an example of a basic serverless application using SAM.

SAMApp

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31

Resources:
  GetProductsFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: com.example.GetProductsFunction::handleRequest
      Runtime: java17
      Architectures:
        - arm64
      MemorySize: 1024
      Policies:
        - DynamoDBReadPolicy:
            TableName: !Ref ProductTable
      Events:
        GetProductsEvent:
          Type: Api
          Properties:
            Path: /products
            Method: get

  ProductTable:
    Type: AWS::Serverless::SimpleTable

The following diagram provides an overview of the transformation with SAM.

SAMTransformation

  1. Define your infrastructure resources such as Lambda functions, Amazon DynamoDB Tables, Amazon S3 buckets, or an Amazon API Gateway endpoint in a template.yaml file.
  2. The CLI command sam build builds the application based on the chosen runtime and configuration within the template.
  3. The sam build command populates the .aws-sam/build folder with the built artifacts (for example, class files or jars).
  4. The sam deploy command uploads your template and function code from the .aws-sam/build folder and starts an AWS CloudFormation deployment.
  5. After a successful deployment, you can find the provisioned resources in your AWS account.

Follow this blog to learn more about Building serverless Java application with the AWS SAM CLI.