0

这就是我的serverless.yml部分的样子:

my-function:
      - http:  # <---- http
          method: POST
          path: /my-function/{id}
          request:
            parameters:
              paths: id:true

我想使用 AWS HTTP-API。所以我把http->改成httpApi这样:

my-function:
      - httpApi:  # <---- now httpApi
          method: POST
          path: /my-function/{id}
          request:
            parameters:
              paths: id:true

但我收到此错误消息:

Serverless: Configuration warning at 'functions['my-function'].events[2].httpApi': unrecognized property 'request'

如何在httpApi部分中定义 URL 参数?

4

2 回答 2

1
  1. 您不必使用“请求...”:

    handler: bin/function
       events:
         - httpApi:
             path: /function/{id}
             method: post
    
  2. 在代码中(本例在go中),只需通过以下方式调用参数:

    id:= request.PathParameters["id"]
    
于 2021-07-07T16:59:00.590 回答
0

一个建议,我用无服务器发送了 4 天,才意识到我需要先了解 Lambda 和整个架构。如果您对整个事情不熟悉,我会暂时跳过无服务器框架,然后再回头看,因为它非常有用。好的,你的问题:

这是基本的 httpApi 格式:

functions:
  params:
    name: myLambdaName
    handler: index.handler
    events:
      - httpApi:
          path: /v1/test
          method: get

这是官方文档,以备不时之需。

这就是 serverless.yml 文件中所有内容的外观,我发表了一些评论,以便您了解发生了什么:

service: my-express-application

frameworkVersion: "2"

provider:
  name: aws
  stackName: myName # Use a custom name for the CloudFormation stack
  runtime: nodejs12.x
  lambdaHashingVersion: 20201221
  stage: v1 # your default stage, the one lambda and all services define here will use
  region: us-east-1 # <- This is your regeion, make sure it is or change it
  httpApi: # rdefining your existing api gateway
    # id: xxx # id of externally created HTTP API to which endpoints should be attached. This will allow you to use it but this lambda can't modify it
    payload: "2.0" # the latest payload version by aws is 2.0
    name: "v1-my-service" # Use custom name for the API Gateway API, default is ${opt:stage, self:provider.stage, 'dev'}-${self:service} you will only be able to modify it if you created the api using this stack and not referencing it above
    cors: false # Implies default behavior, can be fine tuned with specficic options
  timeout: 10
  logRetentionInDays: 7 # Set the default RetentionInDays for a CloudWatch LogGroup

functions:
  params:
    name: myLambdaName
    handler: index.handler
    events:
      - httpApi:
          path: /v1/test
          method: get
于 2021-01-24T01:55:44.483 回答