Created
November 18, 2019 14:22
-
-
Save felipekm/13eb88d95515c65ea736c508fb2b316a to your computer and use it in GitHub Desktop.
start ec2 instance python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import boto3 | |
# defines ec2 | |
ec2 = boto3.resource('ec2') | |
# lambda handler | |
def lambda_handler(event, context): | |
# builds an array filter to get EC2 instances with TAG `LIGAR` | |
arrOffInstancesFilter = [ | |
{ | |
'Name': 'tag:HourToStart', | |
'Values': ['07'] | |
}, | |
{ | |
'Name': 'instance-state-name', | |
'Values': ['stopped'] | |
} | |
] | |
# gets filtered instances | |
offInstances = ec2.instances.filter(Filters=arrOffInstancesFilter) | |
# gets instance ID | |
OffInstancesIds = [instance.id for instance in offInstances] | |
# checks for stopped instances | |
if len(OffInstancesIds) > 0: | |
# start the instances | |
startingInstances = ec2.instances.filter(InstanceIds=OffInstancesIds).start() | |
print("Starting PLING EC2 instances:", str(startingInstances)) | |
else: | |
print("There is Ec2 instances to start") | |
return 'instances successfuly started' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment