Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save seeker815/252570bb479a58f2d650ee6162e6b11e to your computer and use it in GitHub Desktop.
Save seeker815/252570bb479a58f2d650ee6162e6b11e to your computer and use it in GitHub Desktop.
Python - start stopped or stop running instances with particular tag
import boto3
tagname = "Name"
tagvalue = "Value"
ec2 = boto3.resource('ec2')
ec2.instances.filter(Filters=[
{'Name': 'tag': [tagname], 'Values': [tagvalue]},
{'Name': 'instance-state-name', 'Values': ['running']}
]).start()
ec2.instances.filter(Filters=[
{'Name': 'tag': [tagname], 'Values': [tagvalue]},
{'Name': 'instance-state-name', 'Values': ['stopped']}
]).stop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment