Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
services:
lambda:
image: python:3.14.6-alpine
networks:
- localstack_network
volumes:
- ${LAMBDA_DIR}:/var/task
working_dir: /var/task
command: sh -c "pip install --quiet -r requirements.txt && python ${LAMBDA_PATH}"

networks:
localstack_network:
external: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"""
This lambda function is triggered by a CloudWatch event rule on a schedule.
It add a message to the queue with the current date, to be processed by Events.
Events will call the `reindex_touched_dois` method to reindex the DOIs touched today.
"""

import os
import json
import datetime
import boto3

# Lambda handler
def lambda_handler(event, context):
"""Lambda process handler"""
print("Starting reindex_touched_dois_runner lambda function")

# Configuration
queue_name = os.getenv('QUEUE_NAME')

# Calculate the date range for the reindex
yesterday = datetime.datetime.now(datetime.UTC) - datetime.timedelta(days=1)
yesterday = yesterday.strftime("%Y-%m-%d")

message = {
'Name': 'shoryuken_class',
'Type': 'String',
'Value': 'ReindexTouchedDoisWorker',

'date': yesterday,
}

# Queue a task for each repository
sqs = boto3.resource('sqs')
queue = sqs.get_queue_by_name(QueueName=queue_name)
queue.send_message(MessageBody=json.dumps(message))

return("Queued reindex for dois touched on {}".format(yesterday))

if __name__ == '__main__':
# For local testing fake the arguments to lambda handler function
event = {}
context = []
lambda_handler(event, context)
1 change: 1 addition & 0 deletions lambdas/events_queue_reindex_touched_dois/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
boto3
57 changes: 57 additions & 0 deletions run_lambda.sh

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While fine, this seems a little more complicated than needed.

You could specify the AWS localstack env variables in a .env and load this in the docker compose.
The only KEY being passed through to running is QUEUE_NAME, which again is probably always same for local testing and can be defined in the .env

So instead of running this SH, you could just run the docker compose run directly.

@bklaing2 bklaing2 Jul 8, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree it was a bit overkill. I've simplified the script, but for the moment it is still necessary to extract the LAMBDA_DIR/PATH variables needed in docker-compose.yml.

Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/usr/bin/env bash
set -euo pipefail

usage() {
cat <<'EOF'
Usage:
./run_lambda.sh <path/to/lambda_runner.py> [KEY=VALUE]...
EOF
}

[[ $# -ge 1 ]] || { usage; exit 1; }

SCRIPT_PATH="$1"
shift

LAMBDA_DIR="$(dirname "$(realpath "$SCRIPT_PATH")")"
LAMBDA_PATH="$(basename "$SCRIPT_PATH")"

# Defaults for LocalStack use
: "${AWS_ACCESS_KEY_ID:=test}"
: "${AWS_SECRET_ACCESS_KEY:=test}"
: "${AWS_DEFAULT_REGION:=us-east-1}"
: "${AWS_ENDPOINT_URL:=http://localstack:4566}"

export LAMBDA_DIR LAMBDA_PATH
export AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_DEFAULT_REGION AWS_ENDPOINT_URL

# Parse trailing KEY=VALUE args
env_args=()
for spec in "$@"; do
if [[ "$spec" != *=* ]]; then
echo "Error: invalid argument '$spec' (expected KEY=VALUE)"
usage
exit 1
fi

key="${spec%%=*}"
val="${spec#*=}"

if [[ -z "$key" ]]; then
echo "Error: invalid assignment '$spec' (empty key)"
exit 1
fi

# Export so compose process sees it, and pass through to container
export "$key=$val"
env_args+=(-e "$key")
done

# Always pass required AWS vars explicitly
docker compose run --rm \
-e AWS_ACCESS_KEY_ID \
-e AWS_SECRET_ACCESS_KEY \
-e AWS_DEFAULT_REGION \
-e AWS_ENDPOINT_URL \
"${env_args[@]}" \
lambda