MySQL Connection Timing out on AWS Lambda?

Question: I can connect and insert/select from the database just fine when running locally with JETS_ENV_REMOTE=1.

$ JETS_ENV_REMOTE=1 jets console
>> ActiveRecord::Base.connection.tables
=> ["schema_migrations", "ar_internal_metadata", "posts"]    

But when the code runs on Lambda, the function is timing out every time when it gets to the ActiveRecord call.

This has likely to do with your security group settings.

To check the security group with an RDS.

  1. Go to the RDS console and on the specific RDS instance find it’s security group. You’ll be able to click on the security group

  1. Once you get to the security group. Look at its inbound rules

For the test db, you can see that it’s configured so that port 5432 is opened to 0.0.0.0/0 - which means the world. The DB is still protected by the username and password of course. For apps that really require higher levels of network security, some companies like to lock it down within the VPC instead. There are trade-offs, particularly with Lambda.

For applications, with higher security requirements, you can also use DBs within VPCs with a more complex setup. Essentially, you have to use a vpc_config setting https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-lambda-function-vpcconfig.html This is also supported via Jets Function Properties http://rubyonjets.com/docs/function-properties/