Skip to content

Commit 9806eba

Browse files
SinghBSinghB
SinghB
authored and
SinghB
committed
lambda with ssl
1 parent 365374c commit 9806eba

File tree

9 files changed

+77
-16
lines changed

9 files changed

+77
-16
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,4 @@ dmypy.json
127127

128128
# Pyre type checker
129129
.pyre/
130+
/.idea/*

README.md

+23-16
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,48 @@
1-
# AWS Lambda Integration with PostgreSQL and python
1+
## AWS Lambda Integration with PostgreSQL
22

3-
This repository will help in creating the AWS Lambda Function, which will connect to PostgreSQL Db over SSL.
3+
This repository will help in creating the AWS Lambda Function in Python, which will connect to PostgreSQL DB over SSL.
44

5-
# Tech Stack
5+
### Tech Stack
6+
Python 3.6
7+
AWS Lambda
8+
PostgreSQL
9+
Psycopg2(Postgres connector)
10+
SSL
611

712

8-
## Assumption
13+
### Assumption
914
DB is already created with some table and access on AWS cloud.
1015
11-
## Steps Required for Project Setup
16+
### Steps Required for Project Setup
1217
1- Install Python3.6 or 3.8
1318
2- Create a lambda project in python
1419
3- Add psycopg2 dependency in your project root. (Add Image) (Reference)
1520
3- Get SSL certificate from AWS (), add this SSL Certificate to your project directory. (Add Image)
1621
4- Access this certificate in your code and use it in your psycopg2.connect() as sslMode and ssl_ca.(Add Image)
1722
5- Zip it and upload it in AWS lambda (Note: Zip name and lambda name should be same, and lambda_funcation.py should be on root of zip)
1823

19-
## Steps Required for Project Deployment
24+
### Steps Required for Project Deployment
2025
1- Create Lambda furcation (Image) with same name as project zip.
2126
2- Create Lambda a Role, which should have RDSAccess Permission.
2227
3- Lambda should be in same VPC and subnets as PostgreSQL
2328
4- Lambda should have a Security Group, this security group should have permission in PostgreSQL. So there should be a inbound rule in PostgreSQL Security Group for Lambda Security Group
2429

25-
## Step Run
30+
### Step Run
2631

27-
## Steps to upload ZIP file to AWS Lambda
32+
### Steps to upload ZIP file to AWS Lambda
2833

2934

30-
References
35+
# References
3136

32-
SSL Certificate
33-
34-
https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/UsingWithRDS.SSL.html
35-
https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/PostgreSQL.Concepts.General.SSL.html#PostgreSQL.Concepts.General.SSL.Connecting
36-
https://gist.github.com/pfigue/3440e2bc986550a6b8ec
37-
https://help.compose.com/docs/postgresql-and-python
38-
https://github.com/jkehler/awslambda-psycopg2
37+
[SSL](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/UsingWithRDS.SSL.html)
38+
39+
[SSL MODE ](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/PostgreSQL.Concepts.General.SSL.html#PostgreSQL.Concepts.General.SSL.Connecting)
40+
41+
[postgresql-and-python]( https://gist.github.com/pfigue/3440e2bc986550a6b8ec)
42+
43+
[awslambda-psycopg2]( https://gist.github.com/pfigue/3440e2bc986550a6b8ec)
44+
45+
[SSL Issue]( https://gist.github.com/pfigue/3440e2bc986550a6b8ec)
3946

4047

4148

img/psycopg2.PNG

47.1 KB
Loading

img/psycopg2_1.PNG

8.96 KB
Loading

img/ssl_cert.PNG

26.4 KB
Loading

img/ssl_cert_1.PNG

68.2 KB
Loading

lambda_function.py

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import os
2+
import sys
3+
4+
import psycopg2
5+
6+
# We need to access them from AWS SSM parameter store
7+
DB_HOST = 'POSTGRES_HOST'
8+
DB_USER = 'POSTGRES_USER_NAME'
9+
DB_PASSWORD = 'POSTGRES_PASSWORD'
10+
DB_NAME = 'POSTGRES_DB'
11+
DB_PORT = 5432
12+
SSL_MODE = 'verify-full'
13+
SSL_ROOT_CERT = 'cert/xxxx.pem' # Certificate need to be downloaded from AWS for Lambda Region
14+
SSL_ROOT_CERT = os.path.join(os.path.dirname(os.path.realpath(__file__)), SSL_ROOT_CERT)
15+
16+
print("SSL File Location : {}".format(SSL_ROOT_CERT))
17+
18+
19+
def get_connection():
20+
try:
21+
print('[INFO] : AWS Lambda is trying to connect db......!')
22+
return psycopg2.connect(host=DB_HOST, port=DB_PORT, user=DB_NAME, password=DB_PASSWORD,
23+
sslmode=SSL_MODE, # Pass SSL Mode
24+
sslrootcert=SSL_ROOT_CERT, # Pass Root certificate
25+
database=DB_NAME)
26+
except BaseException as err:
27+
print('[ERROR]:: AWS Lambda could not connect to postgres db...! {}'.format(err))
28+
sys.exit()
29+
30+
31+
db_conn = get_connection()
32+
print('[INFO]:: AWS Lambda connected successfully with postgreSQL DB..!')
33+
34+
select_query = "SELECT * FROM FLIGHTS"
35+
36+
37+
def lambda_handler(event, context):
38+
print('[INFO]:: AWS Lambda Handler invoked...!')
39+
cursor = db_conn.cursor()
40+
41+
print("[INFO]:: Execute Select Query cursor.fetchall")
42+
cursor.execute(select_query)
43+
44+
flight_data = cursor.fetchall()
45+
46+
print("[INFO] :: Iterate Row of Flight data..")
47+
for row in flight_data:
48+
print("Id = ", row[0], )
49+
print("Airline Name = ", row[1])
50+
print("Destination = ", row[2], "\n")

psycopg2/__init__.py

Whitespace-only changes.

sslcert/xxxx.pem

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
This need to be download form given url
2+
3+
https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/UsingWithRDS.SSL.html

0 commit comments

Comments
 (0)