无服务器:httpApi中的请求参数

时间:2020-11-05 16:24:50

标签: aws-lambda serverless aws-http-api

我的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参数?

2 个答案:

答案 0 :(得分:0)

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

这是基本的httpApi格式:

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

这里有 official documentation 以备不时之需。

这是 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

答案 1 :(得分:0)

我不知道是不是太晚了,但这就是答案,

  1. 您不必使用“请求...”
handler: bin/function events: - httpApi: path: /function/{id} method: post
  1. 在代码中,(本例在go中),只需调用如下方式的参数

    id:= request.PathParameters["id"]