commit d7d5ee08368921a642bb3cb799dadefc8e2d525d Author: Stefano Date: Tue May 13 18:13:40 2025 -0500 first commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d4dcdf7 --- /dev/null +++ b/.gitignore @@ -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 \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..2139787 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# To do diff --git a/app.py b/app.py new file mode 100644 index 0000000..ea9d225 --- /dev/null +++ b/app.py @@ -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() \ No newline at end of file diff --git a/app/main.py b/app/main.py new file mode 100644 index 0000000..b5a1bc1 --- /dev/null +++ b/app/main.py @@ -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"} \ No newline at end of file diff --git a/cdk.json b/cdk.json new file mode 100644 index 0000000..6c1e42e --- /dev/null +++ b/cdk.json @@ -0,0 +1,14 @@ +{ + "app": "python3 app.py", + "watch": { + "include": ["**"], + "exclude": [ + "README.md", + "cdk*.json", + "requirements*.txt", + "source.bat", + "**/__init__.py", + "python/__pycache__" + ] + } +} diff --git a/deploy.sh b/deploy.sh new file mode 100755 index 0000000..7c74086 --- /dev/null +++ b/deploy.sh @@ -0,0 +1,4 @@ +#!/bin/bash +npm install -g aws-cdk +python3 -m pip install -r requirements.txt +cdk deploy \ No newline at end of file diff --git a/infrastructure/stack.py b/infrastructure/stack.py new file mode 100644 index 0000000..c7365e4 --- /dev/null +++ b/infrastructure/stack.py @@ -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 + ) \ No newline at end of file diff --git a/infrastructure/userdata.sh b/infrastructure/userdata.sh new file mode 100644 index 0000000..5650736 --- /dev/null +++ b/infrastructure/userdata.sh @@ -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 & \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..a388d4f --- /dev/null +++ b/requirements.txt @@ -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 \ No newline at end of file