aws-samples / ecs-cid-sample

In this code provided with the blog, we will demonstrate how to use the draining state to update the AMI used by EC2 instances in your cluster by updating the launch configuration of your auto-scaling group. The process also ensures that the EC2 instances get the tasks drained the tasks launch on new container instance before termination occurs.
Apache License 2.0
103 stars 76 forks source link

paging snsClient.list_subscriptions() #10

Open ghost opened 7 years ago

ghost commented 7 years ago

snsClient.list_subscriptions() seems to be returning 6 endpoints and paging 7+ to next page. workarround as follow:

@@ -163,14 +163,17 @@ def lambda_handler(event, context):

             # If tasks are still running...
             if tasksRunning == 1:
-                response = snsClient.list_subscriptions()
-                for key in response['Subscriptions']:
-                    logger.info("Endpoint %s AND TopicArn %s and protocol %s ",key['Endpoint'], key['TopicArn'],
-                                                                                  key['Protocol'])
-                    if TopicArn == key['TopicArn'] and key['Protocol'] == 'lambda':
-                        logger.info("TopicArn match, publishToSNS function...")
-                        msgResponse = publishToSNS(message, key['TopicArn'])
-                        logger.debug("msgResponse %s and time is %s",msgResponse, datetime.datetime)
+                paginator = snsClient.get_paginator('list_subscriptions')
+                subscriptionListPages = paginator.paginate()
+                for subscriptionListResp in subscriptionListPages:
+                    #response = snsClient.list_subscriptions()
+                    for key in subscriptionListResp['Subscriptions']:
+                        logger.info("Endpoint %s AND TopicArn %s and protocol %s ",key['Endpoint'], key['TopicArn'],
+                                                                                      key['Protocol'])
+                        if TopicArn == key['TopicArn'] and key['Protocol'] == 'lambda':
+                            logger.info("TopicArn match, publishToSNS function...")
+                            msgResponse = publishToSNS(message, key['TopicArn'])
+                            logger.debug("msgResponse %s and time is %s",msgResponse, datetime.datetime)
             # If tasks are NOT running...
             elif tasksRunning == 0:
                 completeHook = 1
fabianfett commented 7 years ago

Well, I think the client should use list_subscriptions_by_topic. There is no point in having this lambda retrieve all topics to filter then for its own topic.

http://boto3.readthedocs.io/en/latest/reference/services/sns.html#SNS.Client.list_subscriptions_by_topic

UPDATE: Oh, there is a pr for this already: #5. Why hasn't this been merged yet?

jhmartin commented 6 years ago

What is the point of fetching the list of subscriptions, when it already knows the TopicArn?