aws / aws-lambda-go

Libraries, samples and tools to help Go developers develop AWS Lambda functions.
Apache License 2.0
3.62k stars 550 forks source link

Context deadline exceeded at start locally #361

Open losnappas opened 3 years ago

losnappas commented 3 years ago

I'm running the lambda image locally, and long story short, it starts off with an expired context deadline.

package main

import (
    "context"
    "log"

    "github.com/aws/aws-lambda-go/lambda"
)

func Handler(ctx context.Context) {
    log.Println(ctx.Err())
    log.Println(ctx.Deadline())
}
func main() {
    lambda.Start(Handler)
}
# installer
FROM golang:1.15-alpine3.12@sha256:4d8abd16b03209b30b48f69a2e10347aacf7ce65d8f9f685e8c3e20a512234d9 as installer
WORKDIR /build

COPY ./go.mod ./go.sum ./

RUN go mod download

# builder
FROM installer as builder

COPY . ./

RUN CGO_ENABLED=0 GOOS=linux go build -o Main ./main.go

# release
FROM public.ecr.aws/lambda/go:latest

COPY --from=builder /build/Main .

CMD ["./Main"]

curl localhost:8080/2015-03-31/functions/function/invocations

logs

time="2021-02-27T23:20:12.39" level=info msg="exec '/var/runtime/bootstrap' (cwd=/var/task, handler=)"
time="2021-02-27T23:20:48.623" level=info msg="extensionsDisabledByLayer(/opt/disable-extensions-jwigqn8j) -> stat /opt/disable-extensions-jwigqn8j: no such file or directory"
time="2021-02-27T23:20:48.624" level=warning msg="Cannot list external agents" error="open /opt/extensions: no such file or directory"
START RequestId: 9af7aefe-d612-4f5e-9e75-c9cdeadab9f5 Version: $LATEST
2021/02/27 23:20:48 context deadline exceeded
2021/02/27 23:20:48 2021-02-27 23:20:48.631152628 +0000 UTC true
END RequestId: 9af7aefe-d612-4f5e-9e75-c9cdeadab9f5
REPORT RequestId: 9af7aefe-d612-4f5e-9e75-c9cdeadab9f5  Init Duration: 0.20 ms  Duration: 7.73 ms       Billed Duration: 100 ms        Memory Size: 3008 MB    Max Memory Used: 3008 MB

"context deadline exceeded"

What am I missing? Here it says

Context methods Deadline – Returns the date that the execution times out, in Unix time milliseconds.

But it's essentially returning the starting time instead.

JadenSimon commented 3 years ago

I'm having this issue as well using SAM CLI. Is there some sort of parameter or environment variable that needs to be set for the context deadline?

drwxmrrs commented 3 years ago

Can confirm I'm also having this issue when calling Dynamo with the Lambda context in SAM local:

    _, err = ddb.PutItemWithContext(ctx, &piu) // Deadline exceeded..

Looks like when the CTX arrives to the handler it's in the past:

Lambda CTX deadline: 2021-04-27 10:07:09.7504344 +0000 UTC
Time now: 2021-04-27 10:07:09.7511904 +0000 UTC

Downgrading my version of SAM to 1.12.0 fixed the issue:

pip3 install --user 'aws-sam-cli==1.12.0'

Also, related:

https://github.com/aws/aws-sam-cli/issues/2510 https://github.com/aws/aws-sam-cli/issues/2519

annymosse commented 3 years ago

i have same issue :

to produce the problem:

amplify add function testFunc

then choose go with default config ,then:

amplify mock function testFunc

the output:

? Provide the path to the event JSON object relative to ..../amplify/backend/function/testFunc src/event.json
Ensuring latest function changes are built...
Starting execution...
Local invoker binary was not found, building it...
go: github.com/aws/aws-lambda-go@v1.15.0: missing go.sum entry; to add it:
        go mod download github.com/aws/aws-lambda-go
testFunc failed with the following error:
Error: Command failed with exit code 1: go build main.go
    at makeError (/home/me/.config/yarn/global/node_modules/execa/lib/error.js:59:11)
    at Function.module.exports.sync (/home/me/.config/yarn/global/node_modules/execa/index.js:188:17)
    at Object.executeCommand (/home/me/.config/yarn/global/node_modules/amplify-go-function-runtime-provider/src/runtime.ts:31:24)
    at buildLocalInvoker (/home/me/.config/yarn/global/node_modules/amplify-go-function-runtime-provider/src/localinvoke.ts:29:5)
    at Object.localInvoke (/home/me/.config/yarn/global/node_modules/amplify-go-function-runtime-provider/src/localinvoke.ts:67:30)
    at Object.invoke (/home/me/.config/yarn/global/node_modules/amplify-go-function-runtime-provider/src/index.ts:26:24)
    at /home/me/.config/yarn/global/node_modules/amplify-category-function/src/index.ts:185:20
    at Object.start (/home/me/.config/yarn/global/node_modules/amplify-util-mock/src/func/index.ts:48:49)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at Object.run (/home/me/.config/yarn/global/node_modules/amplify-util-mock/src/commands/mock/function.ts:14:5)
    at Object.executeAmplifyCommand (/home/me/.config/yarn/global/node_modules/amplify-util-mock/src/amplify-plugin-index.ts:8:3)
    at executePluginModuleCommand (/home/me/.config/yarn/global/node_modules/@aws-amplify/cli/src/execution-manager.ts:175:3)
    at Object.executeCommand (/home/me/.config/yarn/global/node_modules/@aws-amplify/cli/src/execution-manager.ts:28:5)
    at Object.run (/home/me/.config/yarn/global/node_modules/@aws-amplify/cli/src/index.ts:139:5) {
  shortMessage: 'Command failed with exit code 1: go build main.go',
  command: 'go build main.go',
  exitCode: 1,
  signal: undefined,
  signalDescription: undefined,
  stdout: undefined,
  stderr: undefined,
  failed: true,
  timedOut: false,
  isCanceled: false,
  killed: false
}
Finished execution.

whenever i built the same function and others functions and tested them using Dockerfile:

FROM amazon/aws-lambda-go:latest

COPY main ${LAMBDA_TASK_ROOT}

CMD [ "main" ]

and trigger it by curl:

curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{"payload":"world"}'
>hello world!

Note there's no issue when using nodejs.

EltonGarcia commented 3 years ago

Still facing the issue with SAM CLI version 1.25.0 on Ubuntu. Any workarounds for that?

andrew-yustyk commented 3 years ago

SAM CLI, version 1.26.0 The same issue on Ubuntu

robin-sanner commented 3 years ago

Same issue on MacOS SAM CLI, version 1.29.0

zkhan96 commented 3 years ago

Same issue on MacOS SAM CLI, version 1.29.0