Skip to content

Instantly share code, notes, and snippets.

@felipekm
Created November 18, 2019 14:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save felipekm/13eb88d95515c65ea736c508fb2b316a to your computer and use it in GitHub Desktop.
Save felipekm/13eb88d95515c65ea736c508fb2b316a to your computer and use it in GitHub Desktop.
start ec2 instance python
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