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?
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.