Example #1
0
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()
Example #2
0
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)
Example #3
0
from base64 import b64decode
from botocore.exceptions import ClientError
from botocore.config import Config
from lambkin.aws import get_role_arn, get_event_rule_arn
from lambkin.aws import get_function_arn, get_region
from lambkin.runtime import get_sane_runtime, get_file_extension_for_runtime
from lambkin.runtime import get_language_name_for_runtime
from lambkin.template import render_template
from lambkin.ux import say
from lambkin.version import VERSION
from lambkin.virtualenv import create_virtualenv, run_in_virtualenv
from lambkin.zip import create_zip
import lambkin.metadata as metadata
from subprocess import check_output, CalledProcessError, STDOUT

lmbda = boto3.client('lambda', region_name=get_region())


@click.command(help='Make a new Lambda function from a basic template.')
@click.argument('function')
@click.option('--runtime',
              help='The language runtime to use. eg. "python2.7".')
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):