コード例 #1
0
def handler(event, context):
    ''' Invoked when AWS Lambda service executes. '''

    if not __is_valid_event(event):
        return custom_resource_response.failure_response(
            'Malformed event recieved.')

    request_type = event['RequestType']

    if request_type != 'Create':
        print(
            'Saw RequestType: \"{}\". No action needed (Only \"Create\" supported)'
            .format(request_type))
        return custom_resource_response.success_response({}, '*')

    s3_client = s3.get_client()
    lambda_client = lambda_.get_client()

    bucket_name, lambda_arn = __get_resources(event)

    has_permission = __add_permission_to_trigger(lambda_client, lambda_arn,
                                                 bucket_name)

    if not has_permission:
        return custom_resource_response.failure_response(
            'Could not add permissions to Lambda')

    is_configured = __add_notification_configuration(bucket_name, lambda_arn,
                                                     s3_client)

    if is_configured:
        return custom_resource_response.success_response({}, '*')
    else:
        return custom_resource_response.failure_response(
            'Could not succesfully configure AttachmentBucket')
コード例 #2
0
ファイル: Custom_SafeTable.py プロジェクト: bmk10/LYBook_1.19
def handler(event, context):
    dynamodb = aws_utils.ClientWrapper(boto3.client('dynamodb'))
    wait_for_account_tables()

    request_type = event['RequestType']
    table_name = get_table_name(event)

    if request_type == 'Create':
        try:
            if table_name in gather_tables(dynamodb):
                raise RuntimeError(
                    "Trying to create a Custom::DynamoDB::Table custom resource, but DynamoDB table already exists!"
                )
            try:
                response = create_table(table_name, event)
            except Exception as e:
                if isinstance(e,
                              ClientError) and e.response['Error']['Code'] in [
                                  'LimitExceededException'
                              ]:
                    wait_for_account_tables()
                    response = create_table(table_name, event)
                else:
                    raise e
            table_response = _TableResponse(response)
        except RuntimeError as e:
            return custom_resource_response.failure_response(e.message)
        tag_table(dynamodb, response, event)
    elif request_type == 'Update':
        try:
            if not table_name in gather_tables(dynamodb):
                try:
                    response = create_table(table_name, event)
                except Exception as e:
                    if isinstance(
                            e,
                            ClientError) and e.response['Error']['Code'] in [
                                'LimitExceededException'
                            ]:
                        wait_for_account_tables()
                        response = create_table(table_name, event)
                    else:
                        raise e
                table_response = _TableResponse(response)
                tag_table(dynamodb, response, event)
            else:
                try:
                    response = update_table(table_name, event)
                except Exception as e:
                    if isinstance(
                            e,
                            ClientError) and e.response['Error']['Code'] in [
                                'LimitExceededException'
                            ]:
                        wait_for_account_tables()
                        response = update_table(table_name, event)
                    else:
                        raise e
                table_response = _TableResponse(response)
        except RuntimeError as e:
            return custom_resource_response.failure_response(e.message)

    elif request_type == 'Delete':
        try:
            if table_name in gather_tables(dynamodb):
                try:
                    response = dynamodb.delete_table(TableName=table_name)
                except Exception as e:
                    if isinstance(
                            e,
                            ClientError) and e.response['Error']['Code'] in [
                                'LimitExceededException'
                            ]:
                        wait_for_account_tables()
                        response = dynamodb.delete_table(TableName=table_name)
                    else:
                        raise e
                table_response = _TableResponse(response)
            else:
                print "Custom::DynamoDB::Table is trying to delete a DynamoDB table that does not exist"
                table_response = _TableResponse(
                    {'TableDescription': {
                        'TableName': table_name
                    }})
        except RuntimeError as e:
            return custom_resource_response.failure_response(e.message)
    else:
        raise RuntimeError('Invalid RequestType: {}'.format(request_type))

    return custom_resource_response.success_response(table_response.output,
                                                     table_response.table_name)