Repository navigation
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·383 lines (331 loc) · 12 KB
/
Copy pathsetup.sh
File metadata and controls
executable file
·383 lines (331 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#!/bin/bash
# Treez + METRC Data Sync - Lambda + EventBridge Setup Script
# Deploys automated daily sync at 11:00 PM PST
set -e
# Configuration
FUNCTION_NAME="treez-metrc-sync"
ROLE_NAME="treez-metrc-sync-role"
RULE_NAME="treez-metrc-daily-sync"
REGION="us-west-1"
RUNTIME="python3.11"
HANDLER="lambda_function.lambda_handler"
TIMEOUT=300
MEMORY_SIZE=512
# Aurora VPC Configuration (from chapters-data-cluster)
VPC_ID="vpc-0a1b2c3d4e5f6g7h8" # Update with actual VPC ID
SUBNET_IDS="" # Will be populated below
SECURITY_GROUP_ID="" # Will be populated below
echo "=========================================="
echo "Treez + METRC Sync Lambda Deployment"
echo "=========================================="
# Check AWS CLI is installed
if ! command -v aws &> /dev/null; then
echo "Error: AWS CLI is not installed"
exit 1
fi
# Check credentials
echo "Checking AWS credentials..."
AWS_ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text 2>/dev/null)
if [ -z "$AWS_ACCOUNT_ID" ]; then
echo "Error: AWS credentials not configured"
exit 1
fi
echo "Using AWS Account: $AWS_ACCOUNT_ID"
# Get VPC configuration for Aurora
echo ""
echo "Fetching VPC configuration for Aurora access..."
# Find the VPC where Aurora is deployed
AURORA_VPC=$(aws rds describe-db-clusters \
--db-cluster-identifier chapters-data-cluster \
--region $REGION \
--query 'DBClusters[0].DBSubnetGroup' \
--output text 2>/dev/null || echo "")
if [ -n "$AURORA_VPC" ]; then
# Get subnet IDs from the DB subnet group
SUBNET_IDS=$(aws rds describe-db-subnet-groups \
--db-subnet-group-name "$AURORA_VPC" \
--region $REGION \
--query 'DBSubnetGroups[0].Subnets[*].SubnetIdentifier' \
--output text 2>/dev/null | tr '\t' ',')
# Get VPC ID from first subnet
FIRST_SUBNET=$(echo $SUBNET_IDS | cut -d',' -f1)
VPC_ID=$(aws ec2 describe-subnets \
--subnet-ids $FIRST_SUBNET \
--region $REGION \
--query 'Subnets[0].VpcId' \
--output text 2>/dev/null)
echo "Found Aurora VPC: $VPC_ID"
echo "Found Subnets: $SUBNET_IDS"
fi
# Create or get security group for Lambda
echo ""
echo "Setting up security group for Lambda..."
SECURITY_GROUP_ID=$(aws ec2 describe-security-groups \
--filters "Name=group-name,Values=treez-sync-lambda-sg" "Name=vpc-id,Values=$VPC_ID" \
--region $REGION \
--query 'SecurityGroups[0].GroupId' \
--output text 2>/dev/null || echo "None")
if [ "$SECURITY_GROUP_ID" == "None" ] || [ -z "$SECURITY_GROUP_ID" ]; then
echo "Creating security group..."
SECURITY_GROUP_ID=$(aws ec2 create-security-group \
--group-name "treez-sync-lambda-sg" \
--description "Security group for Treez/METRC sync Lambda" \
--vpc-id $VPC_ID \
--region $REGION \
--query 'GroupId' \
--output text)
# Allow outbound HTTPS (for Treez/METRC APIs)
aws ec2 authorize-security-group-egress \
--group-id $SECURITY_GROUP_ID \
--protocol tcp \
--port 443 \
--cidr 0.0.0.0/0 \
--region $REGION 2>/dev/null || true
# Allow outbound PostgreSQL (for Aurora)
aws ec2 authorize-security-group-egress \
--group-id $SECURITY_GROUP_ID \
--protocol tcp \
--port 5432 \
--cidr 0.0.0.0/0 \
--region $REGION 2>/dev/null || true
echo "Created security group: $SECURITY_GROUP_ID"
else
echo "Using existing security group: $SECURITY_GROUP_ID"
fi
# Create IAM role for Lambda
echo ""
echo "Setting up IAM role..."
TRUST_POLICY='{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}'
# Check if role exists
ROLE_ARN=$(aws iam get-role --role-name $ROLE_NAME --query 'Role.Arn' --output text 2>/dev/null || echo "")
if [ -z "$ROLE_ARN" ]; then
echo "Creating IAM role: $ROLE_NAME"
ROLE_ARN=$(aws iam create-role \
--role-name $ROLE_NAME \
--assume-role-policy-document "$TRUST_POLICY" \
--query 'Role.Arn' \
--output text)
# Attach basic Lambda execution policy
aws iam attach-role-policy \
--role-name $ROLE_NAME \
--policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
# Attach VPC access policy for Aurora connectivity
aws iam attach-role-policy \
--role-name $ROLE_NAME \
--policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole
# Create custom policy for Secrets Manager access (for API keys)
SECRETS_POLICY='{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"secretsmanager:GetSecretValue"
],
"Resource": "arn:aws:secretsmanager:'$REGION':'$AWS_ACCOUNT_ID':secret:treez-sync/*"
}
]
}'
aws iam put-role-policy \
--role-name $ROLE_NAME \
--policy-name "treez-sync-secrets-access" \
--policy-document "$SECRETS_POLICY"
echo "Created role: $ROLE_ARN"
echo "Waiting for role to propagate..."
sleep 10
else
echo "Using existing role: $ROLE_ARN"
fi
# Package Lambda function
echo ""
echo "Packaging Lambda function..."
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
BUILD_DIR="$SCRIPT_DIR/build"
PACKAGE_FILE="$SCRIPT_DIR/lambda_package.zip"
# Clean up previous builds
rm -rf "$BUILD_DIR"
rm -f "$PACKAGE_FILE"
mkdir -p "$BUILD_DIR"
# Install dependencies
echo "Installing dependencies..."
pip install -r "$SCRIPT_DIR/requirements.txt" -t "$BUILD_DIR" --quiet
# Copy Lambda function
cp "$SCRIPT_DIR/lambda_function.py" "$BUILD_DIR/"
# Create zip package
echo "Creating deployment package..."
cd "$BUILD_DIR"
zip -r "$PACKAGE_FILE" . -q
cd "$SCRIPT_DIR"
PACKAGE_SIZE=$(du -h "$PACKAGE_FILE" | cut -f1)
echo "Package created: $PACKAGE_FILE ($PACKAGE_SIZE)"
# Deploy or update Lambda function
echo ""
echo "Deploying Lambda function..."
# Check if function exists
FUNCTION_EXISTS=$(aws lambda get-function --function-name $FUNCTION_NAME --region $REGION 2>/dev/null && echo "yes" || echo "no")
if [ "$FUNCTION_EXISTS" == "yes" ]; then
echo "Updating existing function..."
aws lambda update-function-code \
--function-name $FUNCTION_NAME \
--zip-file "fileb://$PACKAGE_FILE" \
--region $REGION \
--output text > /dev/null
# Update configuration
aws lambda update-function-configuration \
--function-name $FUNCTION_NAME \
--timeout $TIMEOUT \
--memory-size $MEMORY_SIZE \
--vpc-config "SubnetIds=$SUBNET_IDS,SecurityGroupIds=$SECURITY_GROUP_ID" \
--region $REGION \
--output text > /dev/null 2>/dev/null || true
else
echo "Creating new function..."
aws lambda create-function \
--function-name $FUNCTION_NAME \
--runtime $RUNTIME \
--role $ROLE_ARN \
--handler $HANDLER \
--timeout $TIMEOUT \
--memory-size $MEMORY_SIZE \
--zip-file "fileb://$PACKAGE_FILE" \
--vpc-config "SubnetIds=$SUBNET_IDS,SecurityGroupIds=$SECURITY_GROUP_ID" \
--region $REGION \
--output text > /dev/null
fi
FUNCTION_ARN=$(aws lambda get-function --function-name $FUNCTION_NAME --region $REGION --query 'Configuration.FunctionArn' --output text)
echo "Lambda deployed: $FUNCTION_ARN"
# Store secrets in Secrets Manager
echo ""
echo "Setting up Secrets Manager..."
# Check if secrets exist, create if not
SECRET_NAME="treez-sync/api-keys"
SECRET_EXISTS=$(aws secretsmanager describe-secret --secret-id $SECRET_NAME --region $REGION 2>/dev/null && echo "yes" || echo "no")
if [ "$SECRET_EXISTS" == "no" ]; then
echo "Creating secrets placeholder in Secrets Manager..."
echo "NOTE: You'll need to update these with your actual API keys"
aws secretsmanager create-secret \
--name $SECRET_NAME \
--description "API keys for Treez and METRC sync" \
--secret-string '{
"TREEZ_API_KEY": "YOUR_TREEZ_API_KEY",
"TREEZ_CLIENT_ID": "chapters_sync",
"TREEZ_DISPENSARY": "barbarycoast",
"METRC_API_KEY": "YOUR_METRC_API_KEY",
"METRC_USER_KEY": "YOUR_METRC_USER_KEY",
"DATABASE_URL": "YOUR_DATABASE_URL"
}' \
--region $REGION \
--output text > /dev/null
echo "Created secret: $SECRET_NAME"
echo ""
echo ">>> IMPORTANT: Update the secret with your actual credentials:"
echo " aws secretsmanager update-secret --secret-id $SECRET_NAME --region $REGION --secret-string '{...}'"
else
echo "Secret already exists: $SECRET_NAME"
fi
# Create EventBridge rule for daily execution at 11 PM PST
echo ""
echo "Setting up EventBridge schedule..."
# 11 PM PST = 7 AM UTC (PST is UTC-8)
# During PDT (daylight saving), 11 PM PDT = 6 AM UTC
SCHEDULE_EXPRESSION="cron(0 7 * * ? *)"
# Check if rule exists
RULE_EXISTS=$(aws events describe-rule --name $RULE_NAME --region $REGION 2>/dev/null && echo "yes" || echo "no")
if [ "$RULE_EXISTS" == "yes" ]; then
echo "Updating existing EventBridge rule..."
aws events put-rule \
--name $RULE_NAME \
--schedule-expression "$SCHEDULE_EXPRESSION" \
--state ENABLED \
--description "Daily Treez/METRC data sync at 11 PM PST" \
--region $REGION \
--output text > /dev/null
else
echo "Creating EventBridge rule..."
aws events put-rule \
--name $RULE_NAME \
--schedule-expression "$SCHEDULE_EXPRESSION" \
--state ENABLED \
--description "Daily Treez/METRC data sync at 11 PM PST" \
--region $REGION \
--output text > /dev/null
fi
# Add Lambda as target
echo "Adding Lambda as EventBridge target..."
aws events put-targets \
--rule $RULE_NAME \
--targets "Id=treez-sync-target,Arn=$FUNCTION_ARN" \
--region $REGION \
--output text > /dev/null
# Grant EventBridge permission to invoke Lambda
echo "Granting EventBridge permission to invoke Lambda..."
aws lambda add-permission \
--function-name $FUNCTION_NAME \
--statement-id "eventbridge-invoke" \
--action "lambda:InvokeFunction" \
--principal events.amazonaws.com \
--source-arn "arn:aws:events:$REGION:$AWS_ACCOUNT_ID:rule/$RULE_NAME" \
--region $REGION \
--output text > /dev/null 2>/dev/null || true
RULE_ARN=$(aws events describe-rule --name $RULE_NAME --region $REGION --query 'Arn' --output text)
echo "EventBridge rule configured: $RULE_ARN"
# Clean up build artifacts
echo ""
echo "Cleaning up..."
rm -rf "$BUILD_DIR"
# Summary
echo ""
echo "=========================================="
echo "Deployment Complete!"
echo "=========================================="
echo ""
echo "Lambda Function: $FUNCTION_NAME"
echo " ARN: $FUNCTION_ARN"
echo " Runtime: $RUNTIME"
echo " Memory: ${MEMORY_SIZE}MB"
echo " Timeout: ${TIMEOUT}s"
echo ""
echo "EventBridge Rule: $RULE_NAME"
echo " Schedule: Daily at 11:00 PM PST (7:00 AM UTC)"
echo " ARN: $RULE_ARN"
echo ""
echo "Secrets Manager: $SECRET_NAME"
echo ""
echo "=========================================="
echo "Next Steps:"
echo "=========================================="
echo ""
echo "1. Update API keys in Secrets Manager:"
echo " aws secretsmanager update-secret \\"
echo " --secret-id $SECRET_NAME \\"
echo " --region $REGION \\"
echo " --secret-string '{"
echo " \"TREEZ_API_KEY\": \"your-treez-key\","
echo " \"TREEZ_CLIENT_ID\": \"chapters_sync\","
echo " \"TREEZ_DISPENSARY\": \"barbarycoast\","
echo " \"METRC_API_KEY\": \"your-metrc-api-key\","
echo " \"METRC_USER_KEY\": \"your-metrc-user-key\","
echo " \"DATABASE_URL\": \"postgresql://...\"
echo " }'"
echo ""
echo "2. Test the Lambda function manually:"
echo " aws lambda invoke \\"
echo " --function-name $FUNCTION_NAME \\"
echo " --region $REGION \\"
echo " --payload '{}' \\"
echo " response.json && cat response.json"
echo ""
echo "3. Check CloudWatch logs:"
echo " aws logs tail /aws/lambda/$FUNCTION_NAME --region $REGION --follow"
echo ""