【问题标题】:Any event listener in aws-python once an instance is started启动实例后,aws-python 中的任何事件侦听器
【发布时间】:2019-11-13 20:42:21
【问题描述】:

我想启动一个 ec2 实例取决于其他 ec2 实例通过 lambda 函数的状态。例如,Webserver ec2 实例必须在数据库实例启动后启动。截至目前我正在使用时间组件的睡眠方法但是认为应该有一种方法可以满足我的要求,并且对于具有 1-2 分钟停机时间的产品仅启动 ec2 实例是不利的。

import boto3
import time
region = 'us-west-1'
db_instances = ['i-12345cb6de4f78g9h', 'i-08ce9b2d7eccf6d26']
web_instances = ['i-12345cb6de4f78h8g', 'i-08ce9b2d7eccf6548']
ec2 = boto3.client('ec2', region_name=region)

def lambda_handler(event, context):
    ec2.start_instances(InstanceIds=db_instances)
    print('started your instances: ' + str(db_instances))
    time.sleep(60)
    ec2.start_instances(InstanceIds=web_instances)
    print('started your instances: ' + str(web_instances))  

欢迎提出建议,谢谢。

【问题讨论】:

    标签: python amazon-ec2 aws-lambda


    【解决方案1】:
    1. 查看实例states

      db_1 = ec2.Instance(id1).state
      db_2 = ec2.Instance(id2).state
      
    2. 设置一些标志,仅当两个实例都在运行时才变为真

      check_flag = True
      if db_1 == 'running' and db_2 == 'running':
          check_flag = False
      
    3. 两者结合,添加web启动sn-p

      check_flag = True
      while check_flag:
          db_1 = ec2.Instance(id1).state
          db_2 = ec2.Instance(id2).state
          if db_1 == 'running' and db_2 == 'running':
              check_flag = False
      ec2.start_instances(InstanceIds=web_instances)
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-07
      • 1970-01-01
      • 2013-06-27
      • 2016-07-24
      • 2018-04-05
      • 2017-08-11
      • 2018-09-04
      相关资源
      最近更新 更多