OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

AWS Lambda Timeout to Amazon SES

  • Thread starter Thread starter Andre
  • Start date Start date
A

Andre

Guest
The code below to send an Amazon SES email works fine. However, I need the code to communicate with a local EC2 database, so I need to add this Lambda function to my VPC and Subnets - At this point, the code below stops working and timeouts.

How can I fix this?

Code:
import json
import boto3

def send_email_ses(email):
    client = boto3.client('ses', region_name='eu-west-1')
    
    try:
        response = client.send_email(
            Destination={
                'ToAddresses': [email]
            },
            Message={
                'Body': {
                    'Text': {
                        'Charset': 'UTF-8',
                        'Data': 'Hello world',
                    }
                },
                'Subject': {
                    'Charset': 'UTF-8',
                    'Data': 'Welcome! Your API Key',
                },
            },
            Source='[email protected]'
        )
        return response['MessageId']
    except Exception as e:
        print(f"An error occurred: {str(e)}")
        return None

def lambda_handler(event, context):
    email = "[email protected]"
    message_id = send_email_ses(email)
    
    if message_id:
        body = f"Email Sent Successfully. MessageId is: {message_id}"
        status_code = 200
    else:
        body = "Failed to send email."
        status_code = 500

    return {
        'statusCode': status_code,
        'body': json.dumps(body)
    }`

Error Message: Response { "errorMessage": "2024-06-26T05:12:37.998Z 34457ba1-910f-4f54-9ced-234dac1c0950 Task timed out after 5.01 seconds" }

If I remove the VPC, it works again.
<p>The code below to send an Amazon SES email works fine. However, I need the code to communicate with a local EC2 database, so I need to add this Lambda function to my VPC and Subnets - At this point, the code below stops working and timeouts.</p>
<p><strong>How can I fix this?</strong></p>
<pre><code>import json
import boto3

def send_email_ses(email):
client = boto3.client('ses', region_name='eu-west-1')

try:
response = client.send_email(
Destination={
'ToAddresses':
},
Message={
'Body': {
'Text': {
'Charset': 'UTF-8',
'Data': 'Hello world',
}
},
'Subject': {
'Charset': 'UTF-8',
'Data': 'Welcome! Your API Key',
},
},
Source='[email protected]'
)
return response['MessageId']
except Exception as e:
print(f"An error occurred: {str(e)}")
return None

def lambda_handler(event, context):
email = "[email protected]"
message_id = send_email_ses(email)

if message_id:
body = f"Email Sent Successfully. MessageId is: {message_id}"
status_code = 200
else:
body = "Failed to send email."
status_code = 500

return {
'statusCode': status_code,
'body': json.dumps(body)
}`
</code></pre>
<p>Error Message:
<code>Response { "errorMessage": "2024-06-26T05:12:37.998Z 34457ba1-910f-4f54-9ced-234dac1c0950 Task timed out after 5.01 seconds" }</code></p>
<p>If I remove the VPC, it works again.</p>
 

Latest posts

I
Replies
0
Views
1
impact christian
I
Top