Reputation: 143
I have set up serverless environment on AWS using lambdas and api gateway. I have a script what gets called whenever someone fills up information on contact form. The script itself looks like this:
const rp = require('request-promise')
const sendEmail = require('./sendEmail')
module.exports.run = async (event, context, callback) => {
const body = JSON.parse(event.body)
const { name, email, budget, message, attachment } = body
if (!name) {
return callback(null, {
statusCode: 400,
body: JSON.stringify({ message: 'Name is required' }),
})
}
if (!email) {
return callback(null, {
statusCode: 400,
body: JSON.stringify({ message: 'Email address is required' }),
})
}
if (!message) {
return callback(null, {
statusCode: 400,
body: JSON.stringify({ message: 'Message is required' }),
})
}
return Promise.all([
sendEmail({
to: 'Example <[email protected]>',
subject: 'Received submission',
data:
'Hello'
}),
sendEmail({
to: `${name} <${email}>`,
subject: 'Subject',
data:
'Example text',
}),
])
.then(() => {
return callback(null, {
statusCode: 200,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Credentials': true,
},
body: JSON.stringify({ message: 'Great success' }),
})
})
.catch(err => {
return callback(null, {
statusCode: 500,
body: JSON.stringify({
message: 'Oh no :( Message not delivered',
error: err,
}),
})
})
}
And this is my sendEmail class
const AWS = require('aws-sdk')
const ses = new AWS.SES()
module.exports = function({ to, subject, data, replyTo }) {
return ses
.sendEmail({
Destination: { ToAddresses: [to] },
Message: {
Body: {
Text: { Charset: 'UTF-8', Data: data },
},
Subject: {
Data: subject,
Charset: 'UTF-8',
},
},
Source: 'Example <[email protected]>',
ReplyToAddresses: [replyTo],
})
.promise()
}
However it keeps hanging due to the timeout which are limited to five minutes on aws side, is there something i'm missing that it's taking longer than five minutes?
Upvotes: 2
Views: 210
Reputation: 143
The issue was that the lambdas were located in eu-west-2, while the SES was set up in eu-west-1, meaning it couldn't contact the api endpoint in eu-west-2 in SES, resulting in response 500.
Upvotes: 1
Reputation: 8583
I am seeing two issues here.
you can simply return the output, instead of of using callback. I have also changed the promise syntax to async/await.
const sendEmail = require('./sendEmail')
module.exports.run = async (event, context) => {
const body = JSON.parse(event.body)
const { name, email, budget, message, attachment } = body
if (!name) {
return {
statusCode: 400,
body: JSON.stringify({ message: 'Name is required' }),
}
}
if (!email) {
return {
statusCode: 400,
body: JSON.stringify({ message: 'Email address is required' }),
}
}
if (!message) {
return {
statusCode: 400,
body: JSON.stringify({ message: 'Message is required' }),
}
}
try {
await Promise.all([
sendEmail({
to: 'Example <[email protected]>',
subject: 'Received submission',
data:
'Hello'
}),
sendEmail({
to: `${name} <${email}>`,
subject: 'Subject',
data:
'Example text',
}),
]);
return {
statusCode: 200,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Credentials': true,
},
body: JSON.stringify({ message: 'Great success' }),
}
} catch (err) {
return {
statusCode: 500,
body: JSON.stringify({
message: 'Oh no :( Message not delivered',
error: err,
}),
}
}
}
Upvotes: 0