示例#1
0
def list_tables(args=None):
    """
    list all DynamoDB tables

    :param args:
    :return:
    """
    parse_arguments('aws.dynamodb.list-tables', args=args)

    # list tables
    resource = boto3.Session().resource('dynamodb')
    tables = [table.name for table in resource.tables.all()]

    print('\n'.join(sorted(tables)))
示例#2
0
def list_tables(args=None):
    """
    list all DynamoDB tables

    :param args:
    :return:
    """
    parse_arguments('aws.dynamodb.list-tables', args=args)

    # list tables
    resource = boto3.Session().resource('dynamodb')
    tables = [table.name for table in resource.tables.all()]

    print('\n'.join(sorted(tables)))
示例#3
0
def list_functions(args=None):
    """
    list all lambda functions

    :param args:
    :return:
    """
    parse_arguments('aws.lambda.list-functions', args=args)

    client = AWSSession().client('lambda')

    # list functions
    functions = _list_functions(client)

    for function in functions:
        print(function['FunctionName'])
示例#4
0
def delete_functions(args=None):
    """
    delete selected lambda functions

    :param args:
    :return:
    """
    xargs = parse_arguments('aws.lambda.delete-functions', args=args)

    client = AWSSession().client('lambda')

    # list functions
    functions = _list_functions(client)

    # delete functions
    for function in functions:
        function_name = function['FunctionName']

        # match regex
        if any(re.match(pattern, function_name) for pattern in xargs.name):
            print('deleting function:', function_name)

            client.delete_function(
                FunctionName=function_name
            )
示例#5
0
def add_permissions(args=None):
    """
    add lambda permission based on swagger document

    :param args:
    :return:
    """
    xargs = parse_arguments('aws.lambda.add-permissions', args=args)

    print("Add lambda permissions: %s" % (xargs.swagger,))

    # template rendering
    data = load(xargs.swagger)
    context = {}
    template.render(data, context=context)

    # add permissions
    client = AWSSession().client('lambda')

    for method in context['lambdas']:
        arn = 'arn:aws:execute-api:{region}:{account_id}:{rest_api_id}/{stage_name}/{method}{endpoint}'
        method.update(rest_api_id=xargs.rest_api_id, stage_name=xargs.stage_name)
        if method['method'] == 'X-AMAZON-APIGATEWAY-ANY-METHOD':
            method['method'] = '*'

        print("adding permission: {rest_api_id} {stage_name} {method} {endpoint} -> {function_name}".format(**method))

        client.add_permission(
            FunctionName=method['function_name'],
            StatementId=str(uuid.uuid4()),
            Action='lambda:InvokeFunction',
            Principal='apigateway.amazonaws.com',
            SourceArn=arn.format(**method)
        )
示例#6
0
def add_permissions(args=None):
    """
    add lambda permission based on swagger document

    :param args:
    :return:
    """
    xargs = parse_arguments('aws.lambda.add-permissions', args=args)

    print("Add lambda permissions: %s" % (xargs.swagger, ))

    # template rendering
    data = load(xargs.swagger)
    context = {}
    template.render(data, context=context)

    # add permissions
    client = AWSSession().client('lambda')

    for method in context['lambdas']:
        arn = 'arn:aws:execute-api:{region}:{account_id}:{rest_api_id}/{stage_name}/{method}{endpoint}'
        method.update(rest_api_id=xargs.rest_api_id,
                      stage_name=xargs.stage_name)
        if method['method'] == 'X-AMAZON-APIGATEWAY-ANY-METHOD':
            method['method'] = '*'

        print(
            "adding permission: {rest_api_id} {stage_name} {method} {endpoint} -> {function_name}"
            .format(**method))

        client.add_permission(FunctionName=method['function_name'],
                              StatementId=str(uuid.uuid4()),
                              Action='lambda:InvokeFunction',
                              Principal='apigateway.amazonaws.com',
                              SourceArn=arn.format(**method))
示例#7
0
def put_alias(args=None):
    """
    create or update lambda alias

    :param args:
    :return:
    """
    xargs = parse_arguments('aws.lambda.put-alias', args=args)

    description = "%s:%s, version %s" % (xargs.function_name, xargs.name,
                                         xargs.function_version)

    client = AWSSession().client('lambda')

    try:
        alias = client.get_alias(FunctionName=xargs.function_name,
                                 Name=xargs.name)
    except ClientError:
        alias = None

    if alias:
        print("Updating lambda alias:", description)

        client.update_alias(FunctionName=xargs.function_name,
                            Name=xargs.name,
                            FunctionVersion=xargs.function_version)

    else:
        print("Creating lambda alias:", description)

        client.create_alias(FunctionName=xargs.function_name,
                            Name=xargs.name,
                            FunctionVersion=xargs.function_version)
示例#8
0
def list_functions(args=None):
    """
    list all lambda functions

    :param args:
    :return:
    """
    parse_arguments('aws.lambda.list-functions', args=args)

    client = AWSSession().client('lambda')

    # list functions
    functions = _list_functions(client)

    for function in functions:
        print(function['FunctionName'])
示例#9
0
def get_stack_output(args=None):
    """
    get CloudFormation stack output

    :param args:
    :return:
    """
    xargs = parse_arguments('aws.cloudformation.get-stack-output', args=args)

    for o in stack_outputs(xargs.stack_name):
        if o['OutputKey'] == xargs.output_key:
            print(o['OutputValue'])
            return

    raise KeyError('Output value %s not found in %s stack' % (xargs.key, xargs.stack_name))
示例#10
0
def get_stack_output(args=None):
    """
    get CloudFormation stack output

    :param args:
    :return:
    """
    xargs = parse_arguments('aws.cloudformation.get-stack-output', args=args)

    for o in stack_outputs(xargs.stack_name):
        if o['OutputKey'] == xargs.output_key:
            print(o['OutputValue'])
            return

    raise KeyError('Output value %s not found in %s stack' %
                   (xargs.key, xargs.stack_name))
示例#11
0
def generate_swagger(args=None):
    """
    generate swagger based on YAML template

    :param args:
    :return:
    """
    xargs = parse_arguments('aws.apigateway.generate-swagger', args=args)

    print("Generating Swagger for API Gateway: %s -> %s" % (xargs.template, xargs.swagger))

    # template rendering
    defs = {k: v for k, v in getattr(xargs, 'def') or []}
    data = load(xargs.template)
    data = template.render(data, defs=defs)
    dump(data, xargs.swagger)
示例#12
0
def export_tables(args=None):
    """
    export selected tables from DynamoDB

    :param args:
    :return:
    """
    xargs = parse_arguments('aws.dynamodb.export-tables', args=args)

    # export tables
    resource = boto3.Session().resource('dynamodb')
    tables = resource.tables.all()

    for table in tables:
        if any(re.match(pattern, table.name) for pattern in xargs.name):
            export_table(table, xargs.directory)
示例#13
0
def export_tables(args=None):
    """
    export selected tables from DynamoDB

    :param args:
    :return:
    """
    xargs = parse_arguments('aws.dynamodb.export-tables', args=args)

    # export tables
    resource = boto3.Session().resource('dynamodb')
    tables = resource.tables.all()

    for table in tables:
        if any(re.match(pattern, table.name) for pattern in xargs.name):
            export_table(table, xargs.directory)
示例#14
0
def create_deployment(args=None):
    """
    create Gateway API deployment to a given stage

    :param args:
    :return:
    """
    xargs = parse_arguments('aws.apigateway.create-deployment', args=args)

    print("Deploying API Gateway to stage: %s" % (xargs.stage_name,))

    # put rest api definition
    AWSSession().client('apigateway').create_deployment(
        restApiId=xargs.rest_api_id,
        stageName=xargs.stage_name,
        description=xargs.description
    )
示例#15
0
def put_rest_api(args=None):
    """
    import swagger to Gateway API

    :param args:
    :return:
    """
    xargs = parse_arguments('aws.apigateway.put-rest-api', args=args)

    print("Importing Swagger to API Gateway: %s" % (xargs.swagger,))

    # read file
    with open(xargs.swagger) as f:
        body = f.read()

    # put rest api definition
    AWSSession().client('apigateway').put_rest_api(
        restApiId=xargs.rest_api_id,
        mode='overwrite',
        failOnWarnings=True,
        body=body
    )
示例#16
0
def put_alias(args=None):
    """
    create or update lambda alias

    :param args:
    :return:
    """
    xargs = parse_arguments('aws.lambda.put-alias', args=args)

    description = "%s:%s, version %s" % (xargs.function_name, xargs.name, xargs.function_version)

    client = AWSSession().client('lambda')

    try:
        alias = client.get_alias(
            FunctionName=xargs.function_name,
            Name=xargs.name
        )
    except ClientError:
        alias = None

    if alias:
        print("Updating lambda alias:", description)

        client.update_alias(
            FunctionName=xargs.function_name,
            Name=xargs.name,
            FunctionVersion=xargs.function_version
        )

    else:
        print("Creating lambda alias:", description)

        client.create_alias(
            FunctionName=xargs.function_name,
            Name=xargs.name,
            FunctionVersion=xargs.function_version
        )
示例#17
0
def delete_functions(args=None):
    """
    delete selected lambda functions

    :param args:
    :return:
    """
    xargs = parse_arguments('aws.lambda.delete-functions', args=args)

    client = AWSSession().client('lambda')

    # list functions
    functions = _list_functions(client)

    # delete functions
    for function in functions:
        function_name = function['FunctionName']

        # match regex
        if any(re.match(pattern, function_name) for pattern in xargs.name):
            print('deleting function:', function_name)

            client.delete_function(FunctionName=function_name)
示例#18
0
def main():
    xargs, xargv = cli.parse_arguments('main', parse_known_args=True)
    invoke(xargs.module, xargs.client, xargs.command, xargv)
示例#19
0
def main():
    xargs, xargv = cli.parse_arguments('main', parse_known_args=True)
    invoke(xargs.module, xargs.client, xargs.command, xargv)