def create_lambda_helper(awsclient, lambda_name, role_arn, handler_filename, lambda_handler='handler.handle', folders_from_file=None, **kwargs): """ NOTE: caller needs to clean up both lambda! :param awsclient: :param lambda_name: :param role_arn: :param handler_filename: :param lambda_handler: :param folders_from_file: :param kwargs: additional kwargs are used in deploy_lambda :return: """ from gcdt_bundler.bundler import get_zipped_file settings_requirements() lambda_description = 'lambda created for unittesting ramuda deployment' timeout = 300 memory_size = 128 if not folders_from_file: folders_from_file = [{ 'source': './vendored', 'target': '.' }, { 'source': './resources/sample_lambda/impl', 'target': 'impl' }] artifact_bucket = None zipfile = get_zipped_file( handler_filename, folders_from_file, ) # create the AWS Lambda function deploy_lambda(awsclient=awsclient, function_name=lambda_name, role=role_arn, handler_filename=handler_filename, handler_function=lambda_handler, folders=folders_from_file, description=lambda_description, timeout=timeout, memory=memory_size, artifact_bucket=artifact_bucket, zipfile=zipfile, **kwargs) # TODO better use waiter for that! time.sleep(10)
def create_lambda_helper(awsclient, lambda_name, role_arn, handler_filename, lambda_handler='handler.handle'): # caller needs to clean up both lambda! ''' role = _create_role( role_name, policies=[ 'arn:aws:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole', 'arn:aws:iam::aws:policy/AWSLambdaExecute'] ) role_arn = role['Arn'] ''' # prepare ./vendored folder and settings file settings_requirements() lambda_description = 'lambda created for unittesting ramuda deployment' # lambda_handler = 'handler.handle' timeout = 300 memory_size = 128 folders_from_file = [{ 'source': './vendored', 'target': '.' }, { 'source': './resources/sample_lambda/impl', 'target': 'impl' }] artifact_bucket = None zipfile = _get_zipped_file( #awsclient, handler_filename, folders_from_file, #runtime=runtime, #settings=settings ) # create the function deploy_lambda(awsclient=awsclient, function_name=lambda_name, role=role_arn, handler_filename=handler_filename, handler_function=lambda_handler, folders=folders_from_file, description=lambda_description, timeout=timeout, memory=memory_size, artifact_bucket=artifact_bucket, zipfile=zipfile) time.sleep(10)
def test_create_lambda(awsclient, vendored_folder, cleanup_lambdas, cleanup_roles): log.info('running test_create_lambda') temp_string = helpers.random_string() lambda_name = 'jenkins_test_' + temp_string log.info(lambda_name) role = create_role_helper( awsclient, 'unittest_%s_lambda' % temp_string, policies=[ 'arn:aws:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole', 'arn:aws:iam::aws:policy/AWSLambdaExecute' ]) cleanup_roles.append(role['RoleName']) config = { "lambda": { "name": "dp-dev-sample-lambda-jobr1", "description": "lambda test for ramuda", "role": "'unused'", "handlerFunction": "handler.handle", "handlerFile": "./resources/sample_lambda/handler.py", "timeout": 300, "memorySize": 256, "events": { "s3Sources": [{ "bucket": "jobr-test", "type": "s3:ObjectCreated:*", "suffix": ".gz" }], "timeSchedules": [{ "ruleName": "infra-dev-sample-lambda-jobr-T1", "ruleDescription": "run every 5 min from 0-5", "scheduleExpression": "cron(0/5 0-5 ? * * *)" }, { "ruleName": "infra-dev-sample-lambda-jobr-T2", "ruleDescription": "run every 5 min from 8-23:59", "scheduleExpression": "cron(0/5 8-23:59 ? * * *)" }] }, "vpc": { "subnetIds": [ "subnet-d5ffb0b1", "subnet-d5ffb0b1", "subnet-d5ffb0b1", "subnet-e9db9f9f" ], "securityGroups": ["sg-660dd700"] } }, "bundling": { "zip": "bundle.zip", "folders": [{ "source": "./vendored", "target": "." }, { "source": "./impl", "target": "impl" }] }, "deployment": { "region": "eu-west-1" } } lambda_description = config['lambda'].get('description') # print (role) role_arn = role['Arn'] lambda_handler = config['lambda'].get('handlerFunction') handler_filename = config['lambda'].get('handlerFile') timeout = int(config['lambda'].get('timeout')) memory_size = int(config['lambda'].get('memorySize')) zip_name = config['bundling'].get('zip') folders_from_file = config['bundling'].get('folders') subnet_ids = config['lambda'].get('vpc', {}).get('subnetIds', None) security_groups = config['lambda'].get('vpc', {}).get('securityGroups', None) region = config['deployment'].get('region') artifact_bucket = config['deployment'].get('artifactBucket', None) zipfile = _get_zipped_file( handler_filename, folders_from_file, ) deploy_lambda(awsclient=awsclient, function_name=lambda_name, role=role_arn, handler_filename=handler_filename, handler_function=lambda_handler, folders=folders_from_file, description=lambda_description, timeout=timeout, memory=memory_size, artifact_bucket=artifact_bucket, zipfile=zipfile) # TODO improve this (by using a waiter??) cleanup_lambdas.append(lambda_name)