first commit
This commit is contained in:
14
.gitignore
vendored
Normal file
14
.gitignore
vendored
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
*.swp
|
||||||
|
package-lock.json
|
||||||
|
__pycache__
|
||||||
|
.pytest_cache
|
||||||
|
.env
|
||||||
|
.venv
|
||||||
|
*.egg-info
|
||||||
|
.coverage
|
||||||
|
cdk.out/
|
||||||
|
node_modules/
|
||||||
|
|
||||||
|
# CDK asset staging directory
|
||||||
|
.cdk.staging
|
||||||
|
cdk.out
|
||||||
7
app.py
Normal file
7
app.py
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
import aws_cdk as cdk
|
||||||
|
from infrastructure.stack import IptvUpdaterStack
|
||||||
|
|
||||||
|
app = cdk.App()
|
||||||
|
IptvUpdaterStack(app, "IptvUpdater")
|
||||||
|
app.synth()
|
||||||
11
app/main.py
Normal file
11
app/main.py
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
from fastapi import FastAPI
|
||||||
|
|
||||||
|
app = FastAPI()
|
||||||
|
|
||||||
|
@app.get("/")
|
||||||
|
async def root():
|
||||||
|
return {"message": "Hello World"}
|
||||||
|
|
||||||
|
@app.get("/health")
|
||||||
|
async def health():
|
||||||
|
return {"status": "healthy"}
|
||||||
14
cdk.json
Normal file
14
cdk.json
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
{
|
||||||
|
"app": "python3 app.py",
|
||||||
|
"watch": {
|
||||||
|
"include": ["**"],
|
||||||
|
"exclude": [
|
||||||
|
"README.md",
|
||||||
|
"cdk*.json",
|
||||||
|
"requirements*.txt",
|
||||||
|
"source.bat",
|
||||||
|
"**/__init__.py",
|
||||||
|
"python/__pycache__"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
4
deploy.sh
Executable file
4
deploy.sh
Executable file
@@ -0,0 +1,4 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
npm install -g aws-cdk
|
||||||
|
python3 -m pip install -r requirements.txt
|
||||||
|
cdk deploy
|
||||||
101
infrastructure/stack.py
Normal file
101
infrastructure/stack.py
Normal file
@@ -0,0 +1,101 @@
|
|||||||
|
import os
|
||||||
|
from aws_cdk import (
|
||||||
|
Stack,
|
||||||
|
aws_ec2 as ec2,
|
||||||
|
aws_iam as iam,
|
||||||
|
CfnOutput
|
||||||
|
)
|
||||||
|
from constructs import Construct
|
||||||
|
|
||||||
|
class IptvUpdaterStack(Stack):
|
||||||
|
def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
|
||||||
|
super().__init__(scope, construct_id, **kwargs)
|
||||||
|
|
||||||
|
# Create VPC
|
||||||
|
vpc = ec2.Vpc(self, "IptvUpdaterVPC",
|
||||||
|
max_azs=1, # Use only one AZ for free tier
|
||||||
|
nat_gateways=0, # No NAT Gateway to stay in free tier
|
||||||
|
subnet_configuration=[
|
||||||
|
ec2.SubnetConfiguration(
|
||||||
|
name="public",
|
||||||
|
subnet_type=ec2.SubnetType.PUBLIC,
|
||||||
|
cidr_mask=24
|
||||||
|
)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
# Security Group
|
||||||
|
security_group = ec2.SecurityGroup(
|
||||||
|
self, "IptvUpdaterSG",
|
||||||
|
vpc=vpc,
|
||||||
|
allow_all_outbound=True
|
||||||
|
)
|
||||||
|
|
||||||
|
security_group.add_ingress_rule(
|
||||||
|
ec2.Peer.any_ipv4(),
|
||||||
|
ec2.Port.tcp(443),
|
||||||
|
"Allow HTTPS traffic"
|
||||||
|
)
|
||||||
|
|
||||||
|
security_group.add_ingress_rule(
|
||||||
|
ec2.Peer.any_ipv4(),
|
||||||
|
ec2.Port.tcp(80),
|
||||||
|
"Allow HTTP traffic"
|
||||||
|
)
|
||||||
|
|
||||||
|
security_group.add_ingress_rule(
|
||||||
|
ec2.Peer.any_ipv4(),
|
||||||
|
ec2.Port.tcp(22),
|
||||||
|
"Allow SSH traffic"
|
||||||
|
)
|
||||||
|
|
||||||
|
# Key pair for IPTV Updater instance
|
||||||
|
key_pair = ec2.KeyPair(self, "IptvUpdaterKeyPair",
|
||||||
|
key_pair_name="iptv-updater-key"
|
||||||
|
)
|
||||||
|
|
||||||
|
# Create IAM role for EC2
|
||||||
|
role = iam.Role(
|
||||||
|
self, "IptvUpdaterRole",
|
||||||
|
assumed_by=iam.ServicePrincipal("ec2.amazonaws.com")
|
||||||
|
)
|
||||||
|
|
||||||
|
# Add SSM managed policy
|
||||||
|
role.add_managed_policy(
|
||||||
|
iam.ManagedPolicy.from_aws_managed_policy_name(
|
||||||
|
"AmazonSSMManagedInstanceCore"
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
# Read the userdata script with proper path resolution
|
||||||
|
script_dir = os.path.dirname(os.path.abspath(__file__))
|
||||||
|
userdata_path = os.path.join(script_dir, "userdata.sh")
|
||||||
|
userdata_file = open(userdata_path, "rb").read()
|
||||||
|
|
||||||
|
# Creates a userdata object for Linux hosts
|
||||||
|
userdata = ec2.UserData.for_linux()
|
||||||
|
# Adds one or more commands to the userdata object.
|
||||||
|
userdata.add_commands(str(userdata_file, 'utf-8'))
|
||||||
|
|
||||||
|
# EC2 Instance
|
||||||
|
instance = ec2.Instance(
|
||||||
|
self, "IptvUpdaterInstance",
|
||||||
|
vpc=vpc,
|
||||||
|
instance_type=ec2.InstanceType.of(
|
||||||
|
ec2.InstanceClass.T2,
|
||||||
|
ec2.InstanceSize.MICRO
|
||||||
|
),
|
||||||
|
machine_image=ec2.AmazonLinuxImage(
|
||||||
|
generation=ec2.AmazonLinuxGeneration.AMAZON_LINUX_2
|
||||||
|
),
|
||||||
|
security_group=security_group,
|
||||||
|
key_pair=key_pair,
|
||||||
|
role=role,
|
||||||
|
user_data=userdata,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Output the public DNS name
|
||||||
|
CfnOutput(
|
||||||
|
self, "InstancePublicDNS",
|
||||||
|
value=instance.instance_public_dns_name
|
||||||
|
)
|
||||||
31
infrastructure/userdata.sh
Normal file
31
infrastructure/userdata.sh
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
yum update -y
|
||||||
|
yum install -y python3-pip git
|
||||||
|
amazon-linux-extras install nginx1
|
||||||
|
|
||||||
|
pip3 install --upgrade pip
|
||||||
|
pip3 install certbot certbot-nginx
|
||||||
|
|
||||||
|
cd /home/ec2-user
|
||||||
|
|
||||||
|
#git clone https://github.com/yourusername/your-repo.git",
|
||||||
|
#cd your-repo",
|
||||||
|
#pip3 install -r requirements.txt",
|
||||||
|
|
||||||
|
# Create nginx config
|
||||||
|
# echo 'server {
|
||||||
|
# listen 80;
|
||||||
|
# server_name $HOSTNAME;
|
||||||
|
# location / {
|
||||||
|
# proxy_pass http://127.0.0.1:8000;
|
||||||
|
# proxy_set_header Host $host;
|
||||||
|
# proxy_set_header X-Real-IP $remote_addr;
|
||||||
|
# }
|
||||||
|
# }' > /etc/nginx/conf.d/iptvUpdater.conf
|
||||||
|
|
||||||
|
systemctl start nginx
|
||||||
|
systemctl enable nginx
|
||||||
|
|
||||||
|
# Start IptvUpdater on port 8000
|
||||||
|
# nohup uvicorn app.main:app --host 127.0.0.1 --port 8000 &
|
||||||
5
requirements.txt
Normal file
5
requirements.txt
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
fastapi==0.104.1
|
||||||
|
uvicorn==0.24.0
|
||||||
|
aws-cdk-lib>=2.0.0
|
||||||
|
constructs>=10.0.0
|
||||||
|
python-dotenv==1.0.0
|
||||||
Reference in New Issue
Block a user