Skip to content

Commit 24223b6

Browse files
using ec2
1 parent ef3bab3 commit 24223b6

File tree

5 files changed

+78
-7
lines changed

5 files changed

+78
-7
lines changed

terraform/ec2.tf

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
// ...existing code...
2-
variable "key_name" {
3-
description = "Name of the EC2 key pair to attach to the instance (must exist in the AWS region)"
4-
type = string
5-
}
6-
71
resource "aws_instance" "api_server" {
82
ami = data.aws_ami.amazon_linux.id
93
instance_type = "t3.micro"
@@ -12,6 +6,7 @@ resource "aws_instance" "api_server" {
126
iam_instance_profile = aws_iam_instance_profile.ec2_profile.name
137
key_name = var.key_name
148

9+
# <-- THIS IS WHERE THE EC2 CONNECTS TO RDS VIA USER DATA
1510
user_data = <<-EOF
1611
#!/bin/bash
1712
yum update -y
@@ -20,14 +15,28 @@ resource "aws_instance" "api_server" {
2015
systemctl enable docker
2116
usermod -aG docker ec2-user
2217
18+
# Environment variables for RDS
19+
export DB_HOST=${aws_db_instance.postgres.address}
20+
export DB_PORT=5432
21+
export DB_USER=${var.db_username}
22+
export DB_PASS=${var.db_password}
23+
export DB_NAME=mydb
24+
export PORT=3000
25+
26+
# Login to ECR and run Docker container
2327
aws ecr get-login-password --region ap-south-1 \
2428
| docker login --username AWS --password-stdin ${aws_ecr_repository.node_api.repository_url}
2529
2630
docker pull ${aws_ecr_repository.node_api.repository_url}:latest
27-
2831
docker run -d \
2932
-p 3000:3000 \
3033
--name node-api \
34+
-e DB_HOST=$DB_HOST \
35+
-e DB_PORT=$DB_PORT \
36+
-e DB_USER=$DB_USER \
37+
-e DB_PASS=$DB_PASS \
38+
-e DB_NAME=$DB_NAME \
39+
-e PORT=$PORT \
3140
${aws_ecr_repository.node_api.repository_url}:latest
3241
EOF
3342

terraform/outputs.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
output "rds_endpoint" {
2+
value = aws_db_instance.postgres.address
3+
}
4+
5+
output "api_url" {
6+
value = "http://${aws_instance.api_server.public_ip}:3000/users"
7+
}

terraform/rds.tf

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
resource "aws_db_subnet_group" "rds_subnet_group" {
2+
name = "rds-subnet-group"
3+
subnet_ids = [aws_subnet.public.id, aws_subnet.private.id] # or private only
4+
}
5+
6+
resource "aws_db_instance" "postgres" {
7+
identifier = "node-api-db"
8+
allocated_storage = 20
9+
engine = "postgres"
10+
engine_version = "15.3"
11+
instance_class = "db.t3.micro"
12+
db_name = "mydb"
13+
username = var.db_username
14+
password = var.db_password
15+
db_subnet_group_name = aws_db_subnet_group.rds_subnet_group.name
16+
vpc_security_group_ids = [aws_security_group.rds_sg.id]
17+
skip_final_snapshot = true
18+
publicly_accessible = false
19+
deletion_protection = false
20+
}

terraform/security_groups.tf

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,22 @@ resource "aws_security_group" "ec2_sg" {
2424
cidr_blocks = ["0.0.0.0/0"]
2525
}
2626
}
27+
28+
resource "aws_security_group" "rds_sg" {
29+
name = "rds-sg"
30+
vpc_id = aws_vpc.main.id
31+
32+
ingress {
33+
from_port = 5432
34+
to_port = 5432
35+
protocol = "tcp"
36+
security_groups = [aws_security_group.ec2_sg.id] # allow EC2 access
37+
}
38+
39+
egress {
40+
from_port = 0
41+
to_port = 0
42+
protocol = "-1"
43+
cidr_blocks = ["0.0.0.0/0"]
44+
}
45+
}

terraform/variables.tf

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
variable "key_name" {
2+
description = "Name of the EC2 key pair to attach to the instance (must exist in the AWS region)"
3+
type = string
4+
}
5+
// ...existing code...
6+
variable "db_username" {
7+
description = "Master username for the RDS instance"
8+
type = string
9+
default = appuser
10+
}
11+
12+
variable "db_password" {
13+
description = "Master password for the RDS instance (sensitive)"
14+
type = string
15+
sensitive = true
16+
}

0 commit comments

Comments
 (0)