1
votes

I am unable to connect to RDS using an Lambda Function via the test example they provide

This is the code:

import sys
import logging
import rds_config
import pymysql
#rds settings
rds_host  = "connection_link"
name = rds_config.db_username
password = rds_config.db_password
db_name = rds_config.db_name


logger = logging.getLogger()
logger.setLevel(logging.INFO)

try:
    conn = pymysql.connect(rds_host, user=name, passwd=password, db=db_name, connect_timeout=5)
except:
    logger.error("ERROR: Unexpected error: Could not connect to MySql instance.")
    sys.exit()

logger.info("SUCCESS: Connection to RDS mysql instance succeeded")

def handler(event, context):
    """
    This function fetches content from mysql RDS instance
    """

    item_count = 0

    with conn.cursor() as cur:
        cur.execute("create table Employee3 ( EmpID  int NOT NULL, Name varchar(255) NOT NULL, PRIMARY KEY (EmpID))")  
        cur.execute('insert into Employee3 (EmpID, Name) values(1, "Joe")')
        cur.execute('insert into Employee3 (EmpID, Name) values(2, "Bob")')
        cur.execute('insert into Employee3 (EmpID, Name) values(3, "Mary")')
        conn.commit()
        cur.execute("select * from Employee3")

        for row in cur:
            item_count += 1
            logger.info(row)
            #print(row)

    return "Added %d items from RDS MySQL table" %(item_count)

This is the structure of my deployment package

app/pymysql/...
app/app.py
app/rds_config.py
app/PyMySQL-0.7.11.dist-info/...

I have packaged all the files inside the app folder in a zip file.

This is the error is get

"errorMessage": "RequestId: 96fb4cd2-79c1-11e7-a2dc-f97407196dbb Process exited before completing request"

I have already checkedmy RDS connection on MYSQL Workbench its working fine

4
What do you mean by "the test example they provide"? Can you provide a link?John Rotenstein
The python example they provide in the documentation link @JohnRotensteinSaransh Agarwal
Either you have a copy/paste error or the def handler(event, context): function has no content. In other words everything after the line def handler(event, context): needs a tab added to it. In Python code whitespace is syntax. You need to have a handle on the basic syntax of the language you are using before attempting to tackle AWS Lambda.Mark B
The code is compiling fine. It is giving the catch error "ERROR: Unexpected error: Could not connect to MySql instance." @MarkBSaransh Agarwal
It's not an error that would prevent the code from compiling, it is an error that would prevent the code from working the way you expect it to. You need fix your indentation to match the example you linked to. Also, for connectivity your Lambda function needs to be in the same VPC as your MySQL RDS instance, and it the RDS security group needs to be open to the security group assigned to your Lambda function.Mark B

4 Answers

0
votes

Update:

Let's assume that your actual Python code is actually indented correctly unlike the code you posted above.

For some reason, your function cannot connect to your database. And instead of returning an error to the user, you basically told it to sys.exit(1) so that's the reason why Lambda says "Process exited before completing the request".


-- Original Answer --

That does not look like an AWS lambda handler.

You're supposed to write a function handler that accepts the lambda event and context as arguments.

Please read more about it from the AWS Lambda documentation.

0
votes

As @MarkB mentioned in the comments, For connectivity you need to set VPC, Subnets and Security Group in your Lambda Function same as your RDS instance: VPC, Subnets and Security group in Lambda function

and also you need to check Protocol, Port and Source for Inbound and Outbound in security group to make sure it is open for your IP and port range. Open Port and IP range for your Security group

0
votes

I just ran into the same problem, and it turns out it is because I did not specify a name for the database on creation. Easy Create doesn't give you this option, so you will have to go with Standard Create, where one can specify the name under Additional Configuration. This name is what you should specify for db_name in rds_config.py.

enter image description here

Alternatively, you can connect with your tool of choice without a database name, perform a CREATE DATABASE xxx; where xxx is the name of the database, and then use can use that database going forward.

Source: https://serverfault.com/a/996423

This one is also relevant: Why don't I have access to the database from aws lambda but have from a local computer with the same login data?

-1
votes

The problem with your zip file.

Im also using the same lambda function.

Please follow the below steps.

1. your app.py and rds_config.py files are good.

2. Then download the pymysql https://pypi.python.org/pypi/PyMySQL

3. Extract it.

4. Copy the pymysql to somewhere. (Note: Dont add all contents inside the PyMySQL-0.7.11 folder, we just need pymysql only.)

5. Then create a zip file with app.py, rds_config.py and pymysql folder.

enter image description here