我是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')
发布于 2021-07-28 01:57:30
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()
https://stackoverflow.com/questions/68503426
复制相似问题