Skip to content

  • Home
  • Core Java
    • Write Your Own Immutable Class In Java
    • Write Your Own Singleton Class In java
    • Java Concurrent Package
    • Java Stream Revisited
    • Print odd and even numbers using thread
    • SOLID principles
    • Comparable Vs Comparator
    • Sort HashMap/TreeMap based on value
    • Deep and Shallow Copy in Java with examples
    • Find the frequency of each character in a String
    • How to avoid duplicate Employee objects in HashSet ?
  • Spring
    • Loose Coupling & Dependency Injection
    • Bean Scope
    • Spring Bean Lifecycle
    • IoC Container Vs Application Context Vs Bean Factory
    • @Component Vs @Service @Repository Vs @Controller
    • How to read properties file in Spring
    • Spring AOP (Aspect Oriented Programming)
    • @Component Vs @Bean in Spring
    • Exception Handling in SpringBoot
    • XML configuration Vs Annotations configuration in Spring
    • Spring Data JPA
    • Spring Data REST
  • Spring Security
    • Spring Security with Form, Basic and JWT
    • Security Configuration in Spring Boot Apps
    • Security Protocols & Frameworks
    • Okta OAuth 2.0 SSO with Springboot
    • Spring Boot 2.x Security using Username Password
    • Spring Boot 2.x Security using JWT
    • Spring Boot 3.x Security using Username Password
    • Spring Boot 3.x Security using JWT
  • Microservices
    • Spring Cloud Config (Server & Client)
    • Spring Boot Microservices Tutorial (Part 1 of 2)
    • Spring Boot Microservices Tutorial (Part 2 of 2)
    • Circuit Breaker – Resilience4j
    • The Twelve-Factor App Principles
  • Event Driven Microservices
    • What is Event Driven Microservice ?
    • What is Saga Design Pattern ?
    • What is CQRS Design Pattern ?
  • Spring AI
    • ChatGPT API + SpringBoot Integration
  • Hibernate & JPA
    • JPA vs JDBC
    • CRUD Example Using Spring Boot JPA + H2 + Gradle
    • MongoDB Atlas With Spring Boot Example
    • Transaction Management
    • Relationships in JPA & Hibernate
    • Hibernate First & Second Level Cache
    • Spring Boot Flyway Postgres
  • DevOps
    • What is Devops ?
    • Docker
    • Kubernetes (K8S)
    • Jenkins
    • Infrastructure As Code
  • Functional Programming
    • Functional Programming Vs Structured Programming
    • Java 8 Programs For Interview
    • Predicate, Function, Consumer and Supplier in Java 8
    • Sort a List having Employee objects using Java 8
    • Find Employee from List using Java 8
  • AWS
    • AWS S3
    • AWS EC2
    • EC2 Solutions Architecting
    • How to create an EC2 instance ?
    • How to connect to AWS EC2 instance ?
    • Deploy application to AWS
    • AWS Lambda Java Tutorial
    • Spring Cloud Functions
    • How to Start/Stop AWS ECS automatically using Java Spring?
    • Container Solution in AWS
    • AWS SQS, SNS, MQ and Kinesis
    • Databases in AWS
    • AWS VPC: Peering, Endpoint, VPN, Gateways- Internet, NAT, DX
    • Machine Learning in AWS
    • Storage Solutions in AWS
    • AWS ASG (Auto Scaling Group)
  • AWS Certifications
    • SAA-C03
      • Design Cost-Optimized Architectures
    • AWS Certified Solution Architect-Associate
      • Question 1
  • Kafka
    • Apache Kafka
    • Kafka Producer & Consumer Example Using Spring boot & Conduktor
  • Angular
    • Angular Tutorial – Part 1
    • Angular Tutorial – Part 2
  • Miscellaneous
    • How to add a project to Git/GitHub/GitLab
    • How to Clone a project from Git/GitLab using Git Bash
    • How to query Oracle tables based on some date ?
    • How to highlight text in WordPress ?
    • How to add Page Jumps in WordPress page ?
  • Interview Preparation
    • Core java interview questions
    • Java Threads Interview Questions
  • Contact Me
  • Toggle search form

AWS Lambda Java Tutorial

AWS Serverless Using Java:

AWS Lambda is a serverless feature provided by AWS. Which means there is no server needed to run this application. Well, in reality there is a server which runs in the background, but it does not run the whole time, it runs based on the requests, if a request comes, the application is loaded to the background server and is ran. Then after the response is completed it is offloaded from the server. This process continues every time a request comes.

What this article is for ?

In this article we will create a AWS Lambda function using Java Maven project. We will create that in our local environment and then deploy the same to AWS envrionment.

Business requirements of Lambda:

  • You want to run something the moment an AWS API Gateway is hit with a request.
  • You want to call another service the moment when an object is loaded to an S3 bucket.
  • During weekends you want to shutdown your servers & start the same during weekdays automatically.

The benefit of Lambda is that you pay only for the execution time and not for the idle time of the applications.

Steps to create a AWS Lambda function:
  1. Create a maven java project (not a Springboot project).
  2. Add Lambda dependency
  3. Add Maven Shade plugin (to create a fat jar)
  4. Write code for Lambda function.
  5. clean package it
  6. Deploy to AWS

Below are the detailed steps:

Create a Maven java project.

Add below dependency to pom.xml:

    <dependency>
       <groupId>com.amazonaws</groupId>
       <artifactId>aws-lambda-java-core</artifactId>
       <version>1.2.0</version>
    </dependency>

Add the maven shade plugin in pom.xml like below to make it a fat jar.
What is a fat jar ?
A fat jar = my own source code + dependency 

  <build>
  		<resources>
            <resource>
                <directory>src/main/java/resources</directory>
            </resource>
        </resources>
  	<plugins>
  		    <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.2</version>
                <configuration>
                    <createDependencyReducedPom>false</createDependencyReducedPom>
                    <!-- <shadedArtifactAttached>true</shadedArtifactAttached>
                    <shadedClassifierName>aws</shadedClassifierName> -->
                </configuration>
                <executions>
                	<execution>
                		<phase>package</phase>
                		<goals>
                			<goal>shade</goal>
                		</goals>
                	</execution>
                </executions>
            </plugin>  		
  	</plugins>
  </build> 

Create a Request Handler class as below:

package com.heapsteep;

import com.amazonaws.services.lambda.runtime.RequestHandler;

public class App implements RequestHandler<Object,Object>{
	public Object handleRequest(Object input, Context context) { 	
			System.out.println("==============> input: " + input);//This will be printed in the log and also in CloudWatch log.
			return "*******> Returned value from Heapsteep"; 	
	}
} 

We have implemented the handleRequest() method from RequestHandler interface.

Do Maven Build by calling this command:    clean package
To do the above, right click the project -> Run As -> Maven Build… -> clean package -> Apply -> Run

Once the build is success, you can see the message in the log stating the path where the jars are created.
If you go to the path you can see 2 jars, the jar with more size is the fat jar.

We have to upload this fat jar to the AWS Lambda console.
Log in to your AWS account and follow the below sequence of steps:

AWS Console -> Lambda -> Create function -> Give a name -> Choose any java:

You need to have a IAM Role for the Lambda having “Lambda Full Access” (or whatever access you want to give). I am already having one IAM Role, so I am selecting it:

Choose create function.

Go to “Code” tab -> Upload From -> .zip or .jar file -> choose the fat jar.

Go to “Runtime Settings” section -> Edit
Give the Handler name as below:
com.heapsteep.App::handleRequest

Go to Test tab ->

Test the lambda function by passing anything(as its not using the input parameter). You will see the statements printed in the console. 

That’s it…
You have successfully deployed a Lambda function to AWS.
Source code of the above code: https://github.com/heapsteep/lambda-maven
If you want to see an usage of Lambda you can read the below article:
How to Start/Stop AWS ECS automatically using Java Spring

You didn’t got what you are looking for ? You can hire me for your work at $10/hr.
Do drop a mail to below id with your problem statement
, I will contact you within 1 hr:
hire_developer@heapsteep.com
Working hours: 7:00 am to 5 pm GMT

Copyright © 2025 .

Powered by PressBook Blog WordPress theme