【问题标题】:Adding parameters to aws lambda events using templates使用模板向 aws lambda 事件添加参数
【发布时间】:2019-04-12 14:36:03
【问题描述】:

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

奇怪的使用:

DeleteItem:
          Type: Api
          Properties:
            Path: /item/{id}
            Method: delete
            Request:
            Parameters:
              Paths:
                id: true

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

【问题讨论】:

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


    【解决方案1】:

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

    您可以找到 SAM 规范 here

    这不是明确的,但您需要做的就是使用{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
    

    会给你一个事件:

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

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

    【讨论】:

    • 有没有办法指定查询字符串参数?我无法从 SAM 文档中找到答案。
    • @WaqasShah - 你不必指定查询字符串参数(afaik 你不能),它们只在event.queryStringParameters 下可用:docs.aws.amazon.com/lambda/latest/dg/…
    • @thomasmichaelwallace:是的,这些都是可用的,但是如果我想根据需要标记一些查询参数,就像我可以通过控制台执行的操作一样。有没有办法在 SAM 模板中定义它。谢谢!
    猜你喜欢
    • 2023-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-25
    • 1970-01-01
    • 1970-01-01
    • 2016-09-30
    相关资源
    最近更新 更多