首页 > 解决方案 > 在 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

标签: pythonflaskamazon-ec2aws-lambdazappa

解决方案


  • 值得检查 zip 文件的创建方式,还要检查上传的 zip 是否格式正确。
  • lambda_hanlder 应该位于 zip 根目录的底部。
  • 用户pip install -r requirements.txt -t .
  • 也只是一个小的改进将ec2连接移到lambda_handler.
  • 启动和停止实例 boto3 docs start and stop instance

推荐阅读