From 24635656943f8c9fa3d552d49c15339706f8d5a8 Mon Sep 17 00:00:00 2001 From: ravisumit33 Date: Sun, 1 Oct 2023 04:19:43 +0530 Subject: [PATCH] Automate postgres & redis setup --- .ebextensions/postgres.config | 28 +++++++++++++++++++--- .ebextensions/single-instance-redis.config | 8 +++++++ 2 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 .ebextensions/single-instance-redis.config diff --git a/.ebextensions/postgres.config b/.ebextensions/postgres.config index 520de9a4..22542678 100644 --- a/.ebextensions/postgres.config +++ b/.ebextensions/postgres.config @@ -1,3 +1,25 @@ -packages: - yum: - postgresql-devel: [] +commands: + 01_update_yum: + command: "sudo yum update -y" + 02_enable_postgresql14: + command: "sudo amazon-linux-extras enable postgresql14" + 03_install_postgresql_devel: + command: "sudo yum install postgresql-devel -y" + +## Below command is used for single-instance local postgres + 04_postgresql_server_setup: + test: "[ ! -f /usr/bin/psql ]" + command: | + sudo yum install postgresql-server -y + sudo postgresql-setup initdb + sudo systemctl start postgresql + sudo systemctl enable postgresql + sudo sed -i "/^host\s*all\s*all\s*.*$/ s/ident/scram-sha-256/" /var/lib/pgsql/data/pg_hba.conf + sudo systemctl restart postgresql + SECRETS_JSON=$(aws secretsmanager get-secret-value --secret-id "mysterio" --region "ap-south-1" --query "SecretString" --output text) + DB_NAME=$(echo "$SECRETS_JSON" | jq -r '.DB_NAME') + DB_PASS=$(echo "$SECRETS_JSON" | jq -r '.DB_PASSWORD') + DB_USER=$(echo "$SECRETS_JSON" | jq -r '.DB_USER') + sudo -u postgres psql -c "CREATE DATABASE ${DB_NAME};" + sudo -u postgres psql -c "CREATE USER ${DB_USER} WITH PASSWORD '${DB_PASS}';" + sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE ${DB_NAME} TO ${DB_USER};" diff --git a/.ebextensions/single-instance-redis.config b/.ebextensions/single-instance-redis.config new file mode 100644 index 00000000..581b7e8b --- /dev/null +++ b/.ebextensions/single-instance-redis.config @@ -0,0 +1,8 @@ +commands: + 01_redis_server_setup: + test: "[ ! -f /usr/bin/redis-server ]" + command: | + sudo yum update -y + sudo amazon-linux-extras install redis6 + sudo systemctl start redis + sudo systemctl enable redis