forked from odanrezende/aws-lambda-start-stop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda-stop-instances.py
More file actions
37 lines (28 loc) · 1018 Bytes
/
lambda-stop-instances.py
File metadata and controls
37 lines (28 loc) · 1018 Bytes
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
35
import boto3
#define ec2
ec2 = boto3.resource('ec2')
def lambda_handler(event, context):
# definir a procura por instâncias RUNNING e com a TAG ambiente=DESLIGAR
filters = [{
'Name': 'tag:DESLIGAR',
'Values': ['19HORAS']
},
{
'Name': 'instance-state-name',
'Values': ['running']
}
]
#busca as instâncias
instances = ec2.instances.filter(Filters=filters)
#Pega o ID da instância
RunningInstances = [instance.id for instance in instances]
#Log para testar quais instâncias foram impactadas
print(RunningInstances)
#verificar se existem instâncias de TAG: DESLIGAR executando
if len(RunningInstances) > 0:
#perform the shutdown
shuttingDown = ec2.instances.filter(InstanceIds=RunningInstances).stop()
print("Stoppando as instâncias")
else:
print("Não foram encontradas instâncias em RUNNING")
return 'Olá, seu comando rodou corretamente!'