Lambda-API网关:“消息”:“内部服务器错误”

时间:2019-07-01 21:19:59

标签: amazon-web-services api aws-lambda sam

我正在使用AWS CodeStar(Lambda + API网关)构建无服务器API。我的lambda函数在Lambda控制台中运行良好,但是当我在AWS CodeStar上运行代码时奇怪地抛出了此错误:

  

“消息”:“内部服务器错误”

请帮助我解决此问题。

import json
import os
import bz2
import pprint
import hashlib
import sqlite3
import re
from collections import namedtuple
from gzip import GzipFile
from io import BytesIO
from botocore.vendored import requests
import logging

logger = logging.getLogger()
logger.setLevel(logging.DEBUG)

def handler(event, context):

    logger.info('## ENVIRONMENT VARIABLES')
    logger.info(os.environ)
    logger.info('## EVENT')
    logger.info(event)



    n = get_package_list()
    n1 = str(n)

    dat = {"total_pack":n1}
    return {'statusCode': 200,
            'headers': {'Content-Type': 'application/json'},
            'body': json.dumps(dat)
    }


def get_package_list():

    url = "http://amazonlinux.us-east-2.amazonaws.com/2/core/2.0/x86_64/c60ceaf6dfa3bc10e730c9e803b51543250c8a12bb009af00e527a598394cd5e/repodata/primary.sqlite.gz"

    db_filename = "dbfile"


    resp = requests.get(url, stream=True)
    remote_data = resp.raw.read()

    cached_fh = BytesIO(remote_data)
    compressed_fh = GzipFile(fileobj=cached_fh)

    with open(os.path.join('/tmp',db_filename), "wb") as local_fh:
        local_fh.write(compressed_fh.read())

    package_obj_list = []

    db = sqlite3.connect(os.path.join('/tmp',db_filename))

    c = db.cursor()

    c.execute('SELECT name FROM packages')
    for package in c.fetchall():
        package_obj_list.append(package)


    no_of_packages = len(package_obj_list)

    return no_of_packages

预期结果:应返回一个整数(no_of_packages)。

0 个答案:

没有答案