forked from odanrezende/aws-lambda-start-stop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda-start-instances.py
More file actions
35 lines (28 loc) · 1007 Bytes
/
lambda-start-instances.py
File metadata and controls
35 lines (28 loc) · 1007 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 STOPPED e com a TAG ambiente=LIGAR
filters = [{
'Name': 'tag:LIGAR',
'Values': ['07HORAS']
},
{
'Name': 'instance-state-name',
'Values': ['stopped']
}
]
#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 com a TAG: LIGAR desligadas
if len(RunningInstances) > 0:
#perform the shutdown
shuttingDown = ec2.instances.filter(InstanceIds=RunningInstances).start()
print("Startando as instâncias")
else:
print("Não foram encontradas instâncias STOPPED")
return 'Olá, seu código rodou corretamente'