Best Python code snippet using localstack_python
34072_lambda_api.py
Source:34072_lambda_api.py
...333def get_handler_file_from_name(handler_name, runtime=LAMBDA_RUNTIME_PYTHON27):334 # TODO: support Java Lambdas in the future335 file_ext = '.js' if runtime.startswith(LAMBDA_RUNTIME_NODEJS) else '.py'336 return '%s%s' % (handler_name.split('.')[0], file_ext)337def get_handler_function_from_name(handler_name, runtime=LAMBDA_RUNTIME_PYTHON27):338 # TODO: support Java Lambdas in the future339 return handler_name.split('.')[-1]340def error_response(msg, code=500, error_type='InternalFailure'):341 LOG.warning(msg)342 return aws_responses.flask_error_response(msg, code=code, error_type=error_type)343def run_lambda_executor(cmd, env_vars={}):344 process = run(cmd, async=True, stderr=subprocess.PIPE, outfile=subprocess.PIPE, env_vars=env_vars)345 return_code = process.wait()346 result = to_str(process.stdout.read())347 log_output = to_str(process.stderr.read())348 if return_code != 0:349 raise Exception('Lambda process returned error status code: %s. Output:\n%s' %350 (return_code, log_output))351 return result, log_output352def set_function_code(code, lambda_name):353 def generic_handler(event, context):354 raise Exception(('Unable to find executor for Lambda function "%s". ' +355 'Note that Node.js Lambdas currently require LAMBDA_EXECUTOR=docker') % lambda_name)356 lambda_handler = generic_handler357 lambda_cwd = None358 arn = func_arn(lambda_name)359 runtime = arn_to_lambda[arn].runtime360 handler_name = arn_to_lambda.get(arn).handler361 lambda_environment = arn_to_lambda.get(arn).envvars362 if not handler_name:363 handler_name = LAMBDA_DEFAULT_HANDLER364 handler_file = get_handler_file_from_name(handler_name, runtime=runtime)365 handler_function = get_handler_function_from_name(handler_name, runtime=runtime)366 if 'S3Bucket' in code:367 s3_client = aws_stack.connect_to_service('s3')368 bytes_io = BytesIO()369 try:370 s3_client.download_fileobj(code['S3Bucket'], code['S3Key'], bytes_io)371 zip_file_content = bytes_io.getvalue()372 except Exception as e:373 return error_response('Unable to fetch Lambda archive from S3: %s' % e, 404)374 elif 'ZipFile' in code:375 zip_file_content = code['ZipFile']376 zip_file_content = base64.b64decode(zip_file_content)377 else:378 return error_response('No valid Lambda archive specified.', 400)379 # save tmp file...
lambda_api.py
Source:lambda_api.py
...324def get_handler_file_from_name(handler_name, runtime=LAMBDA_RUNTIME_PYTHON27):325 # TODO: support Java Lambdas in the future326 file_ext = '.js' if runtime.startswith(LAMBDA_RUNTIME_NODEJS) else '.py'327 return '%s%s' % (handler_name.split('.')[0], file_ext)328def get_handler_function_from_name(handler_name, runtime=LAMBDA_RUNTIME_PYTHON27):329 # TODO: support Java Lambdas in the future330 return handler_name.split('.')[-1]331def error_response(msg, code=500, error_type='InternalFailure'):332 LOG.warning(msg)333 return aws_responses.flask_error_response(msg, code=code, error_type=error_type)334def run_lambda_executor(cmd, env_vars={}):335 process = run(cmd, async=True, stderr=subprocess.PIPE, outfile=subprocess.PIPE, env_vars=env_vars)336 return_code = process.wait()337 result = to_str(process.stdout.read())338 log_output = to_str(process.stderr.read())339 if return_code != 0:340 raise Exception('Lambda process returned error status code: %s. Output:\n%s' %341 (return_code, log_output))342 return result, log_output343def set_function_code(code, lambda_name):344 def generic_handler(event, context):345 raise Exception(('Unable to find executor for Lambda function "%s". ' +346 'Note that Node.js Lambdas currently require LAMBDA_EXECUTOR=docker') % lambda_name)347 lambda_handler = generic_handler348 lambda_cwd = None349 arn = func_arn(lambda_name)350 runtime = arn_to_lambda[arn].runtime351 handler_name = arn_to_lambda.get(arn).handler352 lambda_environment = arn_to_lambda.get(arn).envvars353 if not handler_name:354 handler_name = LAMBDA_DEFAULT_HANDLER355 handler_file = get_handler_file_from_name(handler_name, runtime=runtime)356 handler_function = get_handler_function_from_name(handler_name, runtime=runtime)357 if 'S3Bucket' in code:358 s3_client = aws_stack.connect_to_service('s3')359 bytes_io = BytesIO()360 try:361 s3_client.download_fileobj(code['S3Bucket'], code['S3Key'], bytes_io)362 zip_file_content = bytes_io.getvalue()363 except Exception as e:364 return error_response('Unable to fetch Lambda archive from S3: %s' % e, 404)365 elif 'ZipFile' in code:366 zip_file_content = code['ZipFile']367 zip_file_content = base64.b64decode(zip_file_content)368 else:369 return error_response('No valid Lambda archive specified.', 400)370 # save tmp file...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!