0

this is how my serverless.yml section looks like:

my-function:
      - http:  # <---- http
          method: POST
          path: /my-function/{id}
          request:
            parameters:
              paths: id:true

I want to use an AWS HTTP-API. So I change the http -> to httpApi like this:

my-function:
      - httpApi:  # <---- now httpApi
          method: POST
          path: /my-function/{id}
          request:
            parameters:
              paths: id:true

But I get this error message:

Serverless: Configuration warning at 'functions['my-function'].events[2].httpApi': unrecognized property 'request'

How do I define URL parameters in a httpApi section?

abnormi
  • 628
  • 1
  • 7
  • 15

3 Answers3

1

One suggestion, I sent 4 days with serverless only to realize that I need to understand Lambda and the whole architecture first. If you are new to the whole thing, I would skip serverless framework for now and then go back at it since it's very useful. Ok to your question:

This is the basic httpApi format:

functions:
  params:
    name: myLambdaName
    handler: index.handler
    events:
      - httpApi:
          path: /v1/test
          method: get

Here's the official documentation in case you need it.

This is how everything COULD look in the serverless.yml file, I put some comments so you understand what's going on:

service: my-express-application

frameworkVersion: "2"

provider:
  name: aws
  stackName: myName # Use a custom name for the CloudFormation stack
  runtime: nodejs12.x
  lambdaHashingVersion: 20201221
  stage: v1 # your default stage, the one lambda and all services define here will use
  region: us-east-1 # <- This is your regeion, make sure it is or change it
  httpApi: # rdefining your existing api gateway
    # id: xxx # id of externally created HTTP API to which endpoints should be attached. This will allow you to use it but this lambda can't modify it
    payload: "2.0" # the latest payload version by aws is 2.0
    name: "v1-my-service" # Use custom name for the API Gateway API, default is ${opt:stage, self:provider.stage, 'dev'}-${self:service} you will only be able to modify it if you created the api using this stack and not referencing it above
    cors: false # Implies default behavior, can be fine tuned with specficic options
  timeout: 10
  logRetentionInDays: 7 # Set the default RetentionInDays for a CloudWatch LogGroup

functions:
  params:
    name: myLambdaName
    handler: index.handler
    events:
      - httpApi:
          path: /v1/test
          method: get
ElKePoN
  • 822
  • 11
  • 21
  • Thank you very much for the very detailed explanation. However, it does not answer my question :( I wanted to know how I could define URLs with paramertes like: `/api/todos/{id}/edit` Anyway.. I switched to Koa and koa-router as my server framework and wrapping everything in single lambda function using `serverless-http` – abnormi Feb 17 '21 at 11:58
1
  1. You don't have to use "request...":

    handler: bin/function
       events:
         - httpApi:
             path: /function/{id}
             method: post
    
  2. in code (this case in go), just call the parameter of the following way:

    id:= request.PathParameters["id"]
    
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Rdsc
  • 11
  • 2
  • 1
    If your handling it in Python you can get the parameter as follows: `parameters = event.get('pathParameters', None) ` `id = parameters.get('Id', None) if parameters else None` – Ronnie Hyslop Feb 22 '22 at 15:00
0

Request Parameters To pass optional and required parameters to your functions, so you can use them in API Gateway tests and SDK generation, marking them as true will make them required, false will make them optional.

functions:
  create:
    handler: posts.create
    events:
      - httpApi:
          path: posts/create
          method: post
          request:
            parameters:
              querystrings:
                url: true
              headers:
                foo: false
              paths:
                bar: false

In order for path variables to work, API Gateway also needs them in the method path itself, like so:

    
functions:
  create:
    handler: posts.post_detail
    events:
      - httpApi:
          path: posts/{id}
          method: get
          request:
            parameters:
              paths:
                id: true

To map different values for request parameters, define the required and mappedValue properties of the request parameter.

    
functions:
  create:
    handler: posts.post_detail
    events:
      - httpApi:
          path: posts/{id}
          method: get
          request:
            parameters:
              paths:
                id: true
              headers:
                custom-header:
                  required: true
                  mappedValue: context.requestId