Zachscs
Zachscs

Reputation: 3663

Adding parameters to aws lambda events using templates

How do I add a (path) parameter to lambda function events using cloud formation templates?

Strangely using:

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

works using aws-sam-cli. However when I try to deploy using cloud formation it says that property Request is not defined. I got this request idea from the serverless docs but seems to only work locally. I can't find documentation on how to do this in templates so any help would be greatly appreciated.

Upvotes: 9

Views: 15009

Answers (1)

thomasmichaelwallace
thomasmichaelwallace

Reputation: 8464

The Serverless Framework uses its own syntax, which is different from SAM (although can be compiled down to SAM or raw CloudFormation).

You can find the SAM specification here.

It's not explicit, but all you need to do is use the {path-name} syntax. Adding Request/Parameters is not required (or supported).

For example:

Ratings:
  Type: AWS::Serverless::Function
  Properties:
    Handler: ratings.handler
    Runtime: python3.6
    Events:
      Api:
        Type: Api
        Properties:
          Path: /ratings/{id}
          Method: get

Would give you the an event with:

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

(A long example can be found here: https://github.com/1Strategy/redirect/blob/master/redirect.yaml)

Upvotes: 24

Related Questions