abnormi
abnormi

Reputation: 688

serverless: request parameters in a httpApi

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?

Upvotes: 1

Views: 6055

Answers (3)

marcelocarmona
marcelocarmona

Reputation: 59

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

Upvotes: -1

Rdsc
Rdsc

Reputation: 31

  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"]
    

Upvotes: 3

ElKePoN
ElKePoN

Reputation: 930

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

Upvotes: 0

Related Questions