python - 在 zappa 中调度 lambda 函数以停止实例
问题描述
我在这里创建了一个烧瓶应用程序并使用 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 requirements.txt -t .
- 也只是一个小的改进将
ec2
连接移到lambda_handler
. - 启动和停止实例 boto3 docs start and stop instance
推荐阅读
- java - 如何通过复合键 ID 在 JPA 中找到对象
- sql - 根据 R Shiny App 中的用户输入创建动态 SQL 查询
- sql - 如何从 BigQuery 中存储的大型线串数据集中查找所有道路交叉口
- python - 如何在此新闻 api 语法中设置循环
- c++ - 使用 C++ 将 tesseract API 的 TessBaseAPI 初始化为 api 指针,给出错误
- javascript - 将 JavaScript 组件包含到 Blazor 组件中
- anaconda - Anaconda 报告名称“dateutil”未定义,但已安装 python-dateutil
- javascript - 使用 React 实现分支深度链接
- vba - 在 Access 中使用 VBA 函数返回查询值
- python - 使用 Python 重复写入文件时防止 SD 卡过早磨损