Skip to content

Commit 8e3078b

Browse files
authored
Updated DB used by the sample app from mysql to postgres (#32)
Updated the DB used by the sample app from mysql to postgres. Tested locally using docker and on EKS by running the one touch script. Was able to connect to DB and do the basic CRUD operations through vehicle-service APIs. By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
1 parent 0934954 commit 8e3078b

File tree

14 files changed

+91
-79
lines changed

14 files changed

+91
-79
lines changed

sample-applications/vehicle-dealership-sample-app/ImageServiceApp/ImageServiceApp/settings.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
import os
1616
from pathlib import Path
1717

18+
from dotenv import load_dotenv
19+
20+
load_dotenv()
21+
1822
# Build paths inside the project like this: BASE_DIR / 'subdir'.
1923
BASE_DIR = Path(__file__).resolve().parent.parent
2024

@@ -78,8 +82,12 @@
7882

7983
DATABASES = {
8084
"default": {
81-
"ENGINE": "django.db.backends.sqlite3",
82-
"NAME": BASE_DIR / "db.sqlite3",
85+
"ENGINE": "django.db.backends.postgresql",
86+
"USER": os.environ.get("POSTGRES_USER"),
87+
"NAME": os.environ.get("POSTGRES_DATABASE"),
88+
"PASSWORD": os.environ.get("POSTGRES_PASSWORD"),
89+
"HOST": os.environ.get("DB_SERVICE_HOST"),
90+
"PORT": os.environ.get("DB_SERVICE_PORT"),
8391
}
8492
}
8593

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
Django~=5.0
22
requests~=2.31.0
33
boto3~=1.34.14
4-
python-dotenv~=1.0.0
4+
python-dotenv~=1.0.0
5+
psycopg2~=2.9.9

sample-applications/vehicle-dealership-sample-app/README.md

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ This directory contains code for a microservices based sample app that is used t
2323

2424
### EKS
2525
To get started with AWS EKS, you can run the one touch script as below.
26-
`bash script.sh <account_id> <cluster_name> <region> <mysql_password> <s3_bucket_name>`
26+
`bash script.sh <account_id> <cluster_name> <region> <postgres_password> <s3_bucket_name>`
2727

2828
This will create the docker images, upload them to ECR and then create pods on EKS with those images.
2929

@@ -52,7 +52,7 @@ To deploy to EC2, you will have to go through the following steps.
5252
- AmazonS3FullAccess
5353
- AmazonSQSFullAccess
5454
- Name one vehicle-service and the other image-service
55-
5. Go to RDS and create a MySQL DB with the following configurations:
55+
5. Go to RDS and create a Postgres DB with the following configurations:
5656
- Use the Dev/Test template
5757
- Update the Master username to `root` and create a password of your choosing. Write it down since you will need it later.
5858
- In the Connectivity settings, choose the VPC and security group created in step 3.
@@ -61,18 +61,15 @@ To deploy to EC2, you will have to go through the following steps.
6161
```
6262
sudo dnf install python3.11
6363
sudo dnf install python3.11-pip
64-
sudo dnf install mariadb105
65-
sudo dnf install -y mariadb105-devel gcc python3.11-devel
64+
sudo dnf install postgresql15
6665
67-
mysql -h <RDS_DB_Endpoint> -P 3306 -u root -p<password_from_step_5>
66+
createdb vehicle_inventory -h <rds_url> -U root
67+
createuser djangouser -h <rds_url> -U root
6868
69-
CREATE DATABASE vehicle_inventory;
69+
psql -h <rds_url> -d vehicle_inventory -U root
7070
71-
CREATE USER 'djangouser'@'%' IDENTIFIED BY '<password_of_your_choosing>';
72-
73-
GRANT ALL PRIVILEGES ON vehicle_inventory.* TO 'djangouser'@'%' WITH GRANT OPTION;
74-
75-
FLUSH PRIVILEGES;
71+
alter user djangouser with encrypted password '<password_of_your_choosing>';
72+
grant all privileges on database vehicle_inventory to djangouser;
7673
7774
aws s3 sync s3://<s3_bucket_that_has_python_code> .
7875
@@ -81,18 +78,19 @@ cd to the vehicle microservice directory and run:
8178
python3.11 -m pip install -r requirements.txt
8279
8380
Create a .env file with the following:
84-
MYSQL_ROOT_PASSWORD=<password_from_RDS_setup>
85-
MYSQL_DATABASE=vehicle_inventory
86-
MYSQL_USER=djangouser
87-
MYSQL_PASSWORD=<password_from_this_step>
81+
POSTGRES_ROOT_PASSWORD=<password_from_RDS_setup>
82+
POSTGRES_DATABASE=vehicle_inventory
83+
POSTGRES_USER=djangouser
84+
POSTGRES_PASSWORD=<password_from_this_step>
8885
DB_SERVICE_HOST=<RDS_DB_endpoint>
8986
DB_SERVICE_PORT=3306
9087
IMAGE_BACKEND_SERVICE_HOST=<image-service_ec2_public_IP>
9188
IMAGE_BACKEND_SERVICE_PORT=8000
9289
9390
python3.11 manage.py migrate --noinput && python3.11 manage.py runserver 0.0.0.0:8001
9491
```
95-
7. Connect to the `image-service` EC2 instance and run the following:
92+
7. Go to the EC2 console and select the `image-service`, Go Actions -> Networking -> Connect RDS database.
93+
8. Connect to the `image-service` EC2 instance and run the following:
9694
```
9795
sudo dnf install python3.11
9896
sudo dnf install python3.11-pip
@@ -113,18 +111,18 @@ Now you should be able to access the APIs below through the EC2 addr:port of eac
113111

114112
### Locally with Docker
115113
To get started, make sure you either have you AWS creds in `$HOME/.aws` or the following: `AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_SESSION_TOKEN` are exported.
116-
1. Run `bash local_script.sh <mysql_pass> <s3_bucket_name>`.
114+
1. Run `bash local_script.sh <postgres_pass> <s3_bucket_name>`.
117115
This will create docker containers, move the requirement env variables there and start them up.
118116

119117
They should be accessible through `0.0.0.0:8000` for the image service and `0.0.0.0:8001` for the vehicle service.
120118

121119
## APIs
122120

123121
The following are the APIs and what they do:
124-
1. GET /vehicle-inventory/: returns all the vehicles entries for mysql db
125-
2. PUT /vehicle-inventory/: puts vehicle into db. For example: `curl -X POST http://0.0.0.0:8001/vehicle-inventory/ -d '{"make": "BMW","model": "M340","year": 2022,"image_name": "newCar.jpg"}'`
122+
1. GET /vehicle-inventory/: returns all the vehicles entries for postgres db
123+
2. POST /vehicle-inventory/: puts vehicle into db. For example: `curl -X POST http://0.0.0.0:8001/vehicle-inventory/ -d '{"make": "BMW","model": "M340","year": 2022,"image_name": "newCar.jpg"}'`
126124
3. GET /vehicle-inventory/<int>: returns vehicle entry with id = <int>
127125
4. GET /vehicle-inventory/<int>/image: returns image file information from S3 for the specific vehicle by calling the image microservice
128126
5. GET /images/name/<image_name>: returns image information for <image_name> from S3 if present.
129-
6. PUT /images/name/<image_name>: creates an empty file in S3. This is an async endpoint since it will put image name in an SQS queue and not wait for the file to be created in S3. Instead, a long running thread will poll SQS and then create the image file later.
127+
6. POST /images/name/<image_name>: creates an empty file in S3. This is an async endpoint since it will put image name in an SQS queue and not wait for the file to be created in S3. Instead, a long running thread will poll SQS and then create the image file later.
130128
7. GET /image/remote-image: makes a remote http call to google.com.

sample-applications/vehicle-dealership-sample-app/VehicleInventoryApp/MainService/migrations/__init__.py

Whitespace-only changes.

sample-applications/vehicle-dealership-sample-app/VehicleInventoryApp/VehicleInventoryApp/settings.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,10 @@
8181

8282
DATABASES = {
8383
"default": {
84-
"ENGINE": "django.db.backends.mysql",
85-
"USER": os.environ.get("MYSQL_USER"),
86-
"NAME": os.environ.get("MYSQL_DATABASE"),
87-
"PASSWORD": os.environ.get("MYSQL_PASSWORD"),
84+
"ENGINE": "django.db.backends.postgresql",
85+
"USER": os.environ.get("POSTGRES_USER"),
86+
"NAME": os.environ.get("POSTGRES_DATABASE"),
87+
"PASSWORD": os.environ.get("POSTGRES_PASSWORD"),
8888
"HOST": os.environ.get("DB_SERVICE_HOST"),
8989
"PORT": os.environ.get("DB_SERVICE_PORT"),
9090
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
Django~=5.0
22
requests~=2.31.0
3-
mysqlclient~=2.2.1
3+
psycopg2~=2.9.9
44
python-dotenv~=1.0.0

sample-applications/vehicle-dealership-sample-app/docker-compose.yaml

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,24 @@
33
version: '3'
44
services:
55
db:
6-
image: mysql:8.0
6+
image: postgres:14.0
77
container_name: vehicle_inventory_db
88
restart: always
99
volumes:
10-
- data:/var/lib/mysql
10+
- data:/var/lib/postgres
1111
environment:
12-
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
13-
MYSQL_DATABASE: ${MYSQL_DATABASE}
14-
MYSQL_USER: ${MYSQL_USER}
15-
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
12+
POSTGRES_DB: ${POSTGRES_DATABASE}
13+
POSTGRES_USER: ${POSTGRES_USER}
14+
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
15+
PGUSER: ${POSTGRES_USER}
1616
ports:
17-
- "3306:3306"
17+
- "5432:5432"
1818
healthcheck:
19-
test: ["CMD", "mysql", "-h", "localhost", "-u", "root", "-p${MYSQL_PASSWORD}", "-e", "SELECT 1"]
19+
test: ["CMD", "pg_isready", "-d", "vehicle_inventory", "-U", "djangouser"]
2020
timeout: 20s
2121
retries: 10
2222
expose:
23-
- 3306
23+
- 5432
2424

2525
vehicle-inventory-backend:
2626
image: pythonsampleapp/vehicle-inventory-service
@@ -44,13 +44,17 @@ services:
4444
context: .
4545
dockerfile: ImageServiceApp/Dockerfile
4646
container_name: image-service-backend
47-
command: sh -c "python3 manage.py migrate --noinput && python3 manage.py collectstatic --noinput && python3 manage.py runserver 0.0.0.0:8000"
47+
command: sh -c "python3 manage.py runserver 0.0.0.0:8000"
4848
restart: always
4949
volumes:
5050
- ./ImageServiceApp/.:/image-service-app
5151
- $HOME/.aws/credentials:/home/app/.aws/credentials:ro
5252
ports:
5353
- "8000:8000"
54+
depends_on:
55+
db:
56+
condition: service_healthy
57+
5458
volumes:
5559
data:
5660

sample-applications/vehicle-dealership-sample-app/eks/backend-deployment.yaml

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,11 @@ spec:
3535
image: ${REPOSITORY_PREFIX}/pythonsampleapp/vehicle-inventory-service:latest
3636
name: vehicle-inventory-backend
3737
env:
38-
- name: MYSQL_DATABASE
38+
- name: POSTGRES_DATABASE
3939
value: vehicle_inventory
40-
- name: MYSQL_PASSWORD
41-
value: ${MYSQL_PASSWORD}
42-
- name: MYSQL_ROOT_PASSWORD
43-
value: ${MYSQL_PASSWORD}
44-
- name: MYSQL_USER
40+
- name: POSTGRES_PASSWORD
41+
value: ${POSTGRES_PASSWORD}
42+
- name: POSTGRES_USER
4543
value: djangouser
4644
imagePullPolicy: Always
4745
ports:

sample-applications/vehicle-dealership-sample-app/eks/db-deployment.yaml

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,36 +29,31 @@ spec:
2929
spec:
3030
containers:
3131
- env:
32-
- name: MYSQL_DATABASE
32+
- name: POSTGRES_DB
3333
value: vehicle_inventory
34-
- name: MYSQL_PASSWORD
35-
value: ${MYSQL_PASSWORD}
36-
- name: MYSQL_ROOT_PASSWORD
37-
value: ${MYSQL_PASSWORD}
38-
- name: MYSQL_USER
34+
- name: POSTGRES_PASSWORD
35+
value: ${POSTGRES_PASSWORD}
36+
- name: POSTGRES_USER
3937
value: djangouser
40-
image: mysql:8.0
38+
image: postgres:14.0
4139
livenessProbe:
4240
exec:
4341
command:
44-
- mysql
45-
- -h
46-
- localhost
47-
- -u
48-
- root
49-
- -p${MYSQL_PASSWORD}
50-
- -e
51-
- SELECT 1
42+
- pg_isready
43+
- -d
44+
- vehicle_inventory
45+
- -U
46+
- djangouser
5247
failureThreshold: 10
5348
timeoutSeconds: 20
5449
name: vehicle-inventory-db
5550
ports:
56-
- containerPort: 3306
57-
hostPort: 3306
51+
- containerPort: 5432
52+
hostPort: 5432
5853
protocol: TCP
5954
resources: {}
6055
volumeMounts:
61-
- mountPath: /var/lib/mysql
56+
- mountPath: /var/lib/postgres
6257
name: data
6358
restartPolicy: Always
6459
volumes:

sample-applications/vehicle-dealership-sample-app/eks/db-service.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ metadata:
1212
name: db
1313
spec:
1414
ports:
15-
- port: 3306
15+
- port: 5432
1616
selector:
1717
io.kompose.service: db
1818
status:

sample-applications/vehicle-dealership-sample-app/eks/image-backend-deployment.yaml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ spec:
3131
- args:
3232
- sh
3333
- -c
34-
- python3 manage.py migrate --noinput; python3 manage.py collectstatic --noinput; python3 manage.py runserver 0.0.0.0:8000
34+
- python3 manage.py runserver 0.0.0.0:8000
3535
image: ${REPOSITORY_PREFIX}/pythonsampleapp/image-service:latest
3636
name: image-service-backend
3737
imagePullPolicy: Always
@@ -43,5 +43,11 @@ spec:
4343
env:
4444
- name: S3_BUCKET
4545
value: ${S3_BUCKET}
46+
- name: POSTGRES_DATABASE
47+
value: vehicle_inventory
48+
- name: POSTGRES_PASSWORD
49+
value: ${POSTGRES_PASSWORD}
50+
- name: POSTGRES_USER
51+
value: djangouser
4652
restartPolicy: Always
4753
status: {}

sample-applications/vehicle-dealership-sample-app/local_script.sh

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,26 @@ rm VehicleInventoryApp/.env
2323
rm ImageServiceApp/.env
2424
rm .env
2525

26-
echo "MYSQL_ROOT_PASSWORD=${password}" >> VehicleInventoryApp/.env
27-
echo "MYSQL_DATABASE=vehicle_inventory" >> VehicleInventoryApp/.env
28-
echo "MYSQL_USER=djangouser" >> VehicleInventoryApp/.env
29-
echo "MYSQL_PASSWORD=${password}" >> VehicleInventoryApp/.env
26+
echo "POSTGRES_DATABASE=vehicle_inventory" >> VehicleInventoryApp/.env
27+
echo "POSTGRES_USER=djangouser" >> VehicleInventoryApp/.env
28+
echo "POSTGRES_PASSWORD=${password}" >> VehicleInventoryApp/.env
3029
echo "DB_SERVICE_HOST=db" >> VehicleInventoryApp/.env
31-
echo "DB_SERVICE_PORT=3306" >> VehicleInventoryApp/.env
30+
echo "DB_SERVICE_PORT=5432" >> VehicleInventoryApp/.env
3231
echo "IMAGE_BACKEND_SERVICE_HOST=image-service-backend" >> VehicleInventoryApp/.env
3332
echo "IMAGE_BACKEND_SERVICE_PORT=8000" >> VehicleInventoryApp/.env
3433

34+
echo "POSTGRES_DATABASE=vehicle_inventory" >> ImageServiceApp/.env
35+
echo "POSTGRES_USER=djangouser" >> ImageServiceApp/.env
36+
echo "POSTGRES_PASSWORD=${password}" >> ImageServiceApp/.env
37+
echo "DB_SERVICE_HOST=db" >> ImageServiceApp/.env
38+
echo "DB_SERVICE_PORT=5432" >> ImageServiceApp/.env
3539
echo "AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID}" >> ImageServiceApp/.env
3640
echo "AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY}" >> ImageServiceApp/.env
3741
echo "AWS_SESSION_TOKEN=${AWS_SESSION_TOKEN}" >> ImageServiceApp/.env
3842
echo "S3_BUCKET=${s3_bucket}" >> ImageServiceApp/.env
3943

40-
echo "MYSQL_ROOT_PASSWORD=${password}" >> .env
41-
echo "MYSQL_DATABASE=vehicle_inventory" >> .env
42-
echo "MYSQL_USER=djangouser" >> .env
43-
echo "MYSQL_PASSWORD=${password}" >> .env
44+
echo "POSTGRES_DATABASE=vehicle_inventory" >> .env
45+
echo "POSTGRES_USER=djangouser" >> .env
46+
echo "POSTGRES_PASSWORD=${password}" >> .env
4447

4548
docker-compose up --build

sample-applications/vehicle-dealership-sample-app/script.sh

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@ rm ImageServiceApp/.env
1414
touch ImageServiceApp/.env
1515

1616
export REPOSITORY_PREFIX=${account}.dkr.ecr.$region.amazonaws.com
17-
export MYSQL_ROOT_PASSWORD=${password}
18-
export MYSQL_DATABASE=vehicle_inventory
19-
export MYSQL_USER=djangouser
20-
export MYSQL_PASSWORD=${password}
17+
export POSTGRES_DATABASE=vehicle_inventory
18+
export POSTGRES_USER=djangouser
19+
export POSTGRES_PASSWORD=${password}
2120
export S3_BUCKET=${s3_bucket}
2221

2322
docker-compose build

sample-applications/vehicle-dealership-sample-app/scripts/deploy-eks.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ then
1414
else
1515
for config in $(ls ./eks/*.yaml)
1616
do
17-
sed -e 's#\${REPOSITORY_PREFIX}'"#${REPOSITORY_PREFIX}#g" -e 's#\${MYSQL_PASSWORD}'"#${MYSQL_PASSWORD}#g" -e 's#\${S3_BUCKET}'"#${S3_BUCKET}#g" ${config} | kubectl ${OPERATION} -f -
17+
sed -e 's#\${REPOSITORY_PREFIX}'"#${REPOSITORY_PREFIX}#g" -e 's#\${POSTGRES_PASSWORD}'"#${POSTGRES_PASSWORD}#g" -e 's#\${S3_BUCKET}'"#${S3_BUCKET}#g" ${config} | kubectl ${OPERATION} -f -
1818
done
1919

2020
for config in $(ls ./eks/k8s-nginx-ingress/*.yaml)
2121
do
22-
sed -e 's#\${REPOSITORY_PREFIX}'"#${REPOSITORY_PREFIX}#g" -e 's#\${MYSQL_PASSWORD}'"#${MYSQL_PASSWORD}#g" -e 's#\${S3_BUCKET}'"#${S3_BUCKET}#g" ${config} | kubectl ${OPERATION} -f -
22+
sed -e 's#\${REPOSITORY_PREFIX}'"#${REPOSITORY_PREFIX}#g" -e 's#\${POSTGRES_PASSWORD}'"#${POSTGRES_PASSWORD}#g" -e 's#\${S3_BUCKET}'"#${S3_BUCKET}#g" ${config} | kubectl ${OPERATION} -f -
2323
done
2424
fi

0 commit comments

Comments
 (0)