使用AWS Lambda fn订阅亚马逊SNS主题,即SNS.Subscribe()



我想向主题发送SNS订阅请求以进行电子邮件订阅,由于我是Lambda的新手,你能告诉我需要导入哪些库以及需要触发哪些函数吗。

提前谢谢。

  1. 导入Boto3包

  2. 创建对SNS Boto 的引用

  3. 创建SNS主题

  4. 为该SNS主题创建一个电子邮件发件人。(确保你遵循python中的缩进。这是一个正在运行的代码(

    import boto3
    snsClient =  boto3.client('sns')
    def lambda_handler(event, context):
    topicName = "myFirstTopic"
    emailId= "yourname@company.com"
    #creating Topic and if it already created with the specified name, that topic's ARN is returned without creating a new topic..
    snsArn = createSNSTopic(topicName)   
    #crearting a subscriber for the specified SNS Toic
    snsEndpointSusbcribe(snsArn,emailId)
    
    #Function to create SNS Topic 
    def createSNSTopic(topicName):
    try:
    response = snsClient.create_topic(
    Name=topicName
    )
    print(response['TopicArn'])
    return response['TopicArn']
    except Exception as ex:
    print(ex)
    #Function to create SNS Subscriber for that Topic
    def snsEndpointSusbcribe(snsArn,emailId):
    response = snsClient.subscribe(
    #
    TopicArn=snsArn,
    Protocol='email',
    Endpoint=emailId,
    )
    

最新更新