我写了一个打字稿 Lambda API 。如图
callback complete
记录的地方,10秒后lambda响应。您还可以在时间戳中看到这一点。
export const handler = async (event, context, callback) => {
const { headers: { Authorization } } = event
start = Date.now()
const auth = await authorizer(Authorization, UserType.AGENT)
console.log(`Authorizer took ${Date.now() - start} seconds to complete`)
if (!auth.isAuthenticated) {
callback(null, { statusCode: 401, body: JSON.stringify("Unauthorized") })
return
}
start = Date.now()
const associations = await dynamoDb.query({
TableName: process.env.CUSTOMER_ASSISTANT_TABLE!,
IndexName: 'xxxxx',
KeyConditionExpression: 'xxxx = :email',
ExpressionAttributeValues: {
':email': auth.user.email
}
}).promise()
console.log(`DynamoDB query took ${Date.now() - start} seconds to complete`)
const customers = associations.Items.map(association => association.customer)
if (customers.length === 0) {
callback(null, { statusCode: 200, body: JSON.stringify([]) })
return
}
start = Date.now()
const customerDetails = await pool.query(`SELECT email, first_name, last_name FROM users WHERE email IN ('${customers.join("','")}')`)
console.log(`PostgreSQL query took ${Date.now() - start} seconds to complete`)
const { rows } = customerDetails
console.log('callback')
callback(null, { statusCode: 200, body: JSON.stringify(rows) })
console.log('callback complete')
return
}