Created
April 22, 2019 12:27
-
-
Save daniel-woods/7ef91d4316fd94e1ea445e398f22ed20 to your computer and use it in GitHub Desktop.
Poll AWS APIs faster using multithreading
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 | |
| # Multi threading way to query AWS regional APIs. | |
| # A lot faster than synchronously polling each region one-after-another. | |
| import boto3 | |
| import threading | |
| from time import sleep | |
| logs = [] | |
| class myThread (threading.Thread): | |
| def __init__(self, threadID, region): | |
| threading.Thread.__init__(self) | |
| self.threadID = threadID | |
| self.region = region | |
| def run(self): | |
| print("Thread " + str(self.threadID) + ": Contacting region " + self.region) | |
| query_region(self.region, self.threadID) | |
| print("Thread " + str(self.threadID) + ": Thread task completed. Closing.") | |
| def query_region(region, id): | |
| conn = boto3.resource('ec2',region_name=region) | |
| instances = conn.instances.filter() | |
| for instance in instances: | |
| print("Thread " + str(id) + ": Response from region " + region + ": " + str(instance.id) + " " + instance.instance_type) | |
| def lambda_handler(event, context): | |
| # Use the filter() method of the instances collection to retrieve | |
| # all running EC2 instances. | |
| ec2 = boto3.client('ec2') | |
| ec2_regions = [region['RegionName'] for region in ec2.describe_regions()['Regions']] | |
| threads = [] | |
| for region in ec2_regions: | |
| threads.append(myThread(len(threads), region)) | |
| # Start all threads | |
| for thread in threads: | |
| thread.start() | |
| # Wait for all threads to finish | |
| for thread in threads: | |
| thread.join() | |
| if __name__ == "__main__": | |
| lambda_handler(0, 0) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment