#python #amazon-web-services #aws-lambda #chalice
Вопрос:
У меня есть проект чаши (1.26.2), который всегда завершается с тайм-аутом при развертывании, локальная чаша работает нормально.
Это моя структура проекта:
Это код в моем app.py:
from datetime import datetime from decimal import Decimal import boto3 import firebase_admin from chalice import Chalice, AuthResponse from chalice.app import AuthRequest from firebase_admin import credentials, auth from chalicelib import crud from chalicelib import models from chalicelib import schemas from chalicelib.auth import full_user_from_context, user_from_context from chalicelib.db import SessionLocal, engine app = Chalice(app_name='server-aws') BUCKET = 'meet-app' s3_client = boto3.client('s3') cred = credentials.Certificate('chalicelib/serviceAccountKey.json') firebase_admin.initialize_app(cred) models.Base.metadata.create_all(bind=engine) DISTANCE_IN_METERS = 100 _DB = None def get_db(): global _DB if _DB is None: _DB = SessionLocal() return _DB @app.lambda_function(name='test-function') def create_user(event, context): return {'hello': 'world'} @app.route('/health') def health(): return {'status': 'ok'} @app.authorizer() def token_authorizer(auth_request: AuthRequest) -gt; AuthResponse: token = auth_request.token try: decoded_token = auth.verify_id_token(token) decoded = decoded_token allowed_routes = [ '/auth', '/me', ] if 'permission_verified' in decoded and decoded['permission_verified'] is True: allowed_routes.append('/me/location') allowed_routes.append('/nearby') allowed_routes.append('/me/profile-image') print('routes', allowed_routes) return AuthResponse(routes=allowed_routes, principal_id=decoded['sub'], context=decoded) except Exception as e: print('error', e) return AuthResponse(routes=[], principal_id='non-user') @app.route('/auth', methods=['GET'], authorizer=token_authorizer) def authorize(): u = user_from_context(app.current_request.context) user = crud.get_user(get_db(), u['uid']) if user is None: user = crud.create_user(get_db(), schemas.UserCreate( uid=u['uid'], phone=u['phone_number'], permission_verified=True # TODO: find verification method )) token = auth.create_custom_token(user.uid, { "permission_verified": user.permission_verified, "uid": user.uid, "phone": user.phone, "name": user.name, "linkedin": user.linkedin, "instagram": user.instagram, }) return { 'user': user.__json__(), 'token': token.decode() } @app.route('/me', methods=["PUT"], authorizer=token_authorizer) def update_me(): r = app.current_request u = full_user_from_context(r.context) data = r.json_body u = crud.update_user(get_db(), schemas.UserUpdate( uid=u.uid, name=data.get('name'), phone=data.get('phone'), instagram=data.get('instagram'), linkedin=data.get('linkedin'), )) if u is None: # todo: code return { "error": "could not update" } return { "user": u.__json__() } @app.route('/me/profile-image', methods=["PUT"], content_types=['application/octet-stream'], authorizer=token_authorizer) def update_me(): r = app.current_request u = full_user_from_context(r.context) data = r.raw_body file_name = u.uid tmp_file_name = '/tmp/' file_name '.jpg' with open(tmp_file_name, 'wb') as tmp_file: tmp_file.write(data) key = 'profile-images/' file_name try: s3_client.upload_file(tmp_file_name, BUCKET, key, ExtraArgs={'ACL': 'public-read'}) except Exception as e: app.log.error(e) return { "error": str(e) } url = f'https://{BUCKET}.s3.amazonaws.com/{key}' u = crud.update_user(get_db(), schemas.UserUpdate( uid=u.uid, profile_image_url=url )) return { "url": url } @app.route('/me/location', methods=["PUT"], authorizer=token_authorizer) def update_me_location(): r = app.current_request u = full_user_from_context(r.context) data = r.json_body lat = Decimal(str(data.get('latitude'))) lng = Decimal(str(data.get('longitude'))) loc = crud.update_user_location(get_db(), schemas.UserLocationUpdate( uid=u.uid, latitude=lat, longitude=lng, timestamp=datetime.now(), geo=f'POINT({lng} {lat})' )) if loc is None: loc = crud.create_user_location(get_db(), schemas.UserLocationCreate( uid=u.uid, latitude=lat, longitude=lng )) loc = schemas.UserLocationGet( uid=u.uid, user=schemas.UserGet( uid=u.uid, name=u.name, linkedin=u.linkedin, instagram=u.instagram, profile_image_url=u.profile_image_url ), latitude=loc.latitude, longitude=loc.longitude, timestamp=loc.timestamp.isoformat() ) return { 'loc': loc.json() } @app.route('/nearby', methods=["GET"], authorizer=token_authorizer) def nearby(): r = app.current_request u = full_user_from_context(r.context) user_location = crud.get_user_location(get_db(), u.uid) if user_location is None: return { "error": "no user location" } # todo: better errro nearby_users = crud.get_nearby_users(get_db(), u.uid, schemas.UserLocationGet( uid=user_location.user_id, latitude=user_location.latitude, longitude=user_location.longitude, user=u, timestamp=user_location.timestamp.isoformat() ), DISTANCE_IN_METERS) return { "nearby_distance_in_meters": DISTANCE_IN_METERS, "nearby_users": list(map(lambda x: schemas.UserLocationGet( uid=x.user_id, latitude=x.latitude, longitude=x.longitude, timestamp=x.timestamp.isoformat(), user=schemas.UserGet( uid=x.user.uid, name=x.user.name, instagram=x.user.instagram, linkedin=x.user.linkedin, profile_image_url=x.user.profile_image_url ) ).dict(), nearby_users)) }
Это ответ, когда я вызываю тестовую функцию из консоли AWS:
Это то, что я получаю, когда делаю журналы чаши:
Traceback (most recent call last):[ERROR] Runtime.ImportModuleError: Unable to import module 'app': No module named 'crud' Traceback (most recent call last):[ERROR] Runtime.ImportModuleError: Unable to import module 'app': No module named 'crud' 2021-11-14 19:29:04.197000 88eb68 2021-11-14T19:29:04.197Z c143dcd4-e212-41f1-a786-7d86e4b58e59 Task timed out after 60.06 seconds
Это мое requirements.txt:
chalice~=1.26.2 firebase-admin boto3~=1.18.53 botocore sqlalchemy pydantic # psycopg2 GeoAlchemy2 psycopg2-binary
Мне нужно использовать psycopg2, так что, возможно, в этом и проблема.
Каждый http — запрос приводит к 504 тайм-аутам.
В локальном режиме все работает нормально.
Заранее спасибо.
Ответ №1:
в папке вашего пакета __init__.py
chalicelib (она должна быть написана так) в конце есть дополнительное подчеркивание. Таким образом, система импорта python не распознает такую папку как пакет, отсюда и ошибка.