|
1 | 1 | #!/bin/bash |
2 | | -# Creates bridge-monitor-state and bridge-exchange-histories DynamoDB tables |
3 | | -# Usage: AWS_REGION=us-east-1 bash bootstrap-dynamodb.sh |
| 2 | +# Creates bridge-monitor-state and bridge-exchange-histories DynamoDB tables, |
| 3 | +# and sets up the EventBridge rule to trigger the Lambda every 5 minutes. |
| 4 | +# |
| 5 | +# Lambda timeout must be set to 5 minutes (300s) to match the schedule interval. |
| 6 | +# EventBridge fires every 5 minutes — Lambda must finish before the next invocation. |
| 7 | +# |
| 8 | +# Usage: |
| 9 | +# AWS_REGION=us-east-1 LAMBDA_FUNCTION_NAME=bridge-relayer bash bootstrap-dynamodb.sh |
4 | 10 |
|
5 | 11 | set -e |
6 | 12 |
|
7 | 13 | REGION="${AWS_REGION:-us-east-1}" |
| 14 | +LAMBDA_FUNCTION_NAME="${LAMBDA_FUNCTION_NAME:-bridge-relayer}" |
| 15 | +SCHEDULE_EXPRESSION="rate(5 minutes)" # Must match Lambda timeout (300s) |
8 | 16 |
|
9 | 17 | echo "Creating DynamoDB tables in region: ${REGION}" |
10 | 18 |
|
@@ -55,3 +63,57 @@ echo "bridge-exchange-histories created." |
55 | 63 |
|
56 | 64 | echo "" |
57 | 65 | echo "All DynamoDB tables created successfully." |
| 66 | + |
| 67 | +# --- EventBridge rule: invoke Lambda every 5 minutes --- |
| 68 | +echo "" |
| 69 | +echo "Setting up EventBridge schedule (${SCHEDULE_EXPRESSION})..." |
| 70 | + |
| 71 | +RULE_NAME="bridge-relayer-schedule" |
| 72 | + |
| 73 | +RULE_ARN=$(aws events put-rule \ |
| 74 | + --region "$REGION" \ |
| 75 | + --name "$RULE_NAME" \ |
| 76 | + --schedule-expression "$SCHEDULE_EXPRESSION" \ |
| 77 | + --state ENABLED \ |
| 78 | + --description "Triggers bridge-relayer Lambda every 5 minutes" \ |
| 79 | + --query 'RuleArn' \ |
| 80 | + --output text) |
| 81 | + |
| 82 | +echo "EventBridge rule created: ${RULE_ARN}" |
| 83 | + |
| 84 | +LAMBDA_ARN=$(aws lambda get-function \ |
| 85 | + --region "$REGION" \ |
| 86 | + --function-name "$LAMBDA_FUNCTION_NAME" \ |
| 87 | + --query 'Configuration.FunctionArn' \ |
| 88 | + --output text) |
| 89 | + |
| 90 | +# Allow EventBridge to invoke the Lambda |
| 91 | +aws lambda add-permission \ |
| 92 | + --region "$REGION" \ |
| 93 | + --function-name "$LAMBDA_FUNCTION_NAME" \ |
| 94 | + --statement-id "AllowEventBridgeInvoke" \ |
| 95 | + --action "lambda:InvokeFunction" \ |
| 96 | + --principal "events.amazonaws.com" \ |
| 97 | + --source-arn "$RULE_ARN" \ |
| 98 | + 2>/dev/null || echo " (permission already exists, skipping)" |
| 99 | + |
| 100 | +aws events put-targets \ |
| 101 | + --region "$REGION" \ |
| 102 | + --rule "$RULE_NAME" \ |
| 103 | + --targets "Id=bridge-relayer-target,Arn=${LAMBDA_ARN}" |
| 104 | + |
| 105 | +echo "EventBridge rule wired to Lambda: ${LAMBDA_FUNCTION_NAME}" |
| 106 | + |
| 107 | +# --- Lambda timeout: enforce 300s (5 minutes) --- |
| 108 | +echo "" |
| 109 | +echo "Setting Lambda timeout to 300s..." |
| 110 | +aws lambda update-function-configuration \ |
| 111 | + --region "$REGION" \ |
| 112 | + --function-name "$LAMBDA_FUNCTION_NAME" \ |
| 113 | + --timeout 300 |
| 114 | + |
| 115 | +echo "" |
| 116 | +echo "Bootstrap complete." |
| 117 | +echo " Schedule : ${SCHEDULE_EXPRESSION}" |
| 118 | +echo " Lambda : ${LAMBDA_FUNCTION_NAME} (timeout: 300s)" |
| 119 | +echo " Tables : bridge-monitor-state, bridge-exchange-histories" |
0 commit comments