SNS Publish Timeout when using Lambda

0

I am trying to send a email using SNS after connecting and executing a mysql query. My code works until I add the sns publish to my script. When this is done the Lambda function times out. I have tried increasing timeout, verified that the execution role has sns:publish assigned and sent a email from the SNS Console successfully.

Here is my code snippet for the push to SNS.

import sys
import logging
import pymysql
import json
import os
import boto3
import calendar
import time

username = 'removed'
password = 'removed'
rds_proxy_host = 'removed'
db_name = 'classicmodels'
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)



try:
    conn = pymysql.connect(host=rds_proxy_host, user=username, passwd=password, db=db_name, connect_timeout=5)
except pymysql.MySQLError as e:
    logger.error("ERROR: Unexpected error: Could not connect to MySQL instance.")
    logger.error(e)
    sys.exit(1)
logger.info("SUCCESS: Connection to RDS for MySQL instance succeeded")

def lambda_handler(event, context):
    
    snsclient = boto3.client('sns')
    snsarn = ('removed')
    
    snsclient.publish(TopicArn=snsarn,Message='s3TriggeredRefresh Demo has successfully executed')
    print("Message published")
    return {"statusCode": 200,"body": "Function Completed Successfully!!"}
3回答
1
承認された回答

Hello.

Will your Lambda connect to resources (RDS or EC2) within your VPC?
In that case, you need to create a NAT Gateway or Amazon SNS VPC endpoint in the VPC linked to Lambda and configure it to communicate with the Amazon SNS service.
When creating a NAT gateway, add a route for the NAT gateway to the route table of the subnet associated with Lambda.
https://repost.aws/knowledge-center/internet-access-lambda-function
https://docs.aws.amazon.com/sns/latest/dg/sns-vpc-create-endpoint.html

profile picture
エキスパート
回答済み 3ヶ月前
profile picture
エキスパート
レビュー済み 2ヶ月前
0

I did not know that this was a requirement. My Lambda can publish to the SNS topic if it is the only service used. Is there a reason why that works without the end point?

Monty
回答済み 3ヶ月前
0

Thanks @Riku for your direction. I added the SNS VPC Endpoint and my SNS publish action is working again.

Monty
回答済み 3ヶ月前

ログインしていません。 ログイン 回答を投稿する。

優れた回答とは、質問に明確に答え、建設的なフィードバックを提供し、質問者の専門分野におけるスキルの向上を促すものです。

質問に答えるためのガイドライン

関連するコンテンツ