How to run a Lambda Docker with serverless offline
Asked Answered
K

2

7

I would like to run serverless offline using a Lambda function that points to a Docker image.

When I try to run serverless offline, I am just receiving:

Offline [http for lambda] listening on http://localhost:3002
Function names exposed for local invocation by aws-sdk:
           * hello-function: sample-app3-dev-hello-function

If I try to access http://localhost:3002/hello, a 404 error is returned


serverless.yml

service: sample-app3
frameworkVersion: '3'

plugins:
  - serverless-offline

provider:
  name: aws
  ecr:
    images:
      sampleapp3image:
        path: ./app/
        platform: linux/amd64

functions:
  hello-function:
    image:
      name: sampleapp3image
    events:
      - httpApi:
          path: /hello
          method: GET

app/myfunction.py

def lambda_handler(event, context):
    return {
        'statusCode': 200,
        'body': 'Hello World!'
    }

app/Dockerfile

FROM public.ecr.aws/lambda/python:3.9

COPY myfunction.py ./

CMD ["myfunction.lambda_handler"]
Knobby answered 3/2, 2022 at 9:7 Comment(0)
F
2

at the moment such functionality is not supported in serverless-offline plugin. There's an issue open where the discussion started around supporting this use case: https://github.com/dherault/serverless-offline/issues/1324

Fleet answered 3/2, 2022 at 9:29 Comment(0)
U
2

I had the same issue.

Fixed adding --host 0.0.0.0 when running serverless offline in the docker container.

sls offline --stage dev --host 0.0.0.0

Upthrow answered 3/8, 2023 at 8:52 Comment(1)
I spent hours trying to make that work thinking there was something wrong with my docker side config... this solution finally worked! thank youBegley

© 2022 - 2024 — McMap. All rights reserved.