def stackset_processing()

in functions/source/stackset/stackset.py [0:0]


def stackset_processing(messages):
    cloudFormationClient = session.client('cloudformation')
    snsClient = session.client('sns')
    newRelicStackSNS = os.environ['newRelicStackSNS']
    newRelicRegisterSNS = os.environ['newRelicRegisterSNS']
    
    for stackSetName, params in messages.items():
        logger.info("Processing stack instances for {}".format(stackSetName))
        param_accounts = params['target_accounts']
        param_regions = params['target_regions']
        logger.info("Target accounts : {}".format(param_accounts))
        logger.info("Target regions: {}".format(param_regions))
        
        try:
            stack_operations = True
            cloudFormationClient.describe_stack_set(StackSetName=stackSetName)
            cloudFormationPaginator = cloudFormationClient.get_paginator('list_stack_set_operations')
            stackset_iterator = cloudFormationPaginator.paginate(
                StackSetName=stackSetName
            )
            for page in stackset_iterator:
                if 'Summaries' in page:
                    for operation in page['Summaries']:
                        if operation['Status'] in ('RUNNING', 'STOPPING'):
                            stack_operations = False
                            break
                    if stack_operations == False: 
                        break
            
            if stack_operations:
                response = cloudFormationClient.create_stack_instances(StackSetName=stackSetName, Accounts=param_accounts, Regions=param_regions)
                logger.info("StackSet instance created {}".format(response))
                messageBody = {}
                messageBody[stackSetName] = {'OperationId': response['OperationId']}
                try:
                    snsResponse = snsClient.publish(
                        TopicArn=newRelicRegisterSNS,
                        Message = json.dumps(messageBody))
                        
                    logger.info("Queued for registration: {}".format(snsResponse))
                except Exception as snsException:
                    logger.error("Failed to send queue for registration: {}".format(snsException))                
            else:
                logger.warning("Existing StackSet operations still running")
                messageBody = {}
                messageBody[stackSetName] = messages[stackSetName]
                try:
                    logger.info("Sleep and wait for 20 seconds")
                    time.sleep(20)
                    snsResponse = snsClient.publish(
                        TopicArn=newRelicStackSNS,
                        Message = json.dumps(messageBody))
                        
                    logger.info("Re-queued for stackset instance creation: {}".format(snsResponse))
                except Exception as snsException:
                    logger.error("Failed to send queue for stackset instance creation: {}".format(snsException))

        except cloudFormationClient.exceptions.StackSetNotFoundException as describeException:
            logger.error('Exception getting stack set, {}'.format(describeException))
            raise describeException