This page describes how to create the attendanceFunction
Lambda function.
It is used to query attendance data from DynamoDB based on your Rekognition collection results.
In the Lambda console, navigate to Functions.
Choose Create function.
Under Author from scratch:
attendanceFunction
Node.js 22.x
x86_64
Other Additional configurations can be left alone.
Or copy the handler.js
file from:Amplify/functions/attendanceFunction/handler.js
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
import { DynamoDBDocumentClient, QueryCommand } from "@aws-sdk/lib-dynamodb";
const dynamoClient = new DynamoDBClient({ region: process.env.AWS_REGION });
const ddbDocClient = DynamoDBDocumentClient.from(dynamoClient);
export const handler = async (event) => {
try {
const { externalImageId } = event.queryStringParameters || {};
if (!externalImageId) {
return {
statusCode: 400,
body: JSON.stringify({
success: false,
error: "Missing externalImageId",
}),
};
}
const today = new Date().toISOString().split("T")[0];
const params = {
TableName: process.env.DYNAMO_TABLE,
KeyConditionExpression: "externalImageId = :pk AND checkinDay = :today",
ExpressionAttributeValues: {
":pk": externalImageId,
":today": today,
},
};
const result = await ddbDocClient.send(new QueryCommand(params));
const items = result.Items || [];
return {
statusCode: 200,
body: JSON.stringify({
success: true,
count: items.length,
items,
}),
};
} catch (err) {
console.error("Error fetching today's check-ins:", err);
return {
statusCode: 500,
body: JSON.stringify({ success: false, error: err.message }),
};
}
};
Choose Deploy (or Ctrl + Shift + U
) to save your changes.
S3_BUCKET=YOUR_S3_BUCKET_NAME
REKOGNITION_COLLECTION=YOUR_REKOGNITION_COLLECTION_NAME
DYNAMO_TABLE=YOUR_DYNAMO_TABLE_NAME
Your Lambda function needs permission to query data from your DynamoDB attendance table.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "dynamodb:Query",
"Resource": "arn:aws:dynamodb:us-east-1:938108731074:table/${env:DYNAMO_TABLE}"
}
]
}
Replace ${env:DYNAMO_TABLE}
with your actual DynamoDB table name if environment substitution is not supported in the IAM policy editor.
At this point, attendanceFunction
is ready to query attendance records stored in your DynamoDB table.