How to debug AWS lambda locally (docker and IntelliJ IDEA)
Description
Setup Docker: Install Docker on your local machine to create a containerized environment for running AWS Lambda functions.
Docker Image for Lambda Runtime: Pull or build a Docker image that mimics the Lambda runtime environment, ensuring compatibility with your Lambda function's runtime (e.g., Node.js, Python, Java).
Local Testing with IntelliJ IDEA: Configure your IntelliJ IDEA project to use the Docker image, allowing you to simulate the Lambda environment locally. Set up breakpoints and debug configurations within IntelliJ for seamless debugging.
Invoke Lambda Function Locally: Use the AWS SAM CLI (Serverless Application Model Command Line Interface) or a similar tool to locally invoke your Lambda function within the Docker container, enabling you to test and debug the function's behavior.
Inspect Logs and Output: Examine logs and output generated during the local invocation to identify and troubleshoot issues. Leverage IntelliJ's debugging tools to step through code and pinpoint the source of errors.
Iterative Development: Make code changes based on debugging insights, and repeat the process iteratively until your Lambda function behaves as expected in the local environment. Once satisfied, deploy the updated function to AWS Lambda.
Pre-Requisite:-
AWS SAM CLI ****— Serverless Application Model
Install IntelliJ IDE (or eclipse)
Docker installed on the system
AWS Tool Kit, Docker in IntelliJ IDE
Setting > Plugin> AWS toolkit, Docker
Go through this image
Steps involved in debugging AWS Lambda Locally in IntelliJ
A**. Create AWS SAM Project for Lamda**
Open Windows command prompt
command: “sam init”
A project will be created : “Sam_Project”
B. Import the project in IntelliJ IDEA
C. Setup the SDK according to sam project configuration (here java 11)
File > Project Structure > Project > SDK
Hit apply then OK
[Note: If your SDK is not available, then download it from
Project structure > SDKs > add new SDK (+) > download SDK]
D. Create a new Module (if not created)
File > project structure > Modules > add new Module ( + )
-> give it a name
-> location of the project
-> Create
E. Edit Configuration
Edit Config > Add new ( + ) > Local
Apply > OK
F. Now set any end Point (For debugging) and press Debug Button
Note: Docker should be running