使用AWS SAM在本地运行AWS Lambda函数不会产生结果或日志,只是超时

时间:2019-07-08 14:16:17

标签: amazon-web-services go aws-lambda

说明:

由于某种原因,每当我尝试调用或调试Lambda函数时,AWS SAM始终会卡在安装步骤中。这是我的template.yml文件的样子:

AWSTemplateFormatVersion: '2010-09-09'

Transform: 'AWS::Serverless-2016-10-31'

FFMPEG:
  Type: AWS::Serverless::Application
  Properties:
    Location:
      ApplicationId: arn:aws:serverlessrepo:us-east-1:145266761615:applications/ffmpeg-lambda-layer
      SemanticVersion: 1.0.0

Resources:

  Thumbnail:
    Type: 'AWS::Serverless::Function'
    Properties:
      Handler: bin/thumbnail
      Runtime: go1.x
      Timeout: 300
      CodeUri: ./bin/thumbnail.zip
      Layers: 
         - !GetAtt FFMPEG.Outputs.LayerVersion
      Events:
        GetSegmentFrames:
          Type: Api
          Properties:
            Path: /ai/thumbnail
            Method: post

这是我使用的Makefile命令。请注意,我专门使用 run 命令:

# Go commands
GOCMD=go
GOBUILD=$(GOCMD) build
GOGET=$(GOCMD) get

# SAM commands
SAMCMD=sam
SAMPKG=$(SAMCMD) package
SAMDEPLOY=$(SAMCMD) deploy
SAMLOCAL=$(SAMCMD) local start-api
SAMINVOKE=$(SAMCMD) local invoke
DELVE_PATH = $(GOPATH)/dlv github.com/go-delve/delve/cmd/dlv

#  Debug main.go using AWS SAM
debug:
    GOARCH=amd64 GOOS=linux go build -gcflags='-N -l' -o $(THUMBNAIL_DEBUG_BINARY) $(THUMBNAIL_SOURCE)
    GOARCH=amd64 GOOS=linux go build -o $(DELVE_PATH)
    $(SAMLOCAL) -d 5986 --debugger-path $(GOPATH)

# Invoke Thumbnail Lambda's Handler Locally
run:
    $(SAMINVOKE) --event payload.json "Thumbnail"

我还可以共享构造处理函数的方式:

type Handler struct{}

func (handler Handler) Invoke(ctx context.Context, payload []byte) ([]byte, error) {
    glog.Info("Thumbnail Lambda handler has been Invoked..")
    lambdaConfig := config.Config{}
    thumbnailLambdaStatus := status.Initialize()

    apiGatewayEvent := new(events.APIGatewayProxyRequest)
    if err := json.Unmarshal(payload, apiGatewayEvent); err != nil {
        glog.Errorf("Error occurred while trying to unmarshal payload as APIGatewayProxyRequest. Error message - %v", err)
        return nil, err
    } else {
        if err := json.Unmarshal([]byte(apiGatewayEvent.Body), &lambdaConfig); err != nil {
            glog.Errorf("Error occurred while trying to unmarshal body of APIGatewayProxyRequest. Error message - %v", err)
            return nil, err
        }

        err := lambdaConfig.Validate()
        if err != nil {
            glog.Errorf("Error during validation of Config struct. Error message - %v", err)
            return nil, err
        }

        switch strings.ToUpper(apiGatewayEvent.HTTPMethod) {
        case "POST":
            resp, err := thumbnailLambda(&lambdaConfig, &thumbnailLambdaStatus)
            if err != nil {
                glog.Errorf("Error occurred while trying to execute Thumbnail Lambda's core logic. Error message - %v", err)
                return nil, err
            }

            glog.Infof("Response: %s", resp.Body)
            break
        default:
            err = fmt.Errorf("Invalid method: %s", apiGatewayEvent.HTTPMethod)
        }

    }

    return nil, nil
}

func main(){
         glog.Info("Starting Thumbnail Lambda ...")
         lambda.StartHandler(Handler{})
}

请注意,我正在尝试处理多个事件。目前,Lambda仅支持APIGatewayProxyRequest事件。该方法取自:How to support more than one trigger in AWS Lambda in Golang?

一段时间后,我得到以下输出:

START RequestId: adb8e970-8ccf-1b11-f4f4-d4e65ea5bd69 Version: $LATEST
END RequestId: adb8e970-8ccf-1b11-f4f4-d4e65ea5bd69
REPORT RequestId: adb8e970-8ccf-1b11-f4f4-d4e65ea5bd69  Duration: 300000.00 ms  Billed Duration: 300000 ms      Memory Size: 128 MB     Max Memory Used: 0 MB
{
  "errorMessage": "2019-07-04T07:46:00.692Z adb8e970-8ccf-1b11-f4f4-d4e65ea5bd69 Task timed out after 300.00 seconds"
}
2019-07-04 09:46:00 Function 'Thumbnail' timed out after 300 seconds

让我感到困惑的是,我怎么没收到日志或任何东西?

观察到的结果:

am local invoke --event payload.json "Thumbnail"
2019-07-04 09:26:10 Found credentials in shared credentials file: ~/.aws/credentials
2019-07-04 09:26:10 Invoking bin/thumbnail (go1.x)

Fetching lambci/lambda:go1.x Docker container image......
2019-07-04 09:26:12 Mounting /home/stefan/go/src/pipeline as /var/task:ro,delegated inside runtime container

预期结果:

我希望处理程序函数被调用。

0 个答案:

没有答案