blog-templates/central.yaml [174:223]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
              sns_client = boto3.client('sns')
              response_to_sns = {
              "perms_to_set" : record
              }
              logger.info('record  --->  {} '.format(record))
              logger.info('sending event to sns --->  {} '.format(response_to_sns))
              response = sns_client.publish(
                          TopicArn='arn:aws:sns:{}:{}:lakeformation-automation'.format(os.environ['REGION'],
                                                                                              os.environ['ACCOUNT_ID']),
                          Message= json.dumps(response_to_sns),
                          MessageStructure='string',
                          MessageAttributes={
                                      'account_id': {
                                          'DataType': 'String',
                                          'StringValue': str(record['AccountID'])
                                      }
                                  }
              )
              logger.info('response from sns --->  {} '.format(response))
              return response

          def lambda_handler(event, context):
              app = os.environ['PREFIX']
              env = os.environ['ENV']
              acc_id = os.environ['ACCOUNT_ID']
              region = os.environ['REGION']

              arn_pattern = '^arn:(?P<Partition>[^:\n]*):(?P<Service>[^:\n]*):(?P<Region>[^:\n]*):(?P<AccountID>[^:\n]*):(?P<Ignore>(?P<ResourceType>[^:\/\n]*)[:\/])?(?P<Resource>.*)$'
              arn_regex = re.compile(arn_pattern)

              try:
                  logger.info('Received {} messages'.format(len(event['Records'])))
                  logger.info('messages {}'.format(event))
                  for record in event['Records']:
                      event_body = json.loads(record['body'])['Records'][0]
                      message = parse_s3_event(event_body)
                      s3_content = read_s3_content(message['bucket'], message['key'])
                      for perm_record in s3_content['Records']:
                          regex_obj = arn_regex.match(perm_record['Principal'])
                          if perm_record['AccessType'] == 'grant':
                              if regex_obj.group(4) != acc_id:
                                  response = publish_sns(generate_db_perm(perm_record))
                                  if response['ResponseMetadata']['HTTPStatusCode'] == 200:
                                      logger.info('DB Perm Record Published to sns {}'.format(s3_content))
                                      time.sleep(3)
                          response = publish_sns(perm_record)
                          logger.info('response of actual perm block -- {}'.format(response))
                      logger.info('Processing Permissions for perm json started --> {} '.format(s3_content))
              except Exception as e:
                  raise e
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



src/lakeformation_automation/lambda_function.py [118:168]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    sns_client = boto3.client('sns')
    response_to_sns = {
    "perms_to_set" : record
    }
    logger.info('record  --->  {} '.format(record))
    logger.info('sending event to sns --->  {} '.format(response_to_sns))
    response = sns_client.publish(
                TopicArn='arn:aws:sns:{}:{}:lakeformation-automation'.format(os.environ['REGION'],
                                                                                    os.environ['ACCOUNT_ID']),
                Message= json.dumps(response_to_sns),
                MessageStructure='string',
                MessageAttributes={
                            'account_id': {
                                'DataType': 'String',
                                'StringValue': str(record['AccountID'])
                            }
                        }
    )
    logger.info('response from sns --->  {} '.format(response))
    return response
    
    
def lambda_handler(event, context):
    app = os.environ['PREFIX']
    env = os.environ['ENV']
    acc_id = os.environ['ACCOUNT_ID']
    region = os.environ['REGION']

    arn_pattern = '^arn:(?P<Partition>[^:\n]*):(?P<Service>[^:\n]*):(?P<Region>[^:\n]*):(?P<AccountID>[^:\n]*):(?P<Ignore>(?P<ResourceType>[^:\/\n]*)[:\/])?(?P<Resource>.*)$'
    arn_regex = re.compile(arn_pattern)
    
    try:
        logger.info('Received {} messages'.format(len(event['Records'])))
        logger.info('messages {}'.format(event))
        for record in event['Records']:
            event_body = json.loads(record['body'])['Records'][0]
            message = parse_s3_event(event_body)
            s3_content = read_s3_content(message['bucket'], message['key'])
            for perm_record in s3_content['Records']:
                regex_obj = arn_regex.match(perm_record['Principal'])
                if perm_record['AccessType'] == 'grant':
                    if regex_obj.group(4) != acc_id:
                        response = publish_sns(generate_db_perm(perm_record))
                        if response['ResponseMetadata']['HTTPStatusCode'] == 200:
                            logger.info('DB Perm Record Published to sns {}'.format(s3_content))
                            time.sleep(3)
                response = publish_sns(perm_record)
                logger.info('response of actual perm block -- {}'.format(response))
            logger.info('Processing Permissions for perm json started --> {} '.format(s3_content))
    except Exception as e:
        raise e
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



