Skip to content

Instantly share code, notes, and snippets.

@cometaj2
Last active January 11, 2025 01:45
Show Gist options
  • Save cometaj2/b187e6119f3a2c2cb55637910128f299 to your computer and use it in GitHub Desktop.
Save cometaj2/b187e6119f3a2c2cb55637910128f299 to your computer and use it in GitHub Desktop.
HCLI hosted on AWS Lambda (standalone lambda WSGI handler (no AWS API Gateway))
#!/usr/bin/env python3
import os
# Send huckle to /mnt/efs for lambda
fs = "/mnt/efs"
os.environ['HUCKLE_HOME'] = fs
import logging
# Disable Lambda's default handler to prevent duplicate logs in cloudwatch
logging.getLogger().handlers = []
import json
import errno
import sys
from apig_wsgi import make_lambda_handler
import hcli_core
def create_credentials_file():
creds_path = fs + "/creds"
if not os.path.exists(creds_path):
creds_content = """[config]
core.auth = True
core.root = aggregate
mgmt.credentials = local
[default]
username = admin
password = *
salt = *"""
with open(creds_path, "w") as f:
f.write(creds_content)
return creds_path
creds_path = create_credentials_file()
server = hcli_core.connector(config_path=creds_path)
# Or include a cli folder with a 3rd party HCLI code in your lambda deployment
# hcli = os.getcwd() + "/cli"
# server = hcli_core.connector(plugin_path=hcli, config_path=creds_path)
apig_wsgi_handler = make_lambda_handler(server, binary_support=True)
def handler(event, context):
try:
#print(json.dumps(event, indent=2, sort_keys=True))
response = apig_wsgi_handler(event, context)
return response
except Exception as warning:
eprint(warning)
return ""
# helps with printing error messages to STDERR
def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment