forked from kj4c/Alphas_Propex
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_lambda.sh
More file actions
executable file
·66 lines (50 loc) · 2.33 KB
/
update_lambda.sh
File metadata and controls
executable file
·66 lines (50 loc) · 2.33 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
#!/bin/bash
BACKEND_DIR="./backend"
DOCKER_IMAGE_NAME="docker-lambda"
AWS_REGION="us-east-1"
ECR_URL="754754248018.dkr.ecr.$AWS_REGION.amazonaws.com/$DOCKER_IMAGE_NAME"
CHANGED=0
echo "🚀 Checking for changes in Lambda functions..."
echo ""
# Calculate new checksum, ignoring 'tests/' and 'price_prediction/' directories
NEW_CHECKSUM=$(find "$BACKEND_DIR" -type d \( -name "tests" -o -name "price_prediction" -o -name "route_tests" \) -prune -o -type f -name "*.py" -print0 | sort -z | xargs -0 md5sum | md5sum | awk '{print $1}')
CHECKSUM_FILE=".docker_checksum"
if [ ! -f "$CHECKSUM_FILE" ]; then
touch "$CHECKSUM_FILE"
fi
OLD_CHECKSUM=$(cat "$CHECKSUM_FILE")
if [ "$NEW_CHECKSUM" != "$OLD_CHECKSUM" ]; then
echo "📦 Changes detected in Lambda functions (excluding tests & price_prediction). Rebuilding Docker image..."
# Build the Docker image
docker build --platform linux/amd64 -t $DOCKER_IMAGE_NAME .
# Authenticate with ECR
aws ecr get-login-password --region $AWS_REGION | docker login --username AWS --password-stdin $ECR_URL
# Tag and push the image to ECR
docker tag $DOCKER_IMAGE_NAME:latest $ECR_URL:latest
docker push $ECR_URL:latest
# Store new checksum
echo "$NEW_CHECKSUM" > "$CHECKSUM_FILE"
CHANGED=1
else
echo "🐻 No changes detected in Lambda functions, skipping Docker build."
fi
if [ "$CHANGED" -eq 1 ]; then
echo "Updating variable names in variable.tf..."
python3 update_variables.py
echo "🎉 Docker image updated and pushed to ECR!"
else
echo "🚀 No updates needed for Terraform."
fi
IMAGE_DIGEST=$(aws ecr describe-images --repository-name docker-lambda --region us-east-1 --query 'sort_by(imageDetails,&imagePushedAt)[-1].imageDigest' --output text)
if [ -n "$IMAGE_DIGEST" ]; then
echo "🔄 Updating Terraform Lambda image URI... in main.tf"
awk -v region="$AWS_REGION" -v digest="$IMAGE_DIGEST" '
{
gsub(/image_uri[[:space:]]*=[[:space:]]*"[^"]*"/, "image_uri = \"754754248018.dkr.ecr." region ".amazonaws.com/docker-lambda@" digest "\"")
print
}' main.tf > temp.tf && mv temp.tf main.tf
echo "✅ Terraform configuration updated with new image: $IMAGE_DIGEST"
echo "🎉 Ready for deployment! Run terraform init then terraform apply only if you ready gang"
else
echo "❌ Failed to fetch latest image digest!"
fi