How to stop getting Lambda Invocation output email from SNS whereas I want to get email with custom content

0

I was trying to setup EC2 start/stop/reboot email notification using EventBridge-Lambda-SNS. So when Cloudtrail will detect the API RunInstances/StopInstances/RebootInstances, that will match eventbridge rule condition which invokes lambda function to send a customized email body through sns topic.

Function Code:

import os
import json
import boto3

def lambda_handler(event, context):
        
        EventID = event['detail']['eventID']
        Account = event['account']
        Timestamp = event['time']
        Region = event['region']
        InstanceID = event['detail']['requestParameters']['instancesSet']['items'][0]['instanceId']
        EventName = event['detail']['eventName']
        SourceIP = event['detail']['sourceIPAddress']
        InitiatedBy = event['detail']['userIdentity']['arn']
        
        if EventName == 'StopInstances':
            msg_status = 'stopped'
        elif EventName == 'StartInstances':
            msg_status = 'started'
        elif EventName == 'TerminateInstances':
            msg_status = 'terminated'
            
        body = f'Hi Team, \n\nThis is to inform you that EC2 instance with {InstanceID} is {msg_status}.Please find below information. \n\nEventID = {EventID}, \nAccount = {Account}, \nTimestamp = {Timestamp}, \nRegion = {Region}, \nInstanceID = {InstanceID}, \nEventName = {EventName}, \nSourceIP = {SourceIP}, \nInitiatedBy = {InitiatedBy} \n\nRegards,\nCloud Team'
        
        sns_client = boto3.client('sns')
        snsarn = os.environ['snsarn']
        
        res = sns_client.publish(
            TopicArn = snsarn,
            Subject = f'Alert - {InstanceID} is {msg_status}',
            Message = str(body)
            )

Then I tested this function by starting/stopping instance and I got expected email body. But apart from this I am getting Lambda invocation output JSON content in email which I don't want.

Expected:

Hi Team,
    
This is to inform you that EC2 instance with i-0d8219ba97c61c428 is started. Please find below information.
    
EventID = 9b07afa3-222c-4632-9dc9-bfa145ddb573,
Account = 12345678910,
Timestamp = 2023-08-13T04:49:49Z,
Region = us-east-1,
InstanceID = i-0d8219ba97c61c428,
EventName = StartInstances,
SourceIP = <IP>,
InitiatedBy = arn:aws:iam::12345678910:root

Regards,
Cloud Team

Sample Invocation Output Unexpected:

I am getting another email of below looking content. Here I pasted sample one(copied from documentation), but yes exactly same json structure I receive which I don't want. Please help me how to stop getting this email.

{
    "version": "1.0",
    "timestamp": "2019-11-14T18:16:05.568Z",
    "requestContext": {
        "requestId": "e4b46cbf-b738-xmpl-8880-a18cdf61200e",
        "functionArn": "arn:aws:lambda:us-east-2:123456789012:function:my-function:$LATEST",
        "condition": "RetriesExhausted",
        "approximateInvokeCount": 3
    },
    "requestPayload": {
        "ORDER_IDS": [
            "9e07af03-ce31-4ff3-xmpl-36dce652cb4f",
            "637de236-e7b2-464e-xmpl-baf57f86bb53",
            "a81ddca6-2c35-45c7-xmpl-c3a03a31ed15"
        ]
    },
    "responseContext": {
        "statusCode": 200,
        "executedVersion": "$LATEST",
        "functionError": "Unhandled"
    },
    "responsePayload": {
        "errorMessage": "RequestId: e4b46cbf-b738-xmpl-8880-a18cdf61200e Process exited before completing request"
    }
}
2개 답변
4
수락된 답변

I hope you'd have already gone through this re:Post Knowledge Center Article, which explains very well in detail about how to get customized email notifications via eventbridge-sns.

If not, please take a look and see if it helps.

Do you see SNS as target in your lambda function, if you see that, I'd suggest you to remove that SNS from your lambda target and test it again.

Comment here if you have additional questions, happy to help.

Abhishek

profile pictureAWS
전문가
답변함 9달 전
profile pictureAWS
전문가
iBehr
검토됨 9달 전
profile pictureAWS
전문가
검토됨 9달 전
  • Do you see SNS as target in your lambda function, if you see that, I'd suggest you to remove that SNS from your lambda target and test it again. Comment here, how it goes. Happy to help.

0

I have checked already the article you provided. It's not like that I am not able to configure customized email, but I am getting Lambda invocation event content as well in separate email which I don't want to get.

My question was how to stop getting invocation content email. Request you to check my above python code and outputs as well. :) @secondabhi_aws

iamAni
답변함 9달 전
  • Do you see SNS as target in your lambda function, if you see that, I'd suggest you to remove that SNS from your lambda target and test it again. Comment here, how it goes. Happy to help.

  • Yes now it worked after removing sns from target. Also I got the root cause. I have mentioned sns arn in the script for which I am getting customized email. And as SNS is configured as a target, hence lambda event is being sent to sns directly. Thank you so much for your help :)

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인