|
| 1 | +import json |
| 2 | +import logging |
| 3 | +import requests # this requires adding request layer to lambda function |
| 4 | +from urllib.parse import urlparse, urlunparse |
| 5 | + |
| 6 | +logging.basicConfig(level=logging.DEBUG) |
| 7 | +logger = logging.getLogger() |
| 8 | + |
| 9 | +EC2_PROXY_ENDPOINT = "http://XX.X.X.XX" # Replace with private IP of EC2 proxy running Python/Flask |
| 10 | +if not EC2_PROXY_ENDPOINT: |
| 11 | + logger.error("EC2_PROXY_ENDPOINT is not set") |
| 12 | + raise ValueError("EC2_PROXY_ENDPOINT is not set") |
| 13 | +logger.debug(f"EC2_PROXY_ENDPOINT: {EC2_PROXY_ENDPOINT}") |
| 14 | + |
| 15 | +def lambda_handler(event, context): |
| 16 | + """ |
| 17 | + Expected event example: |
| 18 | + { |
| 19 | + "method": "GET", |
| 20 | + "url": "https://cloud-dev.mongodb.com/api/atlas/v2/groups", |
| 21 | + "headers": {"Header-Key": "value", ...}, |
| 22 | + "body": "request body as string" |
| 23 | + } |
| 24 | +
|
| 25 | + This Lambda function should be deployed in a private subnet. It's corresponding SG |
| 26 | + only allows traffic to EC2 proxy running Python/Flask |
| 27 | + """ |
| 28 | + logger.debug(f"Received event: {json.dumps(event)}") # TODO: remove |
| 29 | + try: |
| 30 | + method = event.get("method") |
| 31 | + url = event.get("url") |
| 32 | + headers = event.get("headers", {}) |
| 33 | + body = event.get("body", None) |
| 34 | + |
| 35 | + if method is None or url is None: |
| 36 | + msg = "Missing 'method' or 'url' in the event payload" |
| 37 | + logger.error(msg) |
| 38 | + raise ValueError(msg) |
| 39 | + |
| 40 | + logger.debug(f"Forwarding request - Method: {method}, URL: {url}, Headers: {headers}, Body: {body}") |
| 41 | + |
| 42 | + parsed_url = urlparse(url) |
| 43 | + if parsed_url.scheme and parsed_url.netloc: |
| 44 | + # Extract the path and query string because incoming URL will be in format: |
| 45 | + # https://www.cloud.mongodb.com/api/atlas/v2/groups?param=value |
| 46 | + path = parsed_url.path or "" |
| 47 | + query = f"?{parsed_url.query}" if parsed_url.query else "" |
| 48 | + new_url = path + query |
| 49 | + logger.debug(f"Extracted relative URL: {new_url} from absolute URL: {url}") |
| 50 | + else: |
| 51 | + new_url = url |
| 52 | + logger.debug(f"URL is relative: {new_url}") |
| 53 | + |
| 54 | + # Construct URL for the EC2 proxy |
| 55 | + full_url = EC2_PROXY_ENDPOINT.rstrip("/") + new_url |
| 56 | + logger.debug(f"Constructed full URL for proxy: {full_url}") |
| 57 | + |
| 58 | + # Forward request to the EC2 proxy |
| 59 | + response = requests.request(method, full_url, headers=headers, data=body) |
| 60 | + logger.debug(f"Response from EC2 proxy - Status: {response.status_code}, Headers: {dict(response.headers)}, Body: {response.text}") |
| 61 | + |
| 62 | + return { |
| 63 | + "statusCode": response.status_code, |
| 64 | + "headers": dict(response.headers), |
| 65 | + "body": response.text |
| 66 | + } |
| 67 | + except Exception as e: |
| 68 | + logger.exception("Error processing the request:") |
| 69 | + return { |
| 70 | + "statusCode": 500, |
| 71 | + "headers": {}, |
| 72 | + "body": json.dumps({"error": str(e)}) |
| 73 | + } |
0 commit comments