首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用Linux服务健康检查启动EC2实例

使用Linux服务健康检查启动EC2实例
EN

Stack Overflow用户
提问于 2021-07-24 02:04:20
回答 1查看 59关注 0票数 1

我是DevOps的初学者,也是编程新手。我被分配了一项任务,要以特定的顺序自动启动一组实例。在启动下一个服务之前检查其Linux服务的运行状况。

我发现了一个可以作为lambda函数运行的自动停止和启动python脚本,但我不知道如何按顺序启动实例并检查服务器服务的运行状况。

如果有什么可以帮助我或指导我如何做到这一点,我将不胜感激。

谢谢

代码语言:javascript
运行
复制
import boto3
import request
import time
region = 'region'
instances = ['']
ec2 = boto3.client('ec2', region_name=region)


def Ec2Instance1(ec2start):
    ec2.start_instances(InstanceIds=instances)
    print('started your instances: ' + str(instances))

    def lambda_handler(event, context):
    websiteURL = ['https://example1.com','https://example2.com','https://example3.com']
    topicArnCode = 'arn:aws:sns:ap-southeast-1:123:sample'
    
    for x in websiteURL: 
        print (x)
        r = requests.get(x,verify=False)
        print (r)
        if r.status_code == 200:
            Ec2Instance1()
            time.sleep(10)
        elif r.status_code == 200:
            Ec2Instance1()
        else:
            sns_client = boto3.client('sns')
            sns_client.publish(
            TopicArn = topicArnCode,
            Subject = 'Website is not reachable ' + x,
            Message = 'Website: ' + x + ' is down\n')
            print('Website is dead')    
EN

Stack Overflow用户

发布于 2021-07-28 01:57:30

代码语言:javascript
运行
复制
import boto3
import requests
import time

AWS_Access_Key_ID = 
AWS_Secret_Access_Key = 

DELAY_TIME=10 # 10 Seconds

region = 'us-east-2'
# instances = ['']

instances = {
  'instance id': 'http://link',
  'instance id': 'http://link'
  
}

ec2 = None

try:
  ec2 = boto3.client('ec2', aws_access_key_id=AWS_Access_Key_ID, aws_secret_access_key=AWS_Secret_Access_Key, region_name=region)
  # ec2 = boto3.resource('ec2',aws_access_key_id=AWS_Access_Key_ID, aws_secret_access_key=AWS_Secret_Access_Key, region_name=region)
except Exception as e:
  print(e)
  print("AWS CREDS ERROR, Exiting...")
  exit()

def startInstances(instancesIds):
  if(type(instancesIds) != list):  
    instancesIds = [instancesIds]

  try:
    response = ec2.start_instances(InstanceIds=instancesIds, DryRun=False)
    print(response)
    print("Instances Started")
  except ClientError as e:
    print(e)
    print("Instances Failed to Start")

def stopInstances(instancesIds):
  if(type(instancesIds) != list):  
    instancesIds = [instancesIds
    ]
  try:
    response = ec2.stop_instances(InstanceIds=instancesIds, DryRun=False)
    print(response)
    print("Instances Stopped")
  except ClientError as e:
    print(e)
    print("Instances Failed to Stop")

def check():
  for x in instances:
    retry = 0
    live = False

    print("Checking Webiste " + instances[x])

    while(retry < 5):
      try:
        r = requests.get(instances[x] ,verify=True)
        if(r.status_code == 200):
          live = True
        break
      except: 
        print("Not Live, retry time " + str(retry + 1))
        print("Delaying request for " + str(DELAY_TIME) + " seconds...")
        retry += 1
        time.sleep(DELAY_TIME)

    if(live):
      print("Website is live")
      # call function  to start the ec2 instance
      startInstances(x)
    else:
      # call function to stop the ec2 instance
      print('Website is dead') 
      stopInstances(x)   
    print("")

def main():
  check()

if __name__ == '__main__':
  main()
票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68503426

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档