Skip to content

Commit

Permalink
deploy rds
Browse files Browse the repository at this point in the history
  • Loading branch information
yordanovsstoyan committed Nov 11, 2024
1 parent 25ee0c0 commit 543a4e5
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 119 deletions.
3 changes: 1 addition & 2 deletions app/Dockerfile
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"]
53 changes: 26 additions & 27 deletions app/app.py
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.")
143 changes: 68 additions & 75 deletions rds.tf
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"]
# }
16 changes: 1 addition & 15 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ variable "db_engine_version" {
# RDS instance name
variable "database_name" {
type = string
default = "db_subnet_grp"
default = "alex"
description = "name of RDS instance"
}

Expand All @@ -103,20 +103,6 @@ variable "database_user" {
default = "dbuser"
}

# RDS instance password
variable "database_password" {
type = string
sensitive = false
default = "password123"
}

# ID of EC2 instance AMI
variable "ami" {
type = string
default = "ami-0ea3c35c5c3284d82"
description = "ID of Instance AMI"
}

# Type of instance
variable "instance_type" {
type = string
Expand Down

0 comments on commit 543a4e5

Please sign in to comment.