Skip to content

Commit a0352ed

Browse files
authored
Integrate Docker for building Lambda package
Added Docker checks and installation for Lambda dependencies.
1 parent 4bbd252 commit a0352ed

1 file changed

Lines changed: 60 additions & 8 deletions

File tree

infra/scripts/setup/analysis.sh

Lines changed: 60 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -111,31 +111,83 @@ BUILD_DIR="$LAMBDA_DIR/build"
111111
DIST_DIR="$LAMBDA_DIR/dist"
112112

113113
rm -rf "$BUILD_DIR" "$DIST_DIR"
114-
mkdir -p "$BUILD_DIR" "$DIST_DIR"
114+
mkdir -p "$BUILD_DIR/package" "$DIST_DIR"
115115

116-
python3 -m venv "$BUILD_DIR/venv"
117-
source "$BUILD_DIR/venv/bin/activate"
118-
pip install --upgrade pip > /dev/null 2>&1
116+
# Check if Docker is available and running
117+
if ! command -v docker &> /dev/null; then
118+
log_error "Docker is not installed. Cannot build Lambda package."
119+
exit 1
120+
fi
119121

122+
if ! docker info > /dev/null 2>&1; then
123+
log_info "Starting Docker daemon..."
124+
sudo systemctl start docker
125+
sleep 3
126+
127+
if ! docker info > /dev/null 2>&1; then
128+
log_error "Docker failed to start"
129+
exit 1
130+
fi
131+
fi
132+
133+
# Use Docker to build Lambda package with correct Python version and architecture
134+
# This ensures native dependencies like cffi are compiled for Lambda runtime (Python 3.13 x86_64)
135+
log_info "Installing dependencies with Docker (Python 3.13 x86_64)..."
120136
if [[ -f "$LAMBDA_DIR/requirements.txt" ]]; then
121-
pip install -r "$LAMBDA_DIR/requirements.txt" -t "$BUILD_DIR/package" > /dev/null 2>&1
137+
# Pull Lambda image first
138+
log_info "Pulling AWS Lambda Python 3.13 image (this may take a minute)..."
139+
if ! docker pull public.ecr.aws/lambda/python:3.13 2>&1 | grep -v "^$"; then
140+
log_error "Failed to pull Lambda Docker image"
141+
exit 1
142+
fi
143+
144+
log_info "Installing Python dependencies..."
145+
# Run pip install inside Lambda container (override entrypoint to use bash)
146+
if ! docker run --rm \
147+
--entrypoint /bin/bash \
148+
-v "$LAMBDA_DIR/requirements.txt:/tmp/requirements.txt:ro" \
149+
-v "$BUILD_DIR/package:/output" \
150+
public.ecr.aws/lambda/python:3.13 \
151+
-c "pip install -r /tmp/requirements.txt -t /output --no-cache-dir && chown -R $(id -u):$(id -g) /output" 2>&1; then
152+
log_error "Failed to install Lambda dependencies with Docker"
153+
exit 1
154+
fi
155+
else
156+
log_warning "No requirements.txt found at $LAMBDA_DIR/requirements.txt"
122157
fi
123158

159+
log_info "Copying Lambda source files..."
124160
cp -r "$LAMBDA_DIR/src/"* "$BUILD_DIR/package/"
161+
162+
log_info "Creating deployment package..."
125163
# Use subshell to avoid changing directory in main script
126-
(cd "$BUILD_DIR/package" && zip -r "$DIST_DIR/lambda_function.zip" . > /dev/null 2>&1)
164+
(cd "$BUILD_DIR/package" && zip -q -r "$DIST_DIR/lambda_function.zip" .)
127165

166+
if [[ ! -f "$DIST_DIR/lambda_function.zip" ]]; then
167+
log_error "Failed to create deployment package"
168+
exit 1
169+
fi
170+
171+
ZIP_SIZE=$(du -h "$DIST_DIR/lambda_function.zip" | cut -f1)
172+
log_info "Deployment package size: $ZIP_SIZE"
173+
174+
log_info "Updating Lambda function code..."
128175
aws lambda update-function-code \
129176
--function-name "$LAMBDA_FUNCTION_NAME" \
130177
--zip-file fileb://"$DIST_DIR/lambda_function.zip" \
131178
--no-cli-pager > /dev/null 2>&1
132179

180+
if [[ $? -ne 0 ]]; then
181+
log_error "Failed to update Lambda function code"
182+
exit 1
183+
fi
184+
185+
log_info "Waiting for Lambda function to update..."
133186
aws lambda wait function-updated --function-name "$LAMBDA_FUNCTION_NAME"
134187

135188
rm -rf "$BUILD_DIR" "$DIST_DIR"
136-
deactivate 2>/dev/null || true
137189

138-
log_success "Lambda updated"
190+
log_success "Lambda function updated successfully"
139191

140192
# Create JVM Metrics dashboard
141193
log_info "Creating JVM Metrics dashboard..."

0 commit comments

Comments
 (0)