Skip to content

Commit

Permalink
changed the .env
Browse files Browse the repository at this point in the history
  • Loading branch information
Shadi38 committed Feb 18, 2025
1 parent 8f4fd66 commit f294742
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 141 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/server.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,22 @@ jobs:
- name: Pull Docker image
run: |
ssh -o StrictHostKeyChecking=no -i my-new-key.pem ec2-user@18.132.98.88 'sudo docker pull shadi38/node-app:4.0'
ssh -o StrictHostKeyChecking=no -i my-new-key.pem ec2-user@13.43.88.72 'sudo docker pull shadi38/node-app:4.0'
- name: Stop running container
run: |
ssh -o StrictHostKeyChecking=no -i my-new-key.pem ec2-user@18.132.98.88 'sudo docker stop node-app || true'
ssh -o StrictHostKeyChecking=no -i my-new-key.pem ec2-user@18.132.98.88 'sudo docker rm node-app || true'
ssh -o StrictHostKeyChecking=no -i my-new-key.pem ec2-user@13.43.88.72 'sudo docker stop node-app || true'
ssh -o StrictHostKeyChecking=no -i my-new-key.pem ec2-user@13.43.88.72 'sudo docker rm node-app || true'
- name: Create `.env` file on EC2
run: |
ssh -o StrictHostKeyChecking=no -i my-new-key.pem ec2-user@18.132.98.88 << 'EOF'
ssh -o StrictHostKeyChecking=no -i my-new-key.pem ec2-user@13.43.88.72 << 'EOF'
echo "${{ secrets.SERVER_ENV_FILE }}" > /home/ec2-user/.env
EOF
- name: Run new container with `.env`
run: |
ssh -o StrictHostKeyChecking=no -i my-new-key.pem ec2-user@18.132.98.88 << 'EOF'
ssh -o StrictHostKeyChecking=no -i my-new-key.pem ec2-user@13.43.88.72 << 'EOF'
sudo docker stop node-app || true
sudo docker rm node-app || true
sudo docker run -d --name node-app --restart=always -p 3000:3000 --env-file /home/ec2-user/.env shadi38/node-app:4.0
Expand Down
159 changes: 23 additions & 136 deletions infrastructure/resources.tf
Original file line number Diff line number Diff line change
@@ -1,122 +1,34 @@
# module "s3" {
# source = "./s3"
# bucket_name = "reccomendationsh"
# }

# resource "aws_instance" "example_server" {
# ami = "ami-0171207a7acd2a570"
# instance_type = "t2.micro"
# vpc_security_group_ids = [local.recommendationRds_id]
# # Adding the key_name to attach the key pair during instance creation
# key_name = "my-new-key"

# tags = {
# Name = "reccomendationserver"
# }


# provisioner "remote-exec" {
# connection {
# type = "ssh"
# user = "ec2-user"
# host = self.public_ip //This means that the host IP address for the SSH connection should be the public IP of the EC2
# private_key = file("~/.ssh/my-new-key.pem")

# }
# //ensures that npm and Docker are installed and running before deploying applications.
# inline = [
# "sudo yum update -y", # Update system packages
# "sudo yum install -y nodejs npm", # Install Node.js and npm
# "sudo npm update -y", # Update npm packages
# "sudo yum install -y docker", # Install Docker
# "sudo systemctl start docker", # Start Docker service
# "sudo systemctl enable docker" # Enable Docker to start on boot
# ]
# }
# }

# resource "aws_security_group" "recommendationRds" {
# name = "reccomendation_security_group"
# description = "Allow ports 22, 8080, and 3000"

# ingress {
# from_port = 5432
# to_port = 5432
# protocol = "tcp"
# security_groups = [aws_security_group.recommendationEc2.id] # Allow only EC2
# }

# ingress {
# from_port = 22
# to_port = 22
# protocol = "tcp"
# cidr_blocks = ["0.0.0.0/0"] # Allows SSH from anywhere
# }

# ingress {
# from_port = 8080
# to_port = 8080
# protocol = "tcp"
# cidr_blocks = ["0.0.0.0/0"]
# }

# ingress {
# from_port = 3000
# to_port = 3000
# protocol = "tcp"
# cidr_blocks = ["0.0.0.0/0"]
# }

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


# resource "aws_db_instance" "default" {
# allocated_storage = 10
# engine = "postgres"
# instance_class = "db.t3.micro"
# db_name = "recommendation"
# username = "postgres"
# password = var.db_password
# publicly_accessible = true
# vpc_security_group_ids = [local.recommendationRds_id]
# skip_final_snapshot = true // required to destroy
# multi_az = false
# # Tags for organization (optional)
# tags = {
# Name = "RecommendationDatabase"
# }
# }


module "s3" {
source = "./s3"
bucket_name = "reccomendationsh"
}

# EC2 Security Group
resource "aws_security_group" "recommendationEc2" {
name = "reccomendation_ec2_security_group"
description = "Allow ports 22, 8080, 3000 from anywhere"

description = "Allow access to EC2"
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"] # Allow SSH from anywhere (modify as needed)
cidr_blocks = ["0.0.0.0/0"]
}

ingress {
from_port = 3000
to_port = 3000
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"] # Allow port 3000 from anywhere (modify as needed)
cidr_blocks = ["0.0.0.0/0"]
}

ingress {
from_port = 8080
to_port = 8080
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}


egress {
from_port = 0
Expand All @@ -126,11 +38,10 @@ resource "aws_security_group" "recommendationEc2" {
}
}

# EC2 Instance
resource "aws_instance" "example_server" {
ami = "ami-0171207a7acd2a570"
instance_type = "t2.micro"
vpc_security_group_ids = [aws_security_group.recommendationEc2.id] # Attach EC2 security group
vpc_security_group_ids = [aws_security_group.recommendationEc2.id]
key_name = "my-new-key"

tags = {
Expand All @@ -156,43 +67,16 @@ resource "aws_instance" "example_server" {
}
}

# RDS Security Group
resource "aws_security_group" "recommendationRds" {
name = "reccomendation_security_group"
description = "Allow port 5432 from EC2 security group"
name = "reccomendation-rds_security_group"
description = "Allow PostgreSQL access"

ingress {
from_port = 5432
to_port = 5432
protocol = "tcp"
security_groups = [aws_security_group.recommendationEc2.id] # Allow access from EC2 security group
}
ingress {
from_port = 5432
to_port = 5432
protocol = "tcp"
security_groups = ["0.0.0.0/0"]
}

ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"] # Allows SSH from anywhere
}

ingress {
from_port = 8080
to_port = 8080
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}

ingress {
from_port = 3000
to_port = 3000
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
# security_groups = [aws_security_group.recommendationEc2.id]
security_groups = [aws_security_group.recommendationEc2.id]
}

egress {
Expand All @@ -203,19 +87,22 @@ resource "aws_security_group" "recommendationRds" {
}
}

# RDS Instance
resource "aws_db_instance" "default" {
allocated_storage = 10
engine = "postgres"
instance_class = "db.t3.micro"
db_name = "recommendation"
db_name = "recommendationDatabase"
username = "postgres"
password = var.db_password
publicly_accessible = true
vpc_security_group_ids = [aws_security_group.recommendationRds.id]
vpc_security_group_ids = [aws_security_group.recommendationRds.id]
skip_final_snapshot = true
multi_az = false

tags = {
Name = "RecommendationDatabase"
}

depends_on = [aws_security_group.recommendationEc2] # Ensures EC2 security group is removed before the DB
}

0 comments on commit f294742

Please sign in to comment.