gpt4 book ai didi

python - 在 zappa 中调度 lambda 函数来停止实例

转载 作者:太空宇宙 更新时间:2023-11-03 20:23:37 26 4
gpt4 key购买 nike

在这里,我创建了一个 Flask 应用程序并使用 zappa 进行部署。

在部署时,我遇到没有模块发现异常,但它在下面离线工作的相同Python代码是我的stop.app应用程序

import boto3
from flask import Flask, request,Response, jsonify

app = Flask(__name__)
logging.basicConfig()
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
region = 'ap-south-1'
myins = ['i-043ae2fbfc26d423f','i-0df3f5ead69c6428c','i-0bac8502574c0cf1d','i-02e866c4c922f1e27']
@app.route('/', methods=['GET'])
def lambda_handler(event=None, context=None):
logger.info('Lambda function invoked index()')
ec2 = boto3.resource('ec2')
ec2client = boto3.client('ec2',region_name=region)
ec2client.stop_instances(InstanceIds=myins)
return 'Instances are stopped!!'
# if __name__ == '__main__':
# app.run(debug=True)```




Below is the error


alling tail for stage dev..
[1568791437587] No module named stop: ImportError
Traceback (most recent call last):
File "/var/task/handler.py", line 602, in lambda_handler
return LambdaHandler.lambda_handler(event, context)
File "/var/task/handler.py", line 245, in lambda_handler
handler = cls()
File "/var/task/handler.py", line 139, in __init__
self.app_module = importlib.import_module(self.settings.APP_MODULE)
File "/usr/lib64/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
ImportError: No module named stop

最佳答案

  • 值得检查 zip 文件的创建方式,还要检查上传的 zip 格式是否正确。
  • lambda_hanlder 应位于 zip 根目录的底部。
  • 用户pip install -r requests.txt -t .
  • 此外,只需将 ec2 连接移至 lambda_handler 之外即可。
  • 启动和停止实例 boto3 文档 start and stop instance

关于python - 在 zappa 中调度 lambda 函数来停止实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57989286/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com