-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
25ee0c0
commit 543a4e5
Showing
4 changed files
with
96 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
FROM python:3.9-slim | ||
WORKDIR /app | ||
COPY . /app | ||
RUN pip install Flask mysql-connector-python | ||
EXPOSE 5000 | ||
RUN pip install pymysql | ||
CMD ["python", "app.py"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,32 @@ | ||
from flask import Flask | ||
import mysql.connector | ||
from mysql.connector import Error | ||
import os | ||
import pymysql | ||
|
||
app = Flask(__name__) | ||
# Retrieve database connection details from environment variables | ||
host = os.getenv('MYSQL_HOST', 'localhost') # Default to 'localhost' if not set | ||
user = os.getenv('MYSQL_USER', 'root') # Default to 'root' if not set | ||
password = os.getenv('MYSQL_PASSWORD', '') # Default to empty string if not set | ||
database = os.getenv('MYSQL_DB', 'test') # Default to 'test' if not set | ||
|
||
# Database connection function | ||
def connect_to_db(): | ||
try: | ||
# Use environment variables for connection details | ||
connection = mysql.connector.connect( | ||
host=os.getenv('MYSQL_HOST', 'localhost'), | ||
user=os.getenv('MYSQL_USER', 'dbuser'), | ||
password=os.getenv('MYSQL_PASSWORD', 'pass123'), | ||
database=os.getenv('MYSQL_DB', 'test_db') | ||
) | ||
connection = None | ||
try: | ||
# Establish a connection to the database | ||
connection = pymysql.connect( | ||
host=host, | ||
user=user, | ||
password=password, | ||
database=database | ||
) | ||
|
||
if connection.is_connected(): | ||
return "Hello, World! Connected to MySQL Database" | ||
except Error as error: | ||
return f"Error connecting to MySQL: {error}" | ||
finally: | ||
if connection.is_connected(): | ||
connection.close() | ||
# If connection is successful | ||
print(f"Connection to MySQL database '{database}' successful!") | ||
|
||
# Define the route | ||
@app.route("/") | ||
def hello_world(): | ||
return connect_to_db() | ||
# You can perform your queries here if needed | ||
|
||
if __name__ == "__main__": | ||
app.run(host="0.0.0.0", port=5000) | ||
except pymysql.MySQLError as e: | ||
# Handle any errors during connection | ||
print(f"Error connecting to MySQL database: {e}") | ||
finally: | ||
# Close the connection if it's open | ||
if connection: | ||
connection.close() | ||
print("Connection closed.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,83 +1,76 @@ | ||
# Create RDS subnet group | ||
resource "aws_db_subnet_group" "RDS_subnet_grp" { | ||
name = var.db_subnet_grp | ||
subnet_ids = aws_subnet.private_db_subnets.*.id | ||
} | ||
|
||
resource "random_password" "db" { | ||
length = 20 | ||
special = false | ||
} | ||
# # Create RDS subnet group | ||
# resource "aws_db_subnet_group" "RDS_subnet_grp" { | ||
# name = var.db_subnet_grp | ||
# subnet_ids = aws_subnet.private_db_subnets.*.id | ||
# } | ||
|
||
resource "aws_ssm_parameter" "db_password" { | ||
name = "db_password" | ||
type = "SecureString" | ||
value = random_password.db.result | ||
} | ||
# Create RDS instance | ||
resource "aws_db_instance" "app_db" { | ||
identifier = var.primary_rds_identifier | ||
availability_zone = var.az[0] | ||
allocated_storage = 10 | ||
engine = "mysql" | ||
engine_version = "8.0.32" | ||
instance_class = var.db_instance_type | ||
storage_type = "gp2" | ||
db_subnet_group_name = aws_db_subnet_group.RDS_subnet_grp.name | ||
vpc_security_group_ids = [aws_security_group.db_server_sg.id] | ||
db_name = var.database_name | ||
username = var.database_user | ||
password = random_password.db.result | ||
skip_final_snapshot = true | ||
backup_retention_period = 7 | ||
# resource "random_password" "db" { | ||
# length = 20 | ||
# special = false | ||
# } | ||
|
||
# CW Logs | ||
enabled_cloudwatch_logs_exports = ["error", "general", "slowquery", "audit"] | ||
# resource "aws_ssm_parameter" "db_password" { | ||
# name = "db_password" | ||
# type = "SecureString" | ||
# value = random_password.db.result | ||
# } | ||
# # Create RDS instance | ||
# resource "aws_db_instance" "app_db" { | ||
# identifier = var.primary_rds_identifier | ||
# availability_zone = var.az[0] | ||
# allocated_storage = 10 | ||
# engine = "mysql" | ||
# engine_version = "8.0.32" | ||
# instance_class = var.db_instance_type | ||
# storage_type = "gp2" | ||
# db_subnet_group_name = aws_db_subnet_group.RDS_subnet_grp.name | ||
# vpc_security_group_ids = [aws_security_group.db_server_sg.id] | ||
# db_name = var.database_name | ||
# username = var.database_user | ||
# password = random_password.db.result | ||
# skip_final_snapshot = true | ||
# backup_retention_period = 7 | ||
# } | ||
|
||
lifecycle { | ||
ignore_changes = [password] | ||
} | ||
} | ||
# # # Create RDS instance replica | ||
# # resource "aws_db_instance" "app_db_replica" { | ||
# # replicate_source_db = var.primary_rds_identifier | ||
# # identifier = var.replica_rds_identifier | ||
# # availability_zone = var.az[1] | ||
# # allocated_storage = 10 | ||
# # engine = "mysql" | ||
# # engine_version = "8.0.32" | ||
# # instance_class = var.db_instance_type | ||
# # storage_type = "gp2" | ||
# # vpc_security_group_ids = [aws_security_group.db_server_sg.id] | ||
# # skip_final_snapshot = true | ||
|
||
# # Create RDS instance replica | ||
# resource "aws_db_instance" "app_db_replica" { | ||
# replicate_source_db = var.primary_rds_identifier | ||
# identifier = var.replica_rds_identifier | ||
# availability_zone = var.az[1] | ||
# allocated_storage = 10 | ||
# engine = "mysql" | ||
# engine_version = "8.0.32" | ||
# instance_class = var.db_instance_type | ||
# storage_type = "gp2" | ||
# vpc_security_group_ids = [aws_security_group.db_server_sg.id] | ||
# skip_final_snapshot = true | ||
# # depends_on = [aws_db_instance.app_db] | ||
# # } | ||
|
||
# depends_on = [aws_db_instance.app_db] | ||
# # Security group for database servers | ||
# resource "aws_security_group" "db_server_sg" { | ||
# name = "db-server-SG" | ||
# description = "Allow inbound SSH traffic for instances in database tier" | ||
# vpc_id = aws_vpc.project_vpc.id | ||
# } | ||
|
||
# Security group for database servers | ||
resource "aws_security_group" "db_server_sg" { | ||
name = "db-server-SG" | ||
description = "Allow inbound SSH traffic for instances in database tier" | ||
vpc_id = aws_vpc.project_vpc.id | ||
} | ||
|
||
# Give application servers access to database servers | ||
resource "aws_security_group_rule" "db_server_mysql_rule" { | ||
security_group_id = aws_security_group.db_server_sg.id | ||
type = "ingress" | ||
from_port = 3306 | ||
to_port = 3306 | ||
protocol = "tcp" | ||
source_security_group_id = module.eks.node_security_group_id | ||
} | ||
# # Give application servers access to database servers | ||
# resource "aws_security_group_rule" "db_server_mysql_rule" { | ||
# security_group_id = aws_security_group.db_server_sg.id | ||
# type = "ingress" | ||
# from_port = 3306 | ||
# to_port = 3306 | ||
# protocol = "tcp" | ||
# source_security_group_id = module.eks.node_security_group_id | ||
# } | ||
|
||
# Allow outbound traffic | ||
resource "aws_security_group_rule" "db_server_outbound_rule" { | ||
security_group_id = aws_security_group.db_server_sg.id | ||
type = "egress" | ||
from_port = 0 | ||
to_port = 0 | ||
protocol = "-1" | ||
cidr_blocks = ["0.0.0.0/0"] | ||
} | ||
# # Allow outbound traffic | ||
# resource "aws_security_group_rule" "db_server_outbound_rule" { | ||
# security_group_id = aws_security_group.db_server_sg.id | ||
# type = "egress" | ||
# from_port = 0 | ||
# to_port = 0 | ||
# protocol = "-1" | ||
# cidr_blocks = ["0.0.0.0/0"] | ||
# } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters