Model/run_lambda_local.sh

42 lines
1.6 KiB
Bash
Executable file

#!/bin/bash
# Set up constants
IMAGE_NAME="fastapi-lambda-image"
TAG="test"
PORT="8000"
RIE_DIR="$HOME/.aws-lambda-rie"
DOCKER_ENTRYPOINT="$RIE_PATH/aws-lambda-rie"
DOCKER_CMD="/usr/local/bin/python -m awslambdaric backend.app.main.handler"
ENV_FILE_PATH="backend/.env"
AWS_CREDENTIALS_PATH="$HOME/.aws/credentials"
# Step 1: Download the AWS Lambda Runtime Interface Emulator if it doesn't exist
if [ ! -f "$RIE_PATH/aws-lambda-rie" ]; then
echo "Downloading AWS Lambda Runtime Interface Emulator..."
mkdir -p $RIE_PATH
curl -Lo $RIE_PATH/aws-lambda-rie https://github.com/aws/aws-lambda-runtime-interface-emulator/releases/latest/download/aws-lambda-rie-arm64
chmod +x $RIE_PATH/aws-lambda-rie
fi
# Step 2: Build the Docker image
echo "Building Docker image..."
docker build --platform linux/amd64 -t $IMAGE_NAME:$TAG -f backend/docker/lambda.Dockerfile .
# Step 3: Run the Docker image with the emulator, .env file, and AWS credentials
echo "Starting the Docker container..."
CONTAINER_ID=$(docker run -d \
--env-file $ENV_FILE_PATH \
-v $AWS_CREDENTIALS_PATH:/root/.aws/credentials \
-v "$RIE_DIR:/aws-lambda" \
-p $PORT:8080 \
--entrypoint "/aws-lambda/aws-lambda-rie" \
$IMAGE_NAME:$TAG $DOCKER_CMD)
# Output information
echo "Docker container is running. Use the following command to send a request:"
echo "curl \"http://localhost:$PORT/2015-03-31/functions/function/invocations\" -d '{}'"
# Optional: If you want the script to stop the Docker container after you press a key, you can uncomment the next lines.
# echo "Press any key to stop the Docker container..."
# read -n 1 -s -r
# docker kill $CONTAINER_ID