Search code examples
serverless-frameworkserverlessaws-serverlessserverless-architecture

Create an AWS HTTP API as resource in serverless


How can I create an HTTP API with serverless framework so I can manage it in my .yml file? The documentations shows this:

functions:
  testFunc:
    name: lambdaName
    handler: handler.hello
    events:
      - httpApi:
          path: /v1/test
          method: get

That would create the api but dependant on the function, how can I create the API here to configure it and use it on the function?

I want to be able to manage it on my .yml file


Solution

  • Ok, I figured it out. When a new lambda function is created using serverless framework you can define the API type as shown above in my question. Now since the stack is expecting that type I can also modify it in my globals (aka provider), and even use the id to share the resource across other lambda functions, for example:

    provider:
      name: aws
      runtime: nodejs12.x
      lambdaHashingVersion: 20201221
      stage: v1 
      region: us-east-1 # <- This is your regeion, make sure it is or change it
      httpApi:
        # id: xxx 
        payload: "2.0" 
        name: "v1-my-service"
        cors: false