I’ve a spring boot lambda function which process the SQSEvents. This lambda function is deployed in AWS and its working fine, i am trying to execute the lambda in my local using sam local invoke, I have defined the teamplate.yaml and event.json file. When i execute the sam local invoke, the logs shows lambda started and then it shows the error No function defined
. ____ _ __ _ _
/\ / ___'_ __ _ _(_)_ __ __ _
( ( )___ | '_ | '_| | '_ / _` |
\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |___, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.2.RELEASE)
2024-06-03 11:52:03.919 INFO 16 --- [ main] lambdainternal.AWSLambda : Starting AWSLambda on a48c97625309 with PID 16 (/var/runtime/lib/aws-lambda-java-runtime-0.2.0.jar started by root in /var/task)
2024-06-03 11:52:03.921 INFO 16 --- [ main] lambdainternal.AWSLambda : The following profiles are active: default
2024-06-03 11:52:08.151 INFO 16 --- [ main] lambdainternal.AWSLambda : Started AWSLambda in 7.047 seconds (JVM running for 9.109)
Started
No function defined: java.lang.IllegalStateException
java.lang.IllegalStateException: No function defined
at org.springframework.cloud.function.context.AbstractSpringFunctionAdapterInitializer.apply(AbstractSpringFunctionAdapterInitializer.java:183)
at org.springframework.cloud.function.adapter.aws.SpringBootRequestHandler.handleRequest(SpringBootRequestHandler.java:51)
END RequestId: 535d54d9-8d9e-45f7-86fe-fbd3178c54db
Below is my main class :
@SpringBootApplication(scanBasePackages = "com.example.helloworld")
public class HelloLambdaSqsApplication {
public static void main(String[] args) {
SpringApplication.run(HelloLambdaSqsApplication.class, args);
}
}
Below is Handler class :
public class HelloLambdaSqsFunctionHandler extends SpringBootRequestHandler<SQSEvent, String> {
}
Below is lambda function :
@Component("helloLambdaSqsFunction")
public class HelloLambdaSqsFunction implements RequestHandler<SQSEvent, String> {
@Autowired
private UserService userService;
@Override
public String handleRequest(SQSEvent event, Context context) {
event.getRecords().forEach(message -> {
String userId = message.getBody(); // Assuming the message body contains the userId
UserDto user = userService.getUserById(userId);
System.out.println("Username: " + user.getUserName());
});
return "Processed " + event.getRecords().size() + " records.";
}
}
Template.yaml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: |
AWS SAM Template for Hello World Lambda function triggered by SQS.
Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: ../lambda/HelloWorldFunction
Handler: com.example.helloworld.HelloLambdaSqsFunctionHandler
Runtime: java11
MemorySize: 512
Timeout: 30
Environment:
Variables:
SPRING_PROFILES_ACTIVE: default
Events:
SQSEvent:
Type: SQS
Properties:
Queue: !GetAtt HelloWorldQueue.Arn
HelloWorldQueue:
Type: AWS::SQS::Queue
Properties:
QueueName: MyQueue
Outputs:
HelloWorldFunction:
Description: Hello World Lambda Function ARN
Value: !GetAtt HelloWorldFunction.Arn
HelloWorldQueue:
Description: Hello World SQS Queue URL
Value: !Ref HelloWorldQueue
Can someone help me on this. Your help would be appreciable, I urgently need solution on this. Thanks in advance.