Skip to content

Serverless

Kironb edited this page Jun 16, 2020 · 1 revision

The Serverless YML file

In terms of the general information behind how the serverless yml file works we have a couple of sections worth investigating:

Provider Section

The provider section, as defined like so:

provider:
  name: aws
  runtime: python3.6
  stage: dev

defines the runtime environment we are using (python), as well as the default deployment stage and provider name.

Plugins Section

The plugins section involves various ways of extending serverless' capabilities. Here we list offline serverless (in order to run serverless locally), as well as serverless python requirements (in order to bundle everything from the requirements.txt file).

plugins:
  - serverless-python-requirements
  - serverless-offline

Package Section

Our package section can be used to specify specific packaging details. We use this to include and exclude certain files for the deployment of serverless. We include all of our python files in the current directory for deployment.

package:
    include: 
        ...
    exclude:
        ...

Functions Section

In our functions section is where we list all of the necessary lambdas for deployment.

functions:
  authorize:
    handler: authorize.authorize
    events:
      - http:
          path: authorize
          integration: lambda
          method: post
          cors: true
          request:
            template:
              application/json: '$input.body'

Taking the authorize function as an example, we provide the function to deploy by first specifying the file name "authorize" and the specific function within it (which also happens to be named authorize). To clarify the pattern it goes:

file_name.function

We then define the events which trigger our lambda to run. Since it is a lambda designed for our backend open on the web, we say it requires an HTTP request via -http. We specify the path from the deployment address to be /authorize via the path parameter. Finally we specify it is a lambda that requires a POST request to the endpoint as well as other features such as requiring cors. We list the required template as well for making requests to the endpoint.

Extra Information For Deployment

To actually deploy, you can deploy all of the functions at once with sls deploy as in the getting started guide while specifying the stage or you can deploy a specific function via:

serverless deploy function --function myFunction

The functions sections will likely prove to be the most likely to change overtime as it will need to change whenever we want to make a new endpoint or adjust an old lambda function name or method.

More details can be found in the serverless docs: https://www.serverless.com/framework/docs/providers/aws/guide/packaging/.