How to access RDS from Lambda (python)

Explain in one line

Learn how to access an RDS from a Lambda function written in Python.

Reference page

There is a tutorial in the official AWS documentation, so follow it. However, in the tutorial, the operation is based on CLI, so we will replace it with the operation from the mannequin as appropriate.

-Tutorial: Set up an Lambda function to access Amazon RDS on Amazon VPC

procedure

  1. Create RDS
  2. Create a Lambda deployment package (zip file)
  3. Create an IAM role to grant to Lambda
  4. Create a Lambda function
  5. Test execution

Let's take a closer look step by step.

1. Create RDS

Create an RDS from the AWS mannequin. It will take some time to create. Once you've created it, make sure you can access it locally.

$ mysql -u username -h lambda.xxxxxxxxxxxx.us-east-1.rds.amazonaws.com -p

Security group fix

When you create an RDS, the security group settings default to restricted sources. Therefore, even if you can access it from your local environment, you will not be able to access it from Lambda after that. Therefore, edit the security group of the relevant RDS instance and change the source to any IP (0.0.0.0/0). (Set an appropriate IP according to your requirements.)

2. Create a Lambda deployment package (zip file)

Suppose you are currently working on / path / to / workdir /. Please read / path / to / workdir / as appropriate. Create a Python script similar to the following. The file name is ʻapp.py`.

import sys
import logging
import rds_config
import pymysql
#rds settings
rds_host  = "rds-instance-endpoint"
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)

In ʻapp.py, rds_configandpymysql` are imported, so prepare them.

First, save the following contents as rds_config.py.

#config file containing credentials for rds mysql instance
db_username = "username"
db_password = "password"
db_name = "databasename"

Then use pip to save pymysql to/ path / to / workdir /.

$ pip install pymysql -t /path/to/workdir/

Now that you have the files you need, zip them up. The caveat here is that instead of archiving the entire directory, archive under / path / to / workdir /.

$ zip -r app.zip /path/to/workdir/*

Here, I created a zip file called ʻapp.zip`.

3. Create an IAM role to grant to Lambda

Create a new role on the AWS IAM screen. At that time, select ʻAWS Lambda` as the AWS service role. Here we name it'lambda-role'.

4. Create a Lambda function

Create a Lambda function from your AWS mannequin. This time I chose Blank Function and API Gateway as the trigger. Please select the necessary ones in this area. Other setting items are as follows.

Create a Lambda function with this setting.

5. Test run

Finally, run it manually. You can do it manually by pressing the Test button on the Lambda screen. If you get the following results, you are successful.

"Added 3 items from RDS MySQL table"

Recommended Posts

How to access RDS from Lambda (python)
How to access wikipedia from python
How to use Python lambda
[Lambda] [Python] Post to Twitter from Lambda!
How to access environment variables in Python
How to update Google Sheets from Python
How to access the Datastore from the outside
How to install Python
Access bitcoind from python
Changes from Python 3.0 to Python 3.5
Changes from Python 2 to Python 3.0
How to install python
How to open a web browser from python
Study from Python Hour7: How to use classes
[Python] How to read data from CIFAR-10 and CIFAR-100
How to generate a Python object from JSON
How to handle Linux commands well from Python
Export RDS snapshot to S3 with Lambda (Python)
[AWS / Lambda] How to load Python external library
How to get a value from a parameter store in lambda (using python)
[Python] How to remove duplicate values from the list
[2020.8 latest] How to install Python
How to "cache enabled" access to FPGA memory from Linux
Post from Python to Slack
How to install Python [Windows]
python3: How to use bottle (2)
How to scrape image data from flickr with python
[Python] How to use list 1
How to update Python Tkinter to 8.6
Anaconda updated from 4.2.0 to 4.3.0 (python3.5 updated to python3.6)
Query Athena from Lambda Python
Access Oracle DB from Python
How to use Python argparse
Python: How to use pydub
[Python] How to use checkio
How to download files from Selenium in Python in Chrome
Switch from python2.7 to python3.6 (centos7)
How to run Notepad ++ Python
Connect to sqlite from python
Execute Python function from Powershell (how to pass arguments)
How to change Python version
How to develop in Python
[python] How to judge scalar
[Python] How to use input ()
[Python] How to use virtualenv
[Python] How to call a c function from python (ctypes)
python3: How to use bottle (3)
How to create a kubernetes pod from python code
python3: How to use bottle
How to use Python bytes
ODBC access to SQL Server from Linux with Python
How to slice a block multiple array from a multiple array in Python
How to import Python library set up in EFS to Lambda
How to run a Python program from within a shell script
How to launch AWS Batch from a python client app
How to connect to various DBs from Python (PEP 249) and SQLAlchemy
How to sample from any probability density function in Python
[Python] Regularly export from CloudWatch Logs to S3 with Lambda
Let's use Watson from Python! --How to use Developer Cloud Python SDK
How to call Python or Julia from Ruby (experimental implementation)
Call Matlab from Python to optimize