Skip to content

Commit

Permalink
update load test script
Browse files Browse the repository at this point in the history
  • Loading branch information
tinhtq committed Nov 25, 2024
1 parent 557225c commit 6bdcf3a
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 21 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ test.txt
python.zip
backend.tf
.DS_Store
.env
1 change: 1 addition & 0 deletions rds/promote-read-replicas/data.tf
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ data "aws_subnets" "default_subnets" {
values = [data.aws_vpc.default.id]
}
}

49 changes: 49 additions & 0 deletions rds/promote-read-replicas/load-test/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import sql from "k6/x/sql";
import { check } from "k6";
import { loadEnv } from "k6/x/dotenv";
import driver from "k6/x/sql/driver/postgres";

export const options = {
vus: 10, // Number of Virtual Users
duration: "30s", // Total test duration
};

// Load environment variables
const env = loadEnv(".env");

// Replace with your PostgreSQL connection string
const db = sql.open(
driver,
`postgres://${env.DB_USER}:${env.DB_PASSWORD}@${env.DB_HOST}:${env.DB_PORT}/${env.DB_NAME}`
);

export function setup() {
db.exec(`CREATE TABLE IF NOT EXISTS person (
id SERIAL PRIMARY KEY,
email VARCHAR NOT NULL,
first_name VARCHAR,
last_name VARCHAR);`);

db.exec(
"INSERT INTO person (email, first_name, last_name) VALUES('[email protected]', 'John', 'Doe');"
);
db.exec(
"INSERT INTO person (email, first_name, last_name) VALUES('[email protected]', 'Mary', 'Sue');"
);
db.exec(
"INSERT INTO person (email, first_name, last_name) VALUES('[email protected]', 'Dory', 'Doe');"
);
}

export function teardown() {
db.exec("DELETE FROM person;");
db.exec("DROP TABLE person;");
db.close();
}

export default function () {
const results = sql.query(db, "SELECT * FROM person;");
check(results, {
"is length 3": (r) => r.length === 3,
});
}
40 changes: 20 additions & 20 deletions rds/promote-read-replicas/main.tf
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
resource "aws_db_subnet_group" "all" {
name = "main"
subnet_ids = data.aws_subnets.default_subnets.ids
tags = {
Name = "DB subnet group"
}
}

resource "aws_rds_cluster" "primary" {
cluster_identifier = var.rds_cluster_name
engine = "aurora-mysql"
availability_zones = var.availability_zones
backup_retention_period = 7
preferred_backup_window = "07:00-09:00"
manage_master_user_password = true
master_username = "admin"

cluster_identifier = var.rds_cluster_name
engine = "aurora-postgresql"
availability_zones = var.availability_zones
backup_retention_period = 7
preferred_backup_window = "07:00-09:00"
manage_master_user_password = true
master_username = "admin"
skip_final_snapshot = var.skip_final_snapshot
db_subnet_group_name = aws_db_subnet_group.all.id
enabled_cloudwatch_logs_exports = ["error", "general", "slowquery"]
database_name = var.db_name
vpc_security_group_ids = [aws_security_group.rds_postgresql_sg.id]
}

resource "aws_rds_cluster_instance" "primary_instance" {
cluster_identifier = aws_rds_cluster.primary.id
instance_class = var.db_type
engine = aws_rds_cluster.primary.engine

cluster_identifier = aws_rds_cluster.primary.id
instance_class = var.db_type
engine = aws_rds_cluster.primary.engine
publicly_accessible = var.publicly_accessible
}

resource "aws_rds_cluster_instance" "read_replica" {
cluster_identifier = aws_rds_cluster.primary.id
instance_class = var.db_type
engine = aws_rds_cluster.primary.engine
cluster_identifier = aws_rds_cluster.primary.id
instance_class = var.db_type
engine = aws_rds_cluster.primary.engine
publicly_accessible = var.publicly_accessible
}

resource "aws_iam_role_policy_attachment" "lambda_attach_policy" {
Expand All @@ -45,8 +45,8 @@ resource "aws_lambda_function" "promote_read_replica" {

environment {
variables = {
DB_INSTANCE_ID = aws_rds_cluster.primary.id
# SNS_TOPIC_ARN = aws_sns_topic.notify.arn
DB_INSTANCE_ID = aws_rds_cluster.primary.id
SNS_TOPIC_ARN = aws_sns_topic.notify.arn
SECRET_NAME = aws_rds_cluster.primary.master_user_secret[0].secret_arn
SUBNET_GROUP_NAME = aws_db_subnet_group.all.name
}
Expand Down
4 changes: 4 additions & 0 deletions rds/promote-read-replicas/output.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
output "endpoint" {
description = "RDS Endpoint"
value = aws_rds_cluster.primary.endpoint
}
23 changes: 23 additions & 0 deletions rds/promote-read-replicas/securitygroup.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Security Group
resource "aws_security_group" "rds_postgresql_sg" {
name = "rds-postgresql-sg"
description = "Security group for RDS PostgreSQL cluster"

ingress {
from_port = 5432
to_port = 5432
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"] # Replace with your IP range or VPC CIDR
}

egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}

tags = {
Name = "rds-postgresql-sg"
}
}
21 changes: 20 additions & 1 deletion rds/promote-read-replicas/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,17 @@ variable "db_type" {
default = "db.t3.medium"
description = "Database Instance Type"
}
variable "db_name" {
default = "test"
description = "Default DB Name"
}

variable "publicly_accessible" {
default = true
description = "Access from public or not"
}
variable "availability_zones" {
default = ["us-east-1a", "us-east-1b"]
default = ["us-east-1a", "us-east-1b", "us-east-1c"]
type = list(string)
description = "RDS AZs"
}
Expand All @@ -23,3 +31,14 @@ variable "emails" {
description = "List notification emails"
default = ["[email protected]"]
}
variable "skip_final_snapshot" {
type = bool
description = "Skip Final Snapshot or not"
default = true
}

variable "ignore_changes_behavior" {
type = list(any)
default = []
}

0 comments on commit 6bdcf3a

Please sign in to comment.