About

nolanpeet@gmail.com | Lakewood, CO

Home Tempurature Monitor About New Post

I'm Nolan Peet. I like to do stuff that is interesting

This project was pretty fun, its a very compact bit of code all hosted on AWS all for free.

I appreciate programming the frameworks and data structures for my projects. I have a lot of experience with building full stacks in Python, JavaScript, PHP, and basic AWS.

I'm using AWS API Gateway to serve html with embedded JS through a REST api. The api uses AWS Lambda integrations written in python to run the logic for my webserver.This includes serving files from my self-hosted websites for a low-cost solution, or load-balancing with AWS mirror backups for a cloud-based backup.

AWS gives enough cloud compute time for free to run simple serverless web applications like this forever with 0 oversight. Combining Lambda automation scripts with self-hosted endpoints can help me distribute my network more securely and with greater availability through AWS proxies.

exerpt from ./lambda_function.py:

...
def lambda_handler(event, context):
http_method = event.get("httpMethod", event.get("method", ""))
if http_method == "GET":
# Process GET as before
path = event["path"]
if path != '/':
path_parts = path.split('/')
if path[0] == '/':
path_parts = path_parts[1:]
file_path = os.path.join(os.path.dirname(__file__), "www", *path_parts, "index.html")
else:
file_path = os.path.join(os.path.dirname(__file__), "www", "index.html")

try:
with open(file_path, "r") as f:
html_content = f.read()
return {
"statusCode": 200,
"headers": { "Content-Type": "text/html" },
"body": html_content
}
except FileNotFoundError:
return {
"statusCode": 404,
"headers": {"Content-Type": "text/plain"},
"body": "404 Not Found"
}
elif http_method == "POST":
...

more from my other site, gfd.sh - lambda_function.py:

def get_data():
"""
1) Try to get data from self-host http://cpu1.nolp.net/data
2) If unsuccessful, fallback to S3
"""
return response(200, requests.get('https://cpu1.nolp.net/data').text)
try:
resp = requests.get('https://cpu1.nolp.net/data', timeout=2)
if resp.status_code == 200:
# Return that data

return response(200, resp.text) # or base64 if it's binary
else:
# fallback to S3
return get_data_from_s3()
except requests.exceptions.RequestException:
# fallback to S3
return get_data_from_s3()

...

def lambda_handler(event, context):
"""Entry point for API Gateway -> Lambda integration."""

http_method = event.get('httpMethod', '')
headers = event.get('headers', {})
# Route by HTTP method
if http_method == 'POST':
# Auth Check
auth_header = headers.get('Authorization', '')
if not auth_header.startswith("Bearer "):
return response(401, "Unauthorized")

# Extract token from 'Bearer XXXXXX'
_, token_value = auth_header.split(" ")
if token_value != PRE_SHARED_TOKEN:
return response(401, "Invalid token.")
return post_data(event)
elif http_method == 'GET':
return get_data()
else:
return response(405, f"Method {http_method} not allowed.")

Posted on 2025-04-04 03:48:07 MDT