AWS SNS 一次发送多封电子邮件

时间:2021-03-31 14:53:28

标签: python amazon-web-services aws-lambda amazon-sns

我在我的 lambda 函数上设置了 SNS 电子邮件 SNS,它将 AWS 凭证发送到我订阅的电子邮件中。

但是,当我运行 lambda 函数时,我一次收到了多次相同的电子邮件,我附上了下面的屏幕截图。我必须取消订阅才能让电子邮件停止向我的帐户发送垃圾邮件。

AWS notifications

下面是我得到的发送 SNS 电子邮件通知的 lambda 函数:

import boto3
import base64
import json 
from botocore.exceptions import ClientError


def lambda_handler(event, context):
    
    secret_name = "test/Secret"
    
    
    region_name = "eu-west-2"

    # Create a Secrets Manager client
    session = boto3.session.Session()
    client = session.client(
        service_name='secretsmanager',
        region_name=region_name
    )
    
    try:
        get_secret_value_response = client.get_secret_value(
            SecretId = secret_name
        )
    except ClientError as e:
        if e.response['Error']['Code'] == 'DecryptionFailureException':
            # Secrets Manager can't decrypt the protected secret text using the provided KMS key.
            # Deal with the exception here, and/or rethrow at your discretion.
            raise e
        elif e.response['Error']['Code'] == 'InternalServiceErrorException':
            # An error occurred on the server side.
            # Deal with the exception here, and/or rethrow at your discretion.
            raise e
        elif e.response['Error']['Code'] == 'InvalidParameterException':
            # You provided an invalid value for a parameter.
            # Deal with the exception here, and/or rethrow at your discretion.
            raise e
        elif e.response['Error']['Code'] == 'InvalidRequestException':
            # You provided a parameter value that is not valid for the current state of the resource.
            # Deal with the exception here, and/or rethrow at your discretion.
            raise e
        elif e.response['Error']['Code'] == 'ResourceNotFoundException':
            # We can't find the resource that you asked for.
            # Deal with the exception here, and/or rethrow at your discretion.
            raise e
    # else:
    #     # Decrypts secret using the associated KMS CMK.
    #     # Depending on whether the secret is a string or binary, one of these fields will be populated.
    #     if 'SecretString' in get_secret_value_response:
    #         secret = get_secret_value_response['SecretString']
    #     else:
    #         decoded_binary_secret = base64.b64decode(get_secret_value_response['SecretBinary'])
    
    else:
        # Decrypts secret using the associated KMS CMK.
        # Depending on whether the secret is a string or binary, one of these fields will be populated.
        if 'SecretString' in get_secret_value_response:
            secret = get_secret_value_response['SecretString']
        else:
            secret = base64.b64decode(get_secret_value_response['SecretBinary'])
            
     # Send message to SNS
    MY_SNS_TOPIC_ARN = 'arn:aws:sns:TOPIC_ARN'
    sns_client = boto3.client('sns')
    sns_client.publish(
        TopicArn = MY_SNS_TOPIC_ARN,
        Message = 'Your Credentians are: ' + secret
    )        

    return json.loads(secret)  # returns the secret as dictionary
    

关于如何解决垃圾邮件并在调用 lambda 时只收到 1 封电子邮件的任何解决方案?

0 个答案:

没有答案