Skip to content

Instantly share code, notes, and snippets.

@bo67192
Created November 3, 2016 23:22
Show Gist options
  • Save bo67192/ed4617b19217a3f903dc0161524dcd91 to your computer and use it in GitHub Desktop.
Save bo67192/ed4617b19217a3f903dc0161524dcd91 to your computer and use it in GitHub Desktop.
Python example of using boto3 to filter instances and print their names
import boto3
# Create boto3 ec2 resource
ec2 = boto3.resource('ec2')
# Retrieve instances with a name tag that contains Webserver
filters = [{'Name':'tag:instanceSchedule', 'Values': ['*Webserver*']}]
instances = ec2.instances.filter(Filters=filters)
# For each insteance with a tag
for i in instances:
for tag in i.tags:
if tag['Key'] == 'Name':
instanceName = tag['Value']
print(instanceName)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment