简体   繁体   中英

How to add the taking of a parameter in AWS CloudFormation

I am using an AWS Serverless Function to create API events. Currently I am working on my GET event and it works the way I want it to with the given URL from AWS. I want to be able to put a / and then a number and be able to get this number. For example, it would be myurl/1 . That number would be used to get a specific id versus the original function which gets all ID's. I am using a CloudFormation template so it needs to be something I put into the template. The event section currently looks like:

Events:
    GetEvent:
      Type: Api
      Properties:
        Path: /
        Method: get

I need to know where to add a section to be able to accept a number at the end of the path so that I can use it in my code.

You can add the event as follows

functions
  get:
      handler: myurl/get.get
      events:
        - http:
            path: myurl/{id}
            method: get
            cors: true

Note that above is not a part of CloudFormation resources section in serverless.yml.

Inside your event handler code in Lambda, you should be able to access the id parameter using the event object as follows

event.pathParameters.id

Refer this Serverless example for more details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM