def test_setup_logging(): """ Ensure we can setup several logging options without an exception. This is kind of a lame test, but rather important as we usually call this function on the import of dozens of modules. """ from logging import getLogger, StreamHandler from io import StringIO log = getLogger('foo') log.addHandler(StreamHandler(StringIO())) tools.setup_logging('foo') tools.setup_logging('bar', level='DEBUG')
# -*- coding: utf-8 -*- # Copyright (c) 2016-present, CloudZero, Inc. All rights reserved. # Licensed under the BSD-style license. See LICENSE file in the project root for full license information. """ Various constructs used to make it easier to use AWS Lambda functions. """ import boto3 import pyfaaster.aws.tools as tools logger = tools.setup_logging('pyfaaster') class LambdaNotFoundException(Exception): pass class LambdaInvokeException(Exception): def __init__(self, messages, boto_error) -> None: super().__init__(messages, boto_error) self.inner_error = boto_error def lambda_invoke(namespace, base_func_name, func_prefix='', payload=bytes(), run_async=False, lambda_client=boto3.client('lambda')): """ Invoke a lambda function
import os import boto3 import pyfaaster.aws.tools as tools logger = tools.setup_logging('motolicious') def table(namespace): name = f'motolicious-{namespace}-test-table' dynamodb_endpointurl = os.environ.get('DYNAMODB_ENDPOINTURL') if dynamodb_endpointurl: logger.debug(f'Connecting to {name} table at {dynamodb_endpointurl}') return boto3.resource('dynamodb', endpoint_url=dynamodb_endpointurl).Table(name) else: logger.debug(f'Connecting to {name} table using default endpoint') return boto3.resource('dynamodb').Table(name) def set_value(table, key, value): logger.debug(f'Saving {value} for {key} in {table.name}') try: policy = table.update_item( Key={'key': key}, UpdateExpression=f'SET #name = :type_value', ExpressionAttributeNames={'#name': 'value'}, ExpressionAttributeValues={':type_value': value}, ReturnValues='ALL_NEW') logger.debug(f'Saved record to {table.name}') return policy