Last active
May 1, 2025 10:49
-
-
Save blackpioter/6d6bfd6158fbf46a73559040848c3a58 to your computer and use it in GitHub Desktop.
boto3_list_instances_env_role
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
| #!/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