-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.py
34 lines (24 loc) · 956 Bytes
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import boto3
import json
ec2 = boto3.resource('ec2', 'eu-west-3')
def lambda_handler(event, context):
filters = [
{'Name': 'tag:Name', 'Values': ['auto stop']},
{'Name': 'instance-state-name', 'Values': ['running']}
]
instance_collection = ec2.instances.filter(Filters=filters)
# Retrieve instance IDs
instance_id = [instance.id for instance in instance_collection]
if(event['requestContext']['path'] == '/stop'):
ec2.instances.filter(
Filters=[{'Name': 'instance-id', 'Values': instance_id}]).stop()
return {
'statusCode': 200,
'body': json.dumps("stopping "+str(instance_id)+"...")
}
elif(event['requestContext']['path'] == '/tags'):
instance_tags = [instance.tags for instance in instance_collection]
return {
'statusCode': 200,
'body': json.dumps(instance_tags, indent=4, sort_keys=True)
}