Lambda demo functions for Server Stop/Start/Status
This commit is contained in:
parent
3436132866
commit
8f62d9bf83
5 changed files with 106 additions and 1 deletions
|
|
@ -0,0 +1,31 @@
|
|||
```python
|
||||
#Get Uptime
|
||||
import boto3
|
||||
import time
|
||||
import datetime
|
||||
region = 'eu-central-1'
|
||||
instances = ['i-0de7cce6fcdf8c0e6']
|
||||
ec2 = boto3.resource('ec2')
|
||||
print("Executing script")
|
||||
|
||||
def lambda_handler(event, context):
|
||||
for instance in ec2.instances.all():
|
||||
if instance.id =="i-0de7cce6fcdf8c0e6":
|
||||
state = instance.state["Name"]
|
||||
if state != "running":
|
||||
return {
|
||||
'state' : state,
|
||||
'uptime': '-'
|
||||
}
|
||||
else:
|
||||
launch_time = (instance.launch_time).strftime("%Y-%m-%d %H:%M:%S")
|
||||
launch_time = datetime.datetime.strptime(launch_time, "%Y-%m-%d %H:%M:%S")
|
||||
current_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
current_time = datetime.datetime.strptime(current_time, "%Y-%m-%d %H:%M:%S")
|
||||
uptime = (current_time - launch_time)
|
||||
threshold = datetime.timedelta(minutes = 2)
|
||||
return {
|
||||
'state' : state,
|
||||
'uptime': str(uptime)
|
||||
}
|
||||
```
|
||||
Loading…
Add table
Add a link
Reference in a new issue