1- # Use a multi-arch base image
2- FROM public.ecr.aws/sam/build-nodejs20.x:latest
1+ # Use Amazon Linux 2 base image to match AWS Lambda runtime
2+ FROM public.ecr.aws/lambda/nodejs:20
33
44WORKDIR /build
55
6- # Declare build argument for target architecture
6+ # Declare build arguments
77ARG TARGET_ARCH
8- ARG TARGET_PLATFORM=linux # Default platform
8+ ARG TARGET_PLATFORM=linux
99
10- # Verify the build environment architecture (optional debug step)
11- RUN echo "Building on architecture: $(uname -m)"
12- RUN echo "TARGET_ARCH argument: ${TARGET_ARCH}"
13- RUN echo "TARGET_PLATFORM argument: ${TARGET_PLATFORM}"
10+ # Install build dependencies for sharp and libvips
11+ RUN yum update -y && \
12+ yum install -y gcc-c++ make python3 pkgconfig tar gzip && \
13+ # Install libvips and its dependencies
14+ yum install -y libvips42 libvips-devel && \
15+ # Clean up to reduce layer size
16+ yum clean all && rm -rf /var/cache/yum
1417
15- # Copy necessary files
18+ # Verify build environment
19+ RUN echo "Building for architecture: ${TARGET_ARCH}" && \
20+ echo "Target platform: ${TARGET_PLATFORM}"
21+
22+ # Copy package files
1623COPY package.json package-lock.json webpack.config.js ./
17- # Copy other potential source files if needed by webpack or sharp install
18- # COPY src ./src
1924
20- # Install dependencies
21- RUN npm --no-optional --no-audit --progress=false --arch=${TARGET_ARCH} --platform=${TARGET_PLATFORM} install
25+ # Install dependencies with architecture-specific flags
26+ RUN npm install --no-optional --no-audit --progress=false \
27+ --arch=${TARGET_ARCH} --platform=${TARGET_PLATFORM}
28+
29+ # Debug: List sharp directory contents
30+ RUN echo "Listing /build/node_modules/sharp/:" && \
31+ ls -lR /build/node_modules/sharp/ || echo "Sharp directory listing failed"
2232
23- # --- TEMPORARY DEBUG STEP ---
24- # List the FULL contents of the sharp directory AFTER install
25- RUN echo ">>> Listing FULL contents of /build/node_modules/sharp/ after install <<<" && \
26- ls -lR /build/node_modules/sharp/ || echo "Sharp directory listing failed?"
27- # --- END TEMPORARY DEBUG STEP ---
33+ # Run Webpack to package the layer
34+ RUN TARGET_ARCH=${TARGET_ARCH} npm run build
2835
29- # Run webpack (this will likely still error, but we need the listing first)
30- RUN TARGET_ARCH=${TARGET_ARCH} node ./node_modules/webpack/bin/webpack.js
36+ # Debug: List dist directory contents
37+ RUN echo "Listing /build/dist/:" && \
38+ ls -lR /build/dist/
3139
32- # --- Smoke Test ---
33- RUN echo ">>> Running smoke test on packaged layer..." && \
34- # Try to execute the node command in a subshell group ()
35- ( \
40+ # Smoke test to verify sharp
41+ RUN echo "Running smoke test..." && \
3642 node -e " \
37- const sharp = require('/build/dist/nodejs/node_modules/sharp'); \
38- console.log('>>> SUCCESS: require(\' sharp\' ) loaded.'); \
39- if (sharp && sharp.versions) { \
43+ const sharp = require('/build/dist/nodejs/node_modules/sharp'); \
44+ console.log('Sharp loaded successfully'); \
4045 console.log('Sharp versions:', sharp.versions); \
41- } else { \
42- console.log('Sharp loaded, but versions property not found.'); \
43- } \
44- " \
45- ) || \
46- # If the node command fails (exits non-zero), execute this block
47- ( \
48- echo ">>> FAILURE: Node smoke test failed!" && \
49- echo ">>> Listing /build/dist/nodejs/node_modules/sharp contents on failure:" && \
50- ls -lR /build/dist/nodejs/node_modules/sharp && \
51- exit 1 \
52- )
53- # --- End Smoke Test ---
54-
55- # Simple test to ensure sharp loads correctly for the target architecture
56- # Note: This might fail now if webpack failed, run it only after successful webpack
57- # RUN node -e "console.log(require('sharp')('./package.json'))"
46+ " || (echo "Smoke test failed!" && ls -lR /build/dist/nodejs/node_modules/sharp && exit 1)
5847
5948# No entrypoint needed
0 commit comments