Question: How can I use a Lambda function to receive SNS alerts when an AWS Glue job fails a retry?

I want to be notified by an Amazon Simple Notification Service (Amazon SNS) alert when my AWS Glue job fails a retry.

Short descriptionAmazon EventBridge events for AWS Glue can be used to create Amazon SNS alerts, but the alerts might not be specific enough for certain situations. To receive SNS notifications for certain AWS Glue Events, such as an AWS Glue job failing on retry, you can use AWS Lambda. You can create a Lambda function to do the following:Check the incoming event for a specific string.Publish a message to Amazon SNS if the string in the event matches the string in the Lambda function.To use an AWS Lambda function to receive an email from SNS when any of your AWS Glue jobs fail a retry, do the following:Create an Amazon SNS topic.Create an AWS Lambda function.Create an Amazon EventBridge event that uses the Lambda function to initiate email notifications.

ResolutionBe sure that you have:An AWS Glue extract, transform, and load (ETL) jobAn AWS Identity and Access Management (IAM) role for AWS Lambda with permission to publish SNS NotificationsCreate an Amazon SNS topicOpen the Amazon SNS Console.Choose Topics, and then Choose Create topic.For Type, select Standard.For Name, enter the topic name.(Optional) For Display name, enter the display name for your topic.Choose Create topic.Your topic is created.Choose Create subscription.For Topic ARN, select the topic that you created.For Protocol, select your desired protocol.For Endpoint, enter the address where you want to receive the SNS notifications.Choose Create subscription.Your subscription is created.Create an AWS Lambda function1.    Open the Lambda console.2.    Choose Create function.3.    On the Create function page, do the following:Select Author from scratch.For Function name, enter a name for your function.For Runtime, select one of the Python options (For script compatibility, it's a best practice to use Python 3.7).Expand the Change default execution role dropdown list.For Execution role, select Use an existing role.For Existing role, select the IAM role with permission to send SNS Notifications.4.    Choose Create function.Your Lambda function is created.5.    On the Code tab, in the Code source section, choose File, and then choose New file.In the new file, enter code similar to the following:# Import modulesimport jsonimport loggingimport boto3# Set up logginglogger = logging.getLogger()logger.setLevel(logging.INFO)# Set up Boto3 client for SNSclient = boto3.client('sns')# Variables for the SNS:SNS_TOPIC_ARN = "arn:aws:sns:us-east-1:012345678901:Glue_Job_Failure_Notification"# Define Lambda functiondef lambda_handler(event, context):    logger.info(f"## INITIATED BY EVENT: \n{event['detail']}")    # Define variables based on the event    glue_job_name = event['detail']['jobName']    jobrun_id = event['detail']['jobRunId']    # Only send SNS notification if the event pattern contains _attempt_1    if event['detail']['jobRunId'].endswith('_attempt_1'):        logger.info(f'## GLUE JOB FAILED RETRY: {glue_job_name}')        message = "A Glue Job has failed after attempting to retry. "\            + f"JobName: {glue_job_name}, JobRunID: {jobrun_id}"        logger.info(message)        client.publish(            TargetArn=SNS_TOPIC_ARN,            Message=json.dumps({'default': json.dumps(message)}),            Subject='An AWS Glue Job has failed',            MessageStructure='json')Note: Be sure to replace snsTopicARN with the ARN of your SNS Topic.Choose File, and then choose Save.For Filename, enter the file name of your choice.6.    Choose Deploy.(Optional) You can test your event by doing the following:1.    Choose the Test tab.For Name, enter the event name. Enter JSON similar to the following:{    "version": "0",    "id": "abcdef01-1234-5678-9abc-def012345678",    "detail-type": "Glue Job State Change",    "source": "aws.glue",    "account": "123456789012",    "time": "2017-09-07T06:02:03Z",    "region": "us-west-2",    "resources": [],    "detail": {        "jobName": "MyTestJob",        "severity": "ERROR",        "state": "FAILED",        "jobRunId": "jr_0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef_attempt_1",        "message": "JobName:MyTestJob and JobRunId:jr_0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef failed to execute with exception Role arn:aws:iam::123456789012:role/Glue_Role should be given assume role permissions for Glue Service."    }}Note: Be sure to replace MyTestJob with the name of your AWS Glue job.2.    Choose Save changes.3.    Choose Test.4.    View the Execution result that opens up after the test is complete.5.    Confirm that you receive an SNS notification.Use Amazon EventBridge to initiate email notifications1.    Open the EventBridge Console.2.    On the navigation pane, choose Rules, and then choose Create rule.3.    On the Create rule page, do the following:For Name, enter the rule name.(Optional) For Description - optional, enter the description of the rule.For Define pattern, select Event pattern.For Event matching pattern, select Custom pattern.For Event pattern, enter the following pattern, or a pattern of your choice:{  "detail-type": [    "Glue Job State Change"  ],  "source": [    "aws.glue"  ],  "detail": {    "state": [      "FAILED"    ]  }}Choose Save.On the Select targets section, do the following:For Target, Choose Lambda functionFor Function, choose the function that you've created.4.    Choose Create.Test the notification with your AWS Glue jobOpen the AWS Glue Console.On the navigation pane, choose Jobs.Select the AWS Glue Job where you want to test the notification.Choose the Action dropdown list, and then choose Edit job.Expand Security configuration, script libraries, and job parameters (optional).Under Security configuration, for Number of retries, enter 1.Choose Save.On the Jobs page, select the AWS Glue job where you want to test the notification.Choose the Action dropdown list, and then choose Edit Script.Change an element of your code so that your job fails. ( Example: Add the word "_BROKEN" to a table name).Choose Save.On the Jobs page, select the AWS Glue job where you want to test the notification.Choose the Action dropdown list, and then Choose Run job.You should receive a notification on the second failed attempt.After the testing is complete, edit your AWS Glue job and undo the changes.

Related informationSetting up Amazon SNS notificationsTroubleshooting Amazon EventBridge