Last active
July 5, 2018 15:33
-
-
Save daniel-woods/a028d1ddb5881f1eb13b1a7fb9fd8677 to your computer and use it in GitHub Desktop.
Count the number of mobile endpoints in an SNS Application. Useful for counting the endpoints in an application where the number is greater than 100,000, the AWS console's current limit.
This file contains 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/python | |
# Script to count the number of endpoints in an SNS platform application | |
# By calling list_ednpoints using pagination, append the response size to a counter. | |
# Count the number of endpoints that are enabled. | |
import boto3 | |
import time | |
start_time = time.time() | |
sns = boto3.client("sns") | |
platform_arn = "YOUR_SNS_APPLICATION_PLATFORM_ARN" | |
# First call does not need a NextToken | |
resp = sns.list_endpoints_by_platform_application(PlatformApplicationArn=platform_arn) | |
count = len(resp['Endpoints']) | |
enabled_count = 0 | |
try: | |
while True: | |
for endpoint in resp['Endpoints']: | |
if endpoint['Attributes']['Enabled'] == "true": | |
enabled_count += 1 | |
# For the Last request, "NextToken" will not be included, | |
# which will throw an exception. We use this to exit the loop. | |
resp = sns.list_endpoints_by_platform_application( | |
PlatformApplicationArn=platform_arn, | |
NextToken=resp['NextToken']) | |
count += len(resp['Endpoints']) | |
except Exception as e: | |
pass | |
finally: | |
print("Total Number of Endpoints counted: " + str(count)) | |
print("Total Number of Endpoints enabled: " + str(enabled_count)) | |
print("Percentage of Endpoints enabled: " + str((enabled_count / count) * 100)) + "%" | |
print("Query time: %s seconds" % (time.time() - start_time)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment