【问题标题】:Starting EC2 instance with Linux services health check使用 Linux 服务运行状况检查启动 EC2 实例
【发布时间】:2021-09-30 19:58:00
【问题描述】:

我是 DevOps 的初学者和编程的菜鸟。我被分配了一个任务来自动启动具有特定顺序的一组实例。在启动下一个服务之前检查其 Linux 服务的运行状况。

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

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

谢谢

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')    

【问题讨论】:

  • 您如何定义“检查其 Linux 服务的健康状况”?

标签: python linux amazon-ec2 aws-lambda devops


【解决方案1】:
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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-19
    • 2019-08-15
    • 2021-07-09
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多