Package: py-lambda
AWS-ALB-lambda-pythonFramework for load-balancer and lambda function.To learn more about writing AWS Lambda functions in python, go tothe official documentationBlogs about AWS lambda and python ishereGetting StartedThis is the AWS lambda framework written in python. When the lambda function is triggered by ALB or API-Gateway the event variable that contain all the required info will be sent to the init function. By default this init function will be in lambda_function.py and the name will be lambda_handler. This framework contains the request resource mappin for the URL and response that you have to send back to Load-Balancer.The package information is inPypi infopip install py-lambdaThis source code contain the following files:lambda_function: Contains the code that will be triggered by ALB or API-Gateway etc.request: Contains the request object which is created from the event that is send as an input.response: Contains the object that should be sent back from lambda function to ALB.lambda_mapping: Is the file that contain the mapping for the routes. Add your routes in here.all_functions: Contain the function contains the respective code that can be used for the given path.test_LambdaHandler: Contains the test code that can be usedfortesting all the functions.Load-Balancer input## test_LambdaHandler.pyimportpy_lambda.test_LambdaHandleraslTestprint(lTest.EVENT_FROM_ALB)The above code will give the sample of the request that will be send by ALB.Mapping## lambda_mapping.pyFUNCT_MAPPING={"":{"":allFun.index},"test":{"":allFun.test,ld.URL_REST_ID_STR:{"":allFun.testUserId,},},"abc":{"":allFun.abc,},}This will have the mapping for each resources. The mapping is dictionary in which key is the URL resource and the value will be the function that you have to execute when the URL is called. The index URL in the mapping is the first one"": {"": allFun.index}.  If all the function for the mapping are inall_Functions.pythen corrponding function can be called in here.If the resource contan some information that will change of each request then the mapping should containURL_REST_ID_STRwhich is there inlambda_defines.pyform wilch you will be able to map the URL.Eg: 	mydomain.com/profile/USER_ID/image
In thisUSER_IDmay change for each user. In that case useURL_REST_ID_STRin the mappingInput validaation## inp_validatn.py## VALID_DOMAINS contains all the valid domains.## VALID_SUB_DOMAINS contails all the valid subdomains.importpy_lambda.funct_definesasfunDefret_validation=validateDomain("http://some_domain.com")##If the domain is valid then the function will return the success responseifret_validation[funDef.FUNCTION_STAT]==funDef.SUCCESS:print("Valid domain")else:print("invalid domain")Input validations can be adde in this function.validateDomainwill validate the given domain and return success if it is valid.Request## request.pyimportpy_lambda.requestasReqimportpy_lambda.test_LambdaHandleraslTestreqObj=Req.Request(lTest.EVENT_FROM_ALB)print("Path Str:",reqObj.getPathStr())print("Req method:",reqObj.httpMeth())print("Req from ALB:",reqObj.isAlb())print("Cookies:",reqObj.getCookies())print("Query params by input list:",reqObj.getQueryParams(['a']))print("Query params all:",reqObj.getAllQueryParam())print("Base64 encooded:",reqObj.isBase64())When the request is sent to lambda function this request object object is created and we can use this to get all the information that we need like cookies, query parameters, headers etc from this.Response## response.pyimportpy_lambda.responseasResrespObj=Res.Response()respObj=Res.Response()print("respObj:",respObj())respObj.setError()print("respObj:",respObj())respObj.setResp()print("respObj:",respObj())respObj.setResp(respBody={'a':1,'b':2})print("respObj:",respObj())# respObj.setContentType("application/json")# print("respObj:", respObj())respObj.setHeader("Access-Control-Allow-Origin","*")print("respObj:",respObj())Loab-Balancer requer the response of the lambda function in a specific format. This response object is created when the function is invoked and this will be changed accordingly to send the reponse intended.Lambda Function## lambda_function.pyimportpy_lambda.test_LambdaHandleraslTestimportpy_lambda.lambda_functionaslfRESOURCE_PREFIX=""lTest.EVENT_FROM_ALB["path"]=RESOURCE_PREFIX+'/abc'lTest.EVENT_FROM_ALB["httpMethod"]="GET"resp=lf.lambda_handler(event=lTest.EVENT_FROM_ALB,context="some_context")print(resp)This will give you the reponse if you have a mapping inlambda_mapping.py. body in the response will contain whatever you set in the response. Even you can set the header and the response code accordingly usingpy_lambda.response.setRespandpy_lambda.response.setHeaderTesting## test_LambdaHandler.pyimportpy_lambda.test_LambdaHandleraslTestprint(lTest.testOptions())This contain all the testing function that are used to build this.