Search code examples
amazon-web-servicesaws-lambdaamazon-sqsamazon-ses

Send email when message being pushed to AWS Simple Queue Service


I am handling multiple log files and I have put a piece of code when something goes wrong and at the same time, I am pushing a message into AWS SQS that contains details of the error. Now I want to send an email to myself as soon as a message is pushed into SQS. Earlier I was trying to do using SES. I put SES email sending code but SQS was not playing role in it. What can I do to implement my use case?

except: 
            #this part runs for any corrupted file
            sqs_msg_response = queue_client.send_message(MessageBody='Data Processing num_failed')
            print('messageID',sqs_msg_response.get('MessageId'))
            subject = 'Data file is not processed'
            body='''the {}file is not processed at the time{}'''.format(string,current_time)
            message = {"Subject":{"Data":subject},"Body":{"Html":{"Data":body}}}
            email_response = email_client.send_email(
            Source="xyz@gmail.com",
            Destination={
                "ToAddresses":[
                    "abc@gmail.com",
                    ]
                },
            Message=message
            )

Above I am implemented but SQS is independent and SES is independent. Now I want to implement as soon as message being pushed to SQS email being sent to the provided email id.


Solution

  • A regular pattern to solve this kind of issue is using a combination of Amazon SQS and Amazon SNS.

    1. Create an SNS topic
    2. Add the SQS queue as a target to the topic
    3. Add an email subscription as a second target to the topic
    4. Modify your application to put messages on the topic instead of the queue

    This way, the topic puts each message on the queue and sends an email at the same time.