Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 587
Zachscs Avatar asked Nov 09 '18 01:11

Zachscs


People also ask

How do you call a Lambda function in CloudFormation template?

In an AWS CloudFormation template, you can specify a Lambda function as the target of a custom resource. Use custom resources to process parameters, retrieve configuration values, or call other AWS services during stack lifecycle events. The following example invokes a function that's defined elsewhere in the template.

Which section of the CloudFormation template should you use to define your Lambda resources?

To automatically deploy a Lambda function, we will add information under the Resources section. Create a YAML file for your new template (this tutorial will use YAML, but CloudFormation also supports JSON format). Under the Resources section, we'll start by defining the Identity and Access Management (IAM) Role.

Is it possible to trigger a Lambda on creation from CloudFormation template?

Yes, it is possible. Here are a few options: Manually create an SNS Topic. Add an AWS::SNS::Subscription to your stack with the lambda function as the Endpoint and the SNS topic as the TopicArn .


1 Answers

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)

like image 137
thomasmichaelwallace Avatar answered Oct 31 '22 11:10

thomasmichaelwallace