def create_zip(zip_file_path): if not zip_file_path: function = metadata.get('function') zip_file_path = '/tmp/lambkin-publish-%s.zip' % function zip_file = zipfile.ZipFile(zip_file_path, 'w', zipfile.ZIP_DEFLATED) for root, dirs, files in os.walk('.'): site_dir = os.path.join('.', 'venv', 'lib', 'python2.7', 'site-packages') dist_dir = os.path.join('.', 'venv', 'lib', 'python2.7', 'dist-packages') for f in files: path = os.path.join(root, f) if path.endswith('.pyc'): pass elif root.startswith(site_dir): # Then strip the library dir, and put the file in the zip. trimmed_path = path[len(site_dir):] zip_file.write(path, trimmed_path) elif root.startswith(dist_dir): # Then strip the library dir, and put the file in the zip. trimmed_path = path[len(dist_dir):] zip_file.write(path, trimmed_path) elif root.startswith('./venv') or root.startswith('./.git'): # Then it's other junk that we don't want. pass else: # Not sure what this is. The function author probably put # it here for a reason, so make sure it goes in the zip. zip_file.write(path, path) zip_file.close() return zip_file_path
def run(function): if not function: function = metadata.get('function') result = lmbda.invoke(FunctionName=function, LogType='Tail') log = b64decode(result['LogResult']).rstrip().split("\n") for line in log: say(line) print result['Payload'].read()
def run(function): lmbda = boto3.client('lambda', region_name=get_region(), config=Config(retries={'max_attempts': 1}, read_timeout=310)) if not function: function = metadata.get('function') result = lmbda.invoke(FunctionName=function, LogType='Tail') log = b64decode(result['LogResult']).rstrip().split("\n") for line in log: say(line) print result['Payload'].read()
def publish(description, timeout, memory, role, zip_file_only, zip_file_path): runtime = metadata.get('runtime') function = metadata.get('function') if description: metadata.update(description=description) else: try: description = metadata.get('description') except KeyError: raise ClickException( 'Please provide a description with "--description"') if timeout: metadata.update(timeout=timeout) else: timeout = metadata.get('timeout') if memory: metadata.update(memory=memory) else: memory = metadata.get('memory') if role: metadata.update(role=role) else: role = metadata.get('role') zip_data = open(create_zip(zip_file_path)).read() if zip_file_only: return if function in get_published_function_names(): # Push the latest code to the existing function in Lambda. update_code_response = lmbda.update_function_code( FunctionName=function, ZipFile=zip_data, Publish=True) # Update any settings for the function too. final_response = lmbda.update_function_configuration( FunctionName=function, Description=description, Role=get_role_arn(role), Timeout=timeout, MemorySize=memory) say('%s updated in Lambda' % function) else: # we need to explictly create the function in AWS. final_response = lmbda.create_function(FunctionName=function, Description=description, Runtime=runtime, Role=get_role_arn(role), Handler='%s.handler' % function, Code={'ZipFile': zip_data}, Timeout=timeout, MemorySize=memory) say('%s created in Lambda' % function) print json.dumps(final_response, sort_keys=True, indent=2)
def build(): runtime = metadata.get('runtime') language = get_language_name_for_runtime(runtime) if language == 'python': # Use virtualenv and pip if not os.path.isfile('venv'): create_virtualenv('.') print run_in_virtualenv('pip install -r requirements.txt') else: # Fall back to a Makefile. try: make_log = check_output(['make'], stderr=STDOUT) for line in make_log.rstrip().split("\n"): say(line) except CalledProcessError as e: for line in e.output.rstrip().split("\n"): say(line) raise ClickException('make failure')
def schedule(function, rate, cron): if not function: function = metadata.get('function') events = boto3.client('events', region_name=get_region()) if (rate and cron): raise ClickException( 'Please use either "--rate" or "--cron", not both.') if rate: schedule_expression = 'rate(%s)' % rate elif cron: schedule_expression = 'cron(%s)' % cron else: raise ClickException('Please provide "--rate" or "--cron".') try: lmbda.add_permission( FunctionName=function, StatementId='lambkin-allow-cloudwatch-invoke-%s' % function, Action='lambda:InvokeFunction', Principal='events.amazonaws.com', SourceArn=get_event_rule_arn('lambkin-cron-%s' % function), ) except ClientError as e: if e.response['Error']['Code'] == 'ResourceConflictException': # The rule is already there. Carry on! pass else: raise e events.put_rule( Name='lambkin-cron-%s' % function, ScheduleExpression=schedule_expression, State='ENABLED', Description='Lambkin cron for %s Lambda function' % function, ) response = events.put_targets(Rule='lambkin-cron-%s' % function, Targets=[{ 'Id': function, 'Arn': get_function_arn(function), }]) print json.dumps(response, sort_keys=True, indent=2)
def create(function, runtime): runtime = get_sane_runtime(runtime) ext = get_file_extension_for_runtime(runtime) func_dir = function func_file = os.path.join(func_dir, '%s.%s' % (function, ext)) for path in (func_dir, func_file): if os.path.exists(path): raise ClickException('Path "%s" already exists.' % path) os.mkdir(func_dir) template_name = get_language_name_for_runtime(runtime) render_template(template_name, function, output_filename="%s.%s" % (function, ext)) if get_language_name_for_runtime(runtime) == 'python': create_virtualenv(function) render_template('requirements', function, output_filename='requirements.txt') render_template('gitignore-python', function, output_filename='.gitignore') else: render_template('makefile', function, output_filename='Makefile') render_template('gitignore', function, output_filename='.gitignore') our_metadata = { 'function': function, 'runtime': runtime, 'language': get_language_name_for_runtime(runtime), 'timeout': metadata.get('timeout') } metadata.write(subdirectory=function, **our_metadata) say('%s created as %s' % (function, func_file))
def unpublish(function): if not function: function = metadata.get('function') lmbda.delete_function(FunctionName=function) say('%s unpublished' % (function))