"""Notifications utility functions.""" import datetime import json import boto3 import botocore from lambdacommon.common_config import logger import config sqs_client = boto3.client('sqs', region_name=config.AWS_REGION) def send_message(jira_issue, user, account_id): """ Send an SQS message with details about the break-glass session. Args: jira_issue (str): Jira issue for break glass session user (str): the user that broke glass account_id (str): ID of the AWS account Returns: str: a message id """ # Format today to match one used by Athena/Glue for audit log partitioning today = datetime.datetime.today().strftime('%Y/%m/%d') now = datetime.datetime.now().strftime('%Y-%m-%d-%H-%M-%S') message_body = json.dumps({ 'jira_issue': jira_issue, 'date': today, 'user': user, 'account_id': account_id, 'datetime': now, }) try: queue = sqs_client.get_queue_url(QueueName=config.SQS_QUEUE_NAME) response = sqs_client.send_message( QueueUrl=queue['QueueUrl'], MessageBody=message_body, ) except botocore.exceptions.ClientError as error: logger.exception('Error sending SQS message: {}'.format( error.response['Error']['Message'])) raise error return response['MessageId']