Skip to content

Instantly share code, notes, and snippets.

@blackpioter
Last active May 1, 2025 10:49
Show Gist options
  • Select an option

  • Save blackpioter/6d6bfd6158fbf46a73559040848c3a58 to your computer and use it in GitHub Desktop.

Select an option

Save blackpioter/6d6bfd6158fbf46a73559040848c3a58 to your computer and use it in GitHub Desktop.
boto3_list_instances_env_role
#!/usr/bin/env python
import boto3
def list_instances_env_role(env, role):
"""
List of instances with tag:Environment and tag:Role
"""
ec2client = boto3.client('ec2')
filter_env = {'Name': 'tag:Environment', 'Values': [env]}
filter_role = {'Name': 'tag:Role', 'Values': [role]}
response = ec2client.describe_instances(Filters=[filter_env, filter_role])
instancelist = []
for reservation in (response["Reservations"]):
for instance in reservation["Instances"]:
instancelist.append(instance["InstanceId"])
return instancelist
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment