Lambda, EC2 and RDS are different services on AWS. So, when you start using them on your account, you need to bring them together into one VPC (Virtual Private Cloud) for simplicity. Yes, there are other ways to connect to RDS but this post is limited to focusing on the intricacies with connecting from a simply Java Lambda function to an RDS MySQL database instance.
- Go to Lambda console/dashboard and create a lambda function by uploading the jar or zip file that has the java class which has a test Lambda.
- VPC needs an inbound an outbound rule for Custom TCP, port 3306(MySQL default port) with source(for inbound) and destination(for outbound) selected as the security group in which the Lambda and MySQL are placed in. If they are in different security groups, that would require a separate configuration.