使用模板向AWS Lambda事件添加参数

Zac*_*scs 5 amazon-web-services aws-cloudformation aws-lambda

如何使用云形成模板向lambda函数事件添加(路径)参数?

奇怪地使用:

DeleteItem:
          Type: Api
          Properties:
            Path: /item/{id}
            Method: delete
            Request:
            Parameters:
              Paths:
                id: true
Run Code Online (Sandbox Code Playgroud)

使用aws-sam-cli工作。但是,当我尝试使用云形成进行部署时,它说未定义属性Request。我从无服务器文档中得到了这个请求的想法,但似乎只能在本地工作。我在模板中找不到有关如何执行此操作的文档,因此将不胜感激任何帮助。

tho*_*ace 7

无服务器框架使用其自己的语法,该语法与SAM不同(尽管可以编译为SAM或原始CloudFormation)。

您可以在此处找到SAM规范。

它不是明确的,但是您需要做的就是使用{path-name}语法。不需要(或不支持)添加Request/ Parameters

例如:

Ratings:
  Type: AWS::Serverless::Function
  Properties:
    Handler: ratings.handler
    Runtime: python3.6
    Events:
      Api:
        Type: Api
        Properties:
          Path: /ratings/{id}
          Method: get
Run Code Online (Sandbox Code Playgroud)

会给您一个事件:

event.pathParameters.id == 'whatever-was-put-in-the-id-position'

(可以在这里找到一个很长的示例:https : //github.com/1Strategy/redirect/blob/master/redirect.yaml

  • @WaqasShah-您不必指定查询字符串参数(您不能指定afaik),它们只能在“ event.queryStringParameters”下使用:https://docs.aws.amazon.com/lambda/latest/ dg / eventsources.html#eventsources-api-gateway-request (2认同)