Skip to content

Commit 07dfcdd

Browse files
committed
feat: add scripts, fix sg.
1 parent 7e5439e commit 07dfcdd

File tree

4 files changed

+54
-23
lines changed

4 files changed

+54
-23
lines changed

package.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,14 @@
2525
"test:coverage": "jest --coverage",
2626
"prisma:generate": "prisma generate",
2727
"prisma:migrate": "prisma migrate dev",
28-
"prisma:seed": "tsx prisma/seed/all.ts"
28+
"prisma:seed": "tsx prisma/seed/all.ts",
29+
"ssm:init": "./scripts/ssm.sh -init",
30+
"ssm:connect:test": "./scripts/ssm.sh -connect:test",
31+
"ssm:connect:prod": "./scripts/ssm.sh -connect:prod",
32+
"ssm:stop": "./scripts/ssm.sh -stop",
33+
"terraform:validate": "cd terraform && terraform validate",
34+
"terraform:plan": "cd terraform && terraform plan",
35+
"terraform:apply": "cd terraform && terraform apply"
2936
},
3037
"dependencies": {
3138
"@apollo/client": "^3.11.10",

scripts/ssm.sh

100644100755
Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
#!/bin/bash
2+
13
# Accepts arguments: -init, -connect, -stop
24
# -init: Start the EC2 instance.
35
# -connect: Connect to the EC2 instance and port-forward the RDS.
46
# -stop: Stop the EC2 instance.
57

68
# Import everything from ../.env into environment.
7-
source ../.env
9+
source .env
810

911
# If the first argument is -init, start the EC2 instance.
1012
if [ "$1" == "-init" ]; then
@@ -13,10 +15,19 @@ if [ "$1" == "-init" ]; then
1315
fi
1416

1517
# If the first argument is -connect, connect to the EC2 instance and port-forward the RDS.
16-
aws ssm start-session \
17-
--target $BASTION_EC2_INSTANCE_ID \
18-
--document-name AWS-StartPortForwardingSessionToRemoteHost \
19-
--parameters host="$RDS_TEST_INSTANCE_HOST",portNumber="5432",localPortNumber="5433"
18+
if [ "$1" == "-connect:test" ]; then
19+
aws ssm start-session \
20+
--target $BASTION_EC2_INSTANCE_ID \
21+
--document-name AWS-StartPortForwardingSessionToRemoteHost \
22+
--parameters host="$RDS_TEST_INSTANCE_HOST",portNumber="5432",localPortNumber="5433"
23+
fi
24+
25+
if [ "$1" == "-connect:prod" ]; then
26+
aws ssm start-session \
27+
--target $BASTION_EC2_INSTANCE_ID \
28+
--document-name AWS-StartPortForwardingSessionToRemoteHost \
29+
--parameters host="$RDS_PROD_INSTANCE_HOST",portNumber="5432",localPortNumber="5433"
30+
fi
2031

2132
# If the first argument is -stop, stop the EC2 instance.
2233
if [ "$1" == "-stop" ]; then

terraform/rds.tf

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ resource "aws_db_instance" "postgresql" {
2222

2323
skip_final_snapshot = true
2424

25-
vpc_security_group_ids = [var.rds_security_group_id]
25+
vpc_security_group_ids = [var.rds_security_group_id, aws_security_group.rds_sg.id]
2626
db_subnet_group_name = aws_db_subnet_group.postgresql.name
2727

2828
publicly_accessible = false
@@ -34,3 +34,32 @@ resource "aws_db_instance" "postgresql" {
3434
prevent_destroy = true
3535
}
3636
}
37+
38+
39+
resource "aws_security_group" "rds_sg" {
40+
name = "rds-security-group"
41+
description = "Security group for RDS instances"
42+
vpc_id = aws_vpc.main.id
43+
44+
ingress {
45+
from_port = 5432
46+
to_port = 5432
47+
protocol = "tcp"
48+
security_groups = [aws_security_group.bastion-sg.id]
49+
}
50+
51+
egress {
52+
from_port = 0
53+
to_port = 0
54+
protocol = "-1"
55+
cidr_blocks = ["0.0.0.0/0"]
56+
}
57+
58+
tags = {
59+
Name = "rds-sg"
60+
}
61+
62+
lifecycle {
63+
prevent_destroy = true
64+
}
65+
}

terraform/security.tf

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)