def lambda_handler()

in shd-notifier/Health-Event-Poller-LambdaFn.py [0:0]


def lambda_handler(event, context): 

  # Load the AWS Health API
  health= boto3.client('health', region_name='us-east-1')
  # Build the filter
  event_filter = {"eventStatusCodes": [eventStatusCodes],"eventTypeCategories": [ eventTypeCategories ]}
  if len(REGION_FILTER)>0:
    event_filter['regions']=REGION_FILTER
  # Poll the open events
  events_dict= health.describe_events(
    filter=event_filter,
    maxResults=maxEvents
    )
  open_issues=events_dict['events']
  if (len(open_issues)==0):
    print("No open issues detected.") # nothing to see here...
    logger.info("No open issues detected.")
  else:
    logger.info("Number of open issues: %s" % (len(open_issues)))
    # load the step state machine API
    stepClient = boto3.client('stepfunctions')
    # for every open issue, lets execute a state machine
    for issue in open_issues:
      # Skip events that are not PUBLIC events that appear on the SHD (i.e. Account specific events)
      if issue['eventScopeCode'] != 'PUBLIC':
        logger.info("Non-public issue not on SHD, skipping: %s" % (issue['arn']))
        continue
      logger.info("Starting Step Function for issue: %s" % (issue['arn']))
      # Execute state machine pass in the issues ARN and the WAIT_TIME
      input_str="{\"eventArn\":\"%s\",\"maxCount\": %i}" % (issue['arn'],WAIT_TIME)
      logger.debug("SFN Arn: %s" % (stateMachineArn))
      logger.debug("SFN input: %s" % (input_str))
      # extract the eventID field within the name size limit
      eventID=trimArnToName(issue['arn'])
      # ok lets fire up the state machine
      try:
        response = stepClient.start_execution(
          stateMachineArn=stateMachineArn,
          name=eventID,
          input= input_str
          )
      except ClientError as e:
        if e.response['Error']['Code'] == 'ExecutionAlreadyExists':
          # Duplicate Event ID's will be ignored since they were handled.
          logger.info("Event already executed named: %s" % (eventID))
          if (DEBUG): 
            logger.debug("DEBUG: Duplicate event detected for: %s" % (eventID))
            break # Only run one issue in debug, even already exists
          continue
        else:
          # we were unable to start the SFN, which is a severe error
          print(e)
          message= 'ERROR: Unable to start state machine'
          print(message)
          raise Exception(message)
      if (DEBUG): break # Only run one issue in debug