Sitemap

Serverless Framework setup

6 min readOct 28, 2023

--

Press enter or click to view image in full size

Description:

Serverless Framework is a CLI tool that makes it easy to build, deploy and update the Serverless architecture and applications on AWS. Some use cases are for APIs, Scheduled Tasks, Event Handlers, Streaming Data Pipelines, Web Sockets & more; It supports Node.js, Python, Java, Go, C#, Ruby, Swift, Kotlin, PHP, Scala, & F#. Serverless is loaded to make sure best practices are observed, it has plugins that allow you to expand it current capabilities, and support multi-environments (e.g. production, staging, development, and etc…).

Installation:

To get started with Serverless Framework, make sure you have Node.js Installed locally. Install serverless by running.

npm install -g serverless

Once installed, run the command below to log into or create your Serverless account, it will open a login page via a browser and all you need to do is enter your login info.

serverless login

Once logged in, navigate to the directory where you aim to create the Serverless project, and use this command to create/pull a template of multiple possible projects you’re able to create with serverless.

serverless

Notice that “sls” can be used as a short hand where “serverless” is applicable

Once you choose your template, choose “NO” for the deploy now option, and you should see the contents of the template appear pre-loaded with a README.md, a serverless.yml file, and an index.js file in our case (shown in picture below).

Note that the format below can also be used to create a service/project

serverless create --template aws-nodejs --path my-service

Configuration:

Index.js and ReadMe:

To prepare your files for a successful deploy. Since our example is deploying a Node.js lambda function, cd into the “index.js” file and create your lambda code as you would normally.

Once done with your lambda code, open the serverless.yml file to edit the configurations to your needs.

Serverless.yml:

Here is a sample template you can copy and paste into the beginning of your file. Explanation for each variable used is also below.

org: MyOrg
app: Tests
service: test-project
frameworkVersion: '3'
configValidationMode: warn
provider:
name: aws
runtime: nodejs14.x <- change to latest
profile: Bert
memorySize: 128
region: us-east-1
timeout: 30
stage: ${opt:stage}
apiGateway:
apiKeys:
- name: testproject-${opt:stage}
binaryMediaTypes:
- 'multipart/form-data'
plugins:
- serverless-offline
- serverless-dotenv-plugin
functions:
api:
handler: mine.handler
role: ${env:IAM_ROLE}
destinations:
onFailure: ${env:SNS_ARN}

Breakdown of .yml file:

org: MyOrg = The name of the organization that owns the service.
app: Tests = The name of the application.
service: test-project = The name of the service.
frameworkVersion: ‘3’ = The version of the Serverless Framework to use.
configValidationMode: warn = Sets the configuration validation mode to warn. This means that the Serverless Framework will log warnings if it detects any configuration errors, but it will not prevent the service from being deployed.

provider: = This section and the following information under it contains configuration for the AWS lambda, and the overall infrastructure.

stage: the ${opt:stage} allows you to set the stage to the value option set when the function is deployed ( you can set the stage using the flag “-s” followed by the stage you want, e.g. production, dev, staging…)

apiGateway.binaryMediaTypes: = This contains the binary media types that will be accepted by the API Gateway. “multipart/form-data” Sets the first binary media type to multipart/form-data.

plugins: This contains 2 plugins, the “serverless-offline” plugin allows you to run and test your Lambda functions locally, and the “serverless-dotenv-plugin” allows you to load environment variables from .env files.

functions: the handler for the api Lambda function is “mine.handler”. This means that the Serverless Framework will look for a function called handler in the mine.js file.

role and destination: these are used to further configure the lambda, we use a “.env” file to keep sensitive information from being hardcoded.

.env files

Get Bert Oubida’s stories in your inbox

Join Medium for free to get updates from this writer.

(pre-requisite is “useDotenv: true” in plugins section of yml file) the create a “.env” file, and add your environment after it to specify different resources per environment (e.g. .env.staging, or .env.production). Values like an IAM role’s ARN can be stored in your .env file in this format “IAM_ROLE=arn:aws:iam::9999999999:role/lambda_basic_execution”, and pulled/called into your code using “${env:IAM_ROLE}”.

Launch:

Serverless deploy:

Once your yaml file, and lambda code file is set, simply run the code below to create the app and service mentioned in the serverless.yml file in the account of the also mentioned organization, and also the cloudformation stack needed to launch all your other resources in aws.

serverless deploy --stage sandbox --region us-east-1

To deploy a packaged app created locally, run the command below

serverless deploy --package path-to-package

Shortcut to update just the function:

To deploy code changes quickly, skip the serverless deploy command which is much slower since it triggers a full AWS CloudFormation update. Instead, deploy code and configuration changes to individual AWS Lambda functions in seconds via the deploy function command, with -f [function name in serverless.yml] set to the function you want to deploy.

serverless deploy function -f functionName

Installing plugins and using Serverless offline:

To use Serverless Offline, you must first install the plugin:

npm install serverless-offline

Once you’re done installing, your serverless files are all set ( MUST HAVE “- serverless-dotenv-plugin” IN THE PLUGINS section), and you’re ready to test it all out, you can access your lambda function on “http://localhost:3000” after you start it with the command below

serverless offline start

You can then use GET or POST requests to the lambda, or invoke it with “serverless offline invoke <function name>”.

Serverless Commands:

1. Commands

Note that “sls” can be used in place of “serverless” wherever the latter is applicable.

  1. Displays information about the Serverless project:
serverless help

2. Allows you to log into your Serverless account via a browse:

serverless login

3. Creates a new Serverless project:

serverless create

4. Deploys the Serverless project to the Serverless console and AWS:

serverless deploy

5. Installs, manages, and uses Serverless plugins:

serverless plugin

6. Manage service-level or provider-level configurations:

serverless config

7. Invokes a Lambda function in the Serverless project:

serverless invoke

8. Use this to package your project without deploying it to AWS. This is best used with CI / CD workflows to ensure consistent deployable artifacts:

serverless package

9. Displays information about the Serverless project:

serverless info

10. Displays the logs for a Lambda function in the Serverless:

serverless logs

11. Updates the Serverless Framework to the latest version:

serverless update

12. Removes the Serverless project from AWS:

serverless remove

13. When used concurrently with “deploy” or “plugin” , it lists the deployment history of your service, or the plugins you have available:

serverless <command> list

2. Flags:

  • s, — stage : Use this to set the stage in which to deploy or invoke the serverless service.
  • r, — region : Use this to set the region to deploy the service to if not pre-set in your “serverless.yml” file.
  • -v, — verbose: To outputs more verbose information.
  • -d, — debug: To outputs debug information.
  • -f, — function : Use this to specify which function to invoke

Example:

serverless deploy -s sandbox -r us-east-1

--

--