question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Sam local invoke (go) not honoring timeout in context

See original GitHub issue

Description:

When running a golang lambda function with sam local invoke, and making a request (in the function) using the context results in RequestCanceled: request context canceled caused by: context deadline exceeded.

When using ListBuckets (without WithContext) results in a successful result.

Steps to reproduce:

hello.go

type Response events.APIGatewayProxyResponse

func Handler(ctx context.Context) (Response, error) {
	fmt.Printf("%+v\n", ctx)
	var buf bytes.Buffer

	sess := session.Must(session.NewSession(&aws.Config{Region: aws.String("us-west-2")}))
	svc := s3.New(sess)

	// Using the WithContext version of ListBuckets makes it apparent that the context isn't set to honor the function timeout
	b, err := svc.ListBucketsWithContext(ctx, &s3.ListBucketsInput{})
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(b)

	body, err := json.Marshal(map[string]interface{}{
		"message": "Go Serverless v1.0! Your function executed successfully!",
	})
	if err != nil {
		return Response{StatusCode: 404}, err
	}
	json.HTMLEscape(&buf, body)

	resp := Response{
		StatusCode:      200,
		IsBase64Encoded: false,
		Body:            buf.String(),
		Headers: map[string]string{
			"Content-Type":           "application/json",
			"X-MyCompany-Func-Reply": "hello-handler",
		},
	}

	return resp, nil
}

func main() {
	lambda.Start(Handler)
}

template.yml

AWSTemplateFormatVersion: "2010-09-09"
Transform: "AWS::Serverless-2016-10-31"
Description: "Demo SAM Template"
Resources:
  Demo:
    Type: "AWS::Serverless::Function"
    Properties:
      Handler: bin/hello
      Runtime: go1.x
      MemorySize: 128
      Timeout: 30
      Environment:
        Variables:
          AWS_ACCESS_KEY_ID: myaccesskey
          AWS_SECRET_ACCESS_KEY: mysecretkey
          AWS_SESSION_TOKEN: mytoken
      Policies:
        - Version: "2012-10-17"
          Statement:
            - Effect: Allow
              Action:
                - "s3:*"
              Resource: "*"

Observed result:

RequestCanceled: request context canceled caused by: context deadline exceeded

Expected result:

{
  Buckets: [
    {
      CreationDate: 2020-04-21 00:00:00 +0000 UTC,
      Name: "bucket-names"
    }
  ],
  Owner: {
    DisplayName: "...",
    ID: "..."
  }
}

Additional environment details (Ex: Windows, Mac, Amazon Linux etc)

  1. OS: macOS 10.15.7
  2. sam --version: SAM CLI, version 1.15.

Add --debug flag to command you are running

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:17
  • Comments:17 (3 by maintainers)

github_iconTop GitHub Comments

6reactions
mousedownmikecommented, Feb 27, 2021

I rolled back to SAM CLI, version 1.12.0 to get this working again.

2reactions
mnugtercommented, Feb 18, 2021

I’m experiencing this issue as well while it was working earlier on when I had v1.15. I’ve tried upgrading to v1.18.1 but that didn’t help. I have the exact same problem as @momotaro98 described.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Sam local invoke (go) not honoring timeout in context
Description: When running a golang lambda function with sam local invoke , and making a request (in the function) using the context results ......
Read more >
Timeout while waiting for command: "sam local invoke"
I'm building server less applications using AWS SAM. SAM + VSCode + Docker desktop. Everything was working fine ...
Read more >
ClientConfiguration (AWS SDK for Java - 1.12.369)
Returns whether or not to cache response metadata. int, getClientExecutionTimeout(). Returns the amount of time (in milliseconds) to allow the client to ...
Read more >
How to Test Serverless Applications - - KTEN
Software or even hardware of any sort will first be tested before it goes to ... While performing integration testing, you'll invoke the...
Read more >
WebDriver - W3C
A local end would only send this capability if it expected it to be honored and the configured proxy used. The intent is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found