9

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.

1 Answer 1

24

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)

Sign up to request clarification or add additional context in comments.

3 Comments

Is there a way to specify query string parameters? I couldn't figure from the SAM docs.
@WaqasShah - you don't have to specify query string parameters (afaik you can't), they'll just be available under event.queryStringParameters: docs.aws.amazon.com/lambda/latest/dg/…
@thomasmichaelwallace: Yes these are available but if I want to mark some query param as required like what i can do via console. Is there any way to define that in SAM template. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.