Skip to content

Commit

Permalink
start a web server
Browse files Browse the repository at this point in the history
  • Loading branch information
proquickly committed Nov 10, 2024
1 parent e238a6e commit ef3c963
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 19 deletions.
1 change: 0 additions & 1 deletion .idea/tfgha.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 62 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ packages = [{include = "tfgha", from = "src"}]

[tool.poetry.dependencies]
python = "^3.12"
duckdb = "^1.1.3"


[build-system]
Expand Down
63 changes: 47 additions & 16 deletions terraform/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,65 @@ provider "aws" {
region = "us-west-2"
}

resource "aws_instance" "example" {
resource "aws_instance" "py_server" {
ami = "ami-0709112b97e5accb1"
instance_type = "t2.micro"

vpc_security_group_ids = [aws_security_group.allow_app.id]
user_data = <<-EOF
#!/bin/bash
yum update -y
yum install -y python3 git
# Clone your git repository (ensure it's public or handle git authentication accordingly)
#cd /home/ec2-user
#git clone https://github.com/your_username/your_repository.git
yum install -y python3 python3-pip
mkdir /app
cat <<EOT > /app/app.py
from flask import Flask
app = Flask(__name__)
# Navigate to the repository
#cd your_repository
@app.route('/')
def hello():
return "Hello from Python!"
# Optionally, create a virtual environment
#python3 -m venv venv
#source venv/bin/activate
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
EOT
# Install any required dependencies from requirements.txt
#pip install -r requirements.txt
pip3 install flask
# Run your Python script
python3 -c "import http.server; import socketserver; PORT = 8080; Handler = http.server.SimpleHTTPRequestHandler; with socketserver.TCPServer(('', PORT), Handler) as httpd: print('Serving HTTP on port', PORT); httpd.serve_forever()"
# Run app
python3 /app/app.py &
EOF

tags = {
Name = "GitHubActionsEC2"
}
}
resource "aws_security_group" "allow_app" {
name = "allow_app"
description = "Allow inbound traffic for Python app"

ingress {
description = "App Port"
from_port = 5000
to_port = 5000
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}

ingress {
description = "SSH"
from_port = 22
to_port = 22
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"]
}
}

output "public_ip" {
value = aws_instance.py_server.public_ip
}

0 comments on commit ef3c963

Please sign in to comment.