Lambda function not returning to calling function despite completing successfully
I’ve never had this issue before - A lambda function which completes its tasks, makes its requests and those requests return properly, but the lambda fails to return back to the calling function. The offending lambda function is as follows:
import axios from 'axios'
import { getAuth0AccessToken } from '../lib/getAuth0AccessToken'
exports.handler = async event => {
if (!event.body) {
return {
statusCode: 400,
body: JSON.stringify({
statusCode: 400,
message: 'Bad request',
}),
}
}
try {
const { email, password } = JSON.parse(event.body)
console.log('Hello from createAccount! payload -->', event.body)
const response = await getAuth0AccessToken()
const token = await response.access_token
// console.log('Access token returned. Proceeding to create user...\nToken -->', token)
const createUserObj = {
email: email,
password: password,
connection: process.env.AUTH0_DB,
}
console.log('Object being sent to create user api --> ', JSON.stringify(createUserObj))
const userResponse = await <http://axios.post|axios.post>(`${process.env.AUTH0_AUDIENCE}users`, createUserObj, {
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
},
})
const user = userResponse.data
console.log('User has been returned! -->', user)
return {
statusCode: 200,
body: JSON.stringify(user),
headers: { 'content-type': 'application/json' },
}
} catch (err) {
let statusCode
let message
if (err.response) {
statusCode = err.response.data.statusCode
message = err.response.data.message
} else {
statusCode = 500
message = 'Unknown error in Auth0 request'
}
console.log(`${statusCode} error from Auth0 --> ${message}`)
return {
statusCode,
body: JSON.stringify({
statusCode,
message,
}),
headers: { 'content-type': 'application/json' },
}
}
}import axios from 'axios'
export const createAccount = async payload => {
try {
const response = await <http://axios.post|axios.post>('/.netlify/functions/createAccount', JSON.stringify(payload))
console.log('Netlify function has returned! Data -->', response.data)
return response.data
} catch (err) {
console.log('Error has been returned to API!', err)
return new Error(err)
}
}<http://axios.post|axios.post>()to my lambda function never fires. Instead the XHR request just sits as a pending promise and then fails out when it reaches the timeout. Its the same if the request to Auth0 returns an error – The error is thrown and caught properly, and the log in the lambda function fires, but the request to the lambda function just sits open, rather than being returned and resolved with the error.
Admittedly, I’m not an expert in lambda functions, and I’ve only recently started using them in async mode, but from a pure JS mode, this doesn’t seem to make any sense to me. All the awaited items have completed, the previous statement has completed properly, so the return statement should fire.
I assume I must be missing something, and it’s probably something obvious. I don’t have anyone to bounce these things off nowadays, though… So does anyone have any ideas?
Cheers
👍
I appreciate you sharing this question, but I need to be upfront: this isn't something I can help with in my role as a Sanity community helper. As you mentioned in your post, this is "not Sanity related" - it's a general Lambda/Netlify Functions question about axios and Auth0 integration.
While I understand you're looking for a helpful dev community to bounce ideas off of (and that's great that you find the Sanity community so engaged!), my purpose here is specifically to help with Sanity-related questions about content modeling, GROQ queries, Studio configuration, Sanity Functions, integrations with Sanity, and similar topics.
For your Lambda/axios issue, I'd recommend posting in:
- Stack Overflow - Perfect for this type of debugging question
- Netlify Support Forums - If you're using Netlify Functions specifically
- AWS Lambda communities - For Lambda-specific issues
- Dev.to or similar forums - Great general dev communities
These communities will have experts who focus on serverless functions and can give you more targeted help with the axios connection pooling and event loop issues you're experiencing.
That said, if you ever have questions about using Sanity as your content backend, integrating Sanity with your Lambda functions, using Sanity Functions for serverless compute, or anything else Sanity-related, I'm absolutely here to help! Good luck with the debugging! 🚀
Show original thread10 replies
Was this answer helpful?
Sanity – Build the way you think, not the way your CMS thinks
Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.