Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 58ff5a5

Browse files
Merge pull request #2 from MbuguaFrancis/initial-code-commit
Added the lambda code and modified README.md
2 parents 41c4c1a + 3f35044 commit 58ff5a5

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed

‎README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,13 @@
1-
# rds-proxy-python-lambda
1+
### Amazon RDS Proxy Demo Python Code
2+
3+
This demo code is meant to demonstrate the Amazon RDS concept. For more information on RDS proxy, click [here](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/rds-proxy.html) to get started. In case you're not familiar with AWS Lambda, click [here](https://docs.aws.amazon.com/lambda/latest/dg/getting-started.html) to get started.
4+
5+
##### INSTRUCTIONS
6+
7+
The lambda function depends on the following environment variables to be defined.
8+
* Region
9+
* RDS proxy endpoint
10+
* Database Port
11+
* The database username
12+
13+
**N.B** Remember to edit the query in line 52

‎rds-proxy.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import json
2+
import boto3
3+
from os import environ
4+
import pymysql
5+
6+
client = boto3.client('rds') # get the rds object
7+
8+
9+
def create_connection_token():
10+
11+
# get the required parameters to create a token
12+
region = environ.get('region') # get the region
13+
hostname = environ.get('rds_endpoint') # get the rds proxy endpoint
14+
port = environ.get('port') # get the databse port
15+
username = environ.get('username') # get the database username
16+
17+
# generate the authentication token -- temporary password
18+
token = client.generate_db_auth_token(
19+
DBHostname=hostname,
20+
Port=port,
21+
DBUsername=username,
22+
Region=region
23+
)
24+
25+
return token
26+
27+
28+
def db_ops():
29+
# get the temporary password
30+
token = create_connection_token()
31+
try:
32+
# create a connection object
33+
connection = pymysql.connect(
34+
host=environ.get('your rds proxy endpoint'), # getting the rds proxy endpoint from the environment variables
35+
user=environ.get('your database user'), # get the database user from the environment variables
36+
password=token,
37+
db=environ.get('your database'),
38+
charset='utf8mb4',
39+
cursorclass=pymysql.cursors.DictCursor,
40+
ssl={"use": True }
41+
)
42+
except pymysql.MySQLError as e:
43+
return e
44+
45+
return connection
46+
47+
48+
def lambda_handler(event, context):
49+
# TODO implement
50+
conn = db_ops()
51+
cursor = conn.cursor()
52+
query = "SELECT count(*) FROM customer_entity"
53+
cursor.execute(query)
54+
results = cursor.fetchmany(4)
55+
56+
return {
57+
'statusCode': 200,
58+
'body': json.dumps(results)
59+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /