shAkur
shAkur

Reputation: 1002

sam local invoke throws cannot find module index error

I'm trying to run a sam local invoke command within my Dockerfile setup and it goes well untill I get to the point of mounting my lambda function where it throws an erro:

redis-populator-service-1  | Mounting /app/.aws-sam/build/GameDictionaryHandlerLambdaFunction as /var/task:ro,delegated, inside runtime container
redis-populator-service-1  | START RequestId: 30157dc6-411c-4d91-b52a-bd2431ca4b6b Version: $LATEST
redis-populator-service-1  | 2024-09-03T14:02:32.936Z   undefined   ERROR   Uncaught Exception  {"errorType":"Runtime.ImportModuleError","errorMessage":"Error: Cannot find module 'index'\nRequire stack:\n- /var/runtime/index.mjs","stack":["Runtime.ImportModuleError: Error: Cannot find module 'index'","Require stack:","- /var/runtime/index.mjs","    at _loadUserApp (file:///var/runtime/index.mjs:1087:17)","    at async UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1119:21)","    at async start (file:///var/runtime/index.mjs:1282:23)","    at async file:///var/runtime/index.mjs:1288:1"]}
redis-populator-service-1  | 03 Sep 2024 14:02:33,065 [ERROR] (rapid) Init failed error=Runtime exited with error: exit status 129 InvokeID=
redis-populator-service-1  | 03 Sep 2024 14:02:33,072 [ERROR] (rapid) Invoke failed error=Runtime exited with error: exit status 129 InvokeID=da9bba4b-f134-4b68-a09a-5f37e7c90d37
redis-populator-service-1  | 03 Sep 2024 14:02:33,085 [ERROR] (rapid) Invoke DONE failed: Sandbox.Failure

This is my dockerfile

FROM python:3.9-slim

# Install system dependencies
RUN apt-get update && \
    apt-get install -y \
    curl \
    unzip \
    groff \
    less \
    nodejs \
    npm \
    && rm -rf /var/lib/apt/lists/*

# Install esbuild globally
RUN npm install -g esbuild

# Install AWS SAM CLI using pip
RUN pip install aws-sam-cli

# Set working directory
WORKDIR /app

# Copy your Lambda function code and other required files
COPY . .

# Ensure the permissions for SSH keys
COPY ./.ssh/id_rsa /root/.ssh/id_rsa
RUN chmod 600 /root/.ssh/id_rsa


# Build and invoke the Lambda function
CMD ["sh", "-c", "sam build && sam local invoke --container-host host.docker.internal --event events/game-dictionary-event.json GameDictionaryHandlerLambdaFunction"]

When I run the sam local invoke without host.docker.internal thing it gets stuck at the mounting /app.. step and don't go further. I'm thinking the error has to do something with it

docker-compose services:
  redis:
    image: redis:latest
    ports:
      - "0.0.0.0:6379:6379"
    networks:
      - backend
    expose:
      - "6379"
  redis-populator-service:
    build:
      context: ../casino-content-management
      dockerfile: Dockerfile
    networks:
      - backend
    depends_on:
      - redis
    volumes:
      - ../casino-content-management:/app
      - /var/run/docker.sock:/var/run/docker.sock
    environment:
      - SAM_CLI_CONTAINER_CONNECTION_TIMEOUT=99999999

template.yaml:

GameDictionaryHandlerLambdaFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: index.handler
      CodeUri: src/game_dictionary_lambda/
      Runtime: nodejs18.x
      Architectures:
        - x86_64
      MemorySize: 512
      Timeout: 600
    Metadata: # Manage esbuild properties
      BuildMethod: esbuild
      BuildProperties:
        Target: es2022
        Platform: node
        External:
          - "@aws-sdk/client-sns"
        EntryPoints:
          - index.ts

Upvotes: 0

Views: 140

Answers (0)

Related Questions