import json import boto3 import os from typing import Any, Dict class LambdaClient: def __init__(self): self.region = os.getenv("AWS_REGION", "us-east-1") self.client = boto3.client("lambda", region_name=self.region) def invoke( self, function_name: str, payload: Dict[str, Any], async_invoke: bool = False ) -> Dict[str, Any]: """Invoke a Lambda function.""" try: response = self.client.invoke( FunctionName=function_name, InvocationType="Event" if async_invoke else "RequestResponse", Payload=json.dumps(payload) ) if "Payload" in response: response_payload = json.loads(response["Payload"].read()) return { "status_code": response.get("StatusCode"), "payload": response_payload } return {"status_code": response.get("StatusCode")} except Exception as e: return {"error": str(e)} def list_functions(self) -> list: """List all Lambda functions.""" try: paginator = self.client.get_paginator("list_functions") functions = [] for page in paginator.paginate(): functions.extend(page["Functions"]) return functions except Exception as e: return {"error": str(e)} def get_function_info(self, function_name: str) -> Dict: """Get information about a Lambda function.""" try: response = self.client.get_function(FunctionName=function_name) config = response.get("Configuration", {}) return { "name": config.get("FunctionName"), "arn": config.get("FunctionArn"), "runtime": config.get("Runtime"), "handler": config.get("Handler"), "timeout": config.get("Timeout"), "memory": config.get("MemorySize") } except Exception as e: return {"error": str(e)}