AWS Java Lambda accessing an AWS RDS PostgreSQL Instance with CDK
![AWS Java Lambda accessing an AWS RDS PostgreSQL Instance with CDK AWS Java Lambda accessing an AWS RDS PostgreSQL Instance with CDK](/sites/default/files/AWS-Java-Lambda-accessing-an-AWS-RDS-PostgreSQL-Instance-with-CDK-2.jpg)
Prerequisites:
Set up your AWS RDS PostgreSQL instance (See Creating an AWS RDS PostgreSQL Database with pgAdmin )
This article will follow a similar approach for developing a Java CDK Approach as documented at Creating an AWS Serverless Java 11 Application with CDK (Lambda, S3, DynamoDb, API Gateway)
Project Setup:
Create a Java Lambda to query and update data in this database.
Assumption: You have already setup your AWS Console and Eclipse environment as documented at Getting started with AWS, Java 11 (Amazon Corretto), Eclipse and AWS Toolkit
- Create a Java PostGres Lambda project using CDK
cd c:\projects
mkdir postgresjlambda
cd postgresjlambda
cdk init app --language java
![](http://accolite.wpengine.com/wp-content/uploads/2023/07/AWS-Java-Lambda-accessing-an-AWS-RDS-PostgreSQL-Instance-with-CDK_1-1.png)
- Open the project up in Eclipse
File -> Open Projects from File System
![](http://accolite.wpengine.com/wp-content/uploads/2023/07/AWS-Java-Lambda-accessing-an-AWS-RDS-PostgreSQL-Instance-with-CDK_2.png)
![](http://accolite.wpengine.com/wp-content/uploads/2023/07/AWS-Java-Lambda-accessing-an-AWS-RDS-PostgreSQL-Instance-with-CDK_3.png)
Press Finish
Your Eclipse should now look like the following:
Ensure your Java Build Path is set up correctly with JavaSE-11 and Maven Dependencies selected (Right click the Project Explorer in Eclipse to open the build path):
![](http://accolite.wpengine.com/wp-content/uploads/2023/07/AWS-Java-Lambda-accessing-an-AWS-RDS-PostgreSQL-Instance-with-CDK_4.png)
We will now work to create a Java Lambda to access the Postgres Database we created above.
Create AWS Lambda Handler GetRDSDataLambdaHandler class by right clicking in the Project Explorer New->Class
Refer to the code located at https://github.com/collin-smith/postgresjlambda/ for the following 3 classes or files:
//Postgres RDS Configuration
env.put("DBENDPOINT", "dbendpointforrdspostgres");
env.put("DATABASENAME", "santasworkshop");
env.put("USERNAME", "myusername");
env.put("PASSWORD", "mypassword");
(** Within the PostgresjlambdaStack.java class, ensure that the RDS information is configured properly to your instance in the class above to connect to the RDS PostgreSQL instance. Then endpoint can be retrieved from the AWS console for the RDS instance)
Once the files have been updated you should be able to compile the project by right clicking on your project in the Project Explorer -> Run As -> Maven Build
Type in “clean install” and press run
![](http://accolite.wpengine.com/wp-content/uploads/2023/07/AWS-Java-Lambda-accessing-an-AWS-RDS-PostgreSQL-Instance-with-CDK_5.png)
![](http://accolite.wpengine.com/wp-content/uploads/2023/07/AWS-Java-Lambda-accessing-an-AWS-RDS-PostgreSQL-Instance-with-CDK_0.png)
Once built, you should be able to go to the cmd console and complete the following steps to deploy it to your aws environment:
cd c:\projects\postgresjlambda
cdk bootstrap
![](http://accolite.wpengine.com/wp-content/uploads/2023/07/AWS-Java-Lambda-accessing-an-AWS-RDS-PostgreSQL-Instance-with-CDK_6.png)
cdk synth
![](http://accolite.wpengine.com/wp-content/uploads/2023/07/AWS-Java-Lambda-accessing-an-AWS-RDS-PostgreSQL-Instance-with-CDK_7.png)
cdk deploy
![](http://accolite.wpengine.com/wp-content/uploads/2023/07/AWS-Java-Lambda-accessing-an-AWS-RDS-PostgreSQL-Instance-with-CDK_8.png)
The Lamdba endpoint should now be deployed into your AWS environment and you should have an endpoint you can test with PostMan
![](http://accolite.wpengine.com/wp-content/uploads/2023/07/AWS-Java-Lambda-accessing-an-AWS-RDS-PostgreSQL-Instance-with-CDK_9.png)
That is what it looks like for a successful run of the Lambda. This Lambda reads the records from the table and it also inserts a record into the table.
This can be seen when you run the following SQL command in the pgAdmin client to see that the Lambda has inserted a new record into the Postgres database.
SELECT * FROM LETTERHISTORY
![](http://accolite.wpengine.com/wp-content/uploads/2023/07/AWS-Java-Lambda-accessing-an-AWS-RDS-PostgreSQL-Instance-with-CDK_10.png)
Troubleshooting issues:
Verify in CloudWatch Log Groups if you have any unexpected errors for troubleshooting. I would look at the Log Groups for this Lambda to understand the errors a bit more.
![](http://accolite.wpengine.com/wp-content/uploads/2023/07/AWS-Java-Lambda-accessing-an-AWS-RDS-PostgreSQL-Instance-with-CDK_11.png)
This concludes how to set up a Java Lambda query and make updates to a PostgreSQL RDS instance.
Resource Cleanup:
If you have finished with the PostGres instance, go to the RDS section in the AWS console and delete the data base instance.
Also, to remove the Lambda code, you should run cdk destroy to remove the code deployed by the cdk project.
cdk destroy
![](http://accolite.wpengine.com/wp-content/uploads/2023/07/AWS-Java-Lambda-accessing-an-AWS-RDS-PostgreSQL-Instance-with-CDK_12.png)