How to create AWS Lambda, Azure Function, Google Cloud Function with Serverless Framework CLI

Serverless framework is a CLI to build cloud applications. There are more than 10 Serverless Infrastructure Providers like AWS, Azure and GCP as shown below screenshot.

Find more Serverless Framework CLI command in Azure Serverless TypeScript project in GitHub

According to my experience, Serverless Framework gives a better support for AWS provider, for example, if you see project template below, there are many project templates for AWS but very less support for other providers.

Find more details in https://www.serverless.com/framework/docs/getting-started

When i installed serverless@4.4.7, help options weren't working. For example, sls --help or serverless --help show error. So, i use serverless@3.39.0 which works very well with Nodejs v20.18.0(install with asdf global nodejs 20.18.0

$ npm i -g serverless@3.39.0

use serverless or sls in terminal.

$ serverless --help
Serverless Framework v3.34.0

Usage
serverless <command> <options>
sls <command> <options>

Get started
Run serverless to interactively setup a project.
Use --help-interactive to display the interactive setup help.

Monitoring
Enable performance and error monitoring with the Serverless Dashboard.
Learn more: https://serverless.com/monitoring

Plugins
Extend the Serverless Framework with plugins.
Explore plugins: https://serverless.com/plugins

Options
--help / -h                     Show this message
--version / -v                  Show version info
--verbose                       Show verbose logs
--debug                         Namespace of debug logs to expose (use "*" to display all)

Main commands
deploy                          Deploy a Serverless service
deploy function                 Deploy a single function from the service
info                            Display information about the service
invoke                          Invoke a deployed function
invoke local                    Invoke function locally
logs                            Output the logs of a deployed function

Other commands
deploy list                     List deployed version of your Serverless Service
deploy list functions           List all the deployed functions and their versions
metrics                         Show metrics for a specific function
remove                          Remove Serverless service and all resources
rollback                        Rollback the Serverless service to a specific deployment
rollback function               Rollback the function to the previous version
test                            Run HTTP tests
package                         Packages a Serverless service
plugin install                  Install and add a plugin to your service
plugin uninstall                Uninstall and remove a plugin from your service
print                           Print your compiled and resolved config file
config                          Configure Serverless
config credentials              Configures a new provider profile for the Serverless Framework
create                          Create new Serverless service
dashboard                       Open the Serverless dashboard
doctor                          Print status on reported deprecations triggered in the last command run
generate-event                  Generate event
help                            Show this help
install                         Install a Serverless service from GitHub or a plugin from the Serverless registry
login                           Login or sign up for Serverless
logout                          Logout from Serverless
output get                      Get value of dashboard deployment profile parameter
output list                     List all dashboard deployment profile parameters
param get                       Get value of dashboard service output
param list                      List all dashboard deployment profile parameters
plugin list                     Lists all available plugins
plugin search                   Search for plugins
slstats                         Enable or disable stats
$ aws % serverless create --help
create                          Create new Serverless service
--template / -t                 Template for the service. Available templates: 
                                             "aws-clojure-gradle", "aws-clojurescript-gradle", "aws-nodejs", "aws-nodejs-docker", "aws-nodejs-typescript", "aws-alexa-typescript", "aws-nodejs-ecma-script", "aws-python"
                                             "aws-python3", "aws-python-docker", "aws-groovy-gradle", "aws-java-maven", "aws-java-gradle", "aws-kotlin-jvm-maven", "aws-kotlin-jvm-gradle", "aws-kotlin-jvm-gradle-kts"
                                             "aws-kotlin-nodejs-gradle", "aws-scala-sbt", "aws-csharp", "aws-fsharp", "aws-go", "aws-go-dep", "aws-go-mod", "aws-ruby"
                                             "aws-provided"
                                             "tencent-go", "tencent-nodejs", "tencent-python", "tencent-php"
                                             "azure-csharp", "azure-nodejs", "azure-nodejs-typescript", "azure-python"
                                             "cloudflare-workers", "cloudflare-workers-enterprise", "cloudflare-workers-rust"
                                             "fn-nodejs", "fn-go"
                                             "google-nodejs", "google-nodejs-typescript", "google-python", "google-go"
                                             "kubeless-python", "kubeless-nodejs"
                                             "knative-docker"
                                             "openwhisk-java-maven", "openwhisk-nodejs", "openwhisk-php", "openwhisk-python", "openwhisk-ruby", "openwhisk-swift"
                                             "spotinst-nodejs", "spotinst-python", "spotinst-ruby", "spotinst-java8"
                                             "twilio-nodejs"
                                             "aliyun-nodejs"
                                             "plugin"
                                             "hello-world"
--template-url / -u             Template URL for the service. Supports: GitHub, BitBucket
--template-path                 Template local path for the service.
--path / -p                     The path where the service should be created (e.g. --path my-service)
--name / -n                     Name for the service. Overwrites the default name of the created service.
--help / -h                     Show this message
--version / -v                  Show version info
--verbose                       Show verbose logs
--debug                         Namespace of debug logs to expose (use "*" to display all)

Project template names

Below template names are coming from above terminal output;

AWS

  • aws-clojure-gradle
  • aws-clojurescript-gradle
  • aws-nodejs
  • aws-nodejs-docker
  • aws-nodejs-typescript
  • aws-alexa-typescript
  • aws-nodejs-ecma-script
  • aws-python
  • aws-python3
  • aws-python-docker
  • aws-groovy-gradle
  • aws-java-maven
  • aws-java-gradle
  • aws-kotlin-jvm-maven
  • aws-kotlin-jvm-gradle
  • aws-kotlin-jvm-gradle-kts
  • aws-kotlin-nodejs-gradle
  • aws-scala-sbt
  • aws-csharp
  • aws-fsharp
  • aws-go
  • aws-go-dep
  • aws-go-mod
  • aws-ruby
  • aws-provided

Tencent Cloud

  • tencent-go
  • tencent-nodejs
  • tencent-python
  • tencent-php

Azure

  • azure-csharp
  • azure-nodejs
  • azure-nodejs-typescript
  • azure-python

Cloudflare

  • cloudflare-workers
  • cloudflare-workers-enterprise
  • cloudflare-workers-rust

Fn

  • fn-nodejs
  • fn-go

Google Cloud

  • google-nodejs
  • google-nodejs-typescript
  • google-python
  • google-go

Kubeless

  • kubeless-python
  • kubeless-nodejs

Knative

  • knative-docker

OpenWhisk

  • openwhisk-java-maven
  • openwhisk-nodejs
  • openwhisk-php
  • openwhisk-python
  • openwhisk-ruby
  • openwhisk-swift

Spotinst

  • spotinst-nodejs
  • spotinst-python
  • spotinst-ruby
  • spotinst-java8

Twilio

  • twilio-nodejs

Aliyun

  • aliyun-nodejs

Miscellaneous

  • plugin
  • hello-world

Creating a Serverless Python Project for AWS, Azure and Google

$ serverless create --template aws-python --path aws-python-demo1
$ serverless create --template azure-python --path azure-python-demo1
$ serverless create --template google-python --path google-python-demo1

Creating a Serverless Node.js Project for AWS, Azure and Google

$ serverless create --template aws-nodejs-typescript --path aws-nodejs-demo1
$ serverless create --template azure-nodejs-typescript --path azure-nodejs-demo1
$ serverless create --template google-nodejs-typescript --path google-nodejs-demo1

Creating a Serverless C# Project for AWS, Azure and Google

$ serverless create --template aws-csharp --path aws-csharp-demo1
$ serverless create --template azure-csharp --path azure-csharp-demo1

Creating a Serverless Java Project for AWS, Azure and Google

$ serverless create --template aws-java-maven --path aws-java-demo1

AWS Python Lambda Project Folder Structure

aws-python-demo1
├── handler.py
└── serverless.yml

0 directories, 2 files

I just removed comments in serverless.yml file, I added london region line(region: eu-west-2) and events: section for API Gateway, so it looks like as below;

service: aws-python-demo1

frameworkVersion: '3'

provider:
  name: aws
  runtime: python3.10
  region: eu-west-2

functions:
  hello:
    handler: handler.hello
    events:
      - httpApi:
          method: POST
          path: /hello

Deploying Service

After deployment, Serverless Framework will provide you with the URL of the deployed API Gateway endpoint

If --region parameter is not specified in command as below, then Serverless CLI will check region field in serverless.yml file above. But, if we want to deploy in different region, we can just set region in command.

$ cd aws-python-demo1
$ serverless deploy --region eu-west-2

List Deployments

$ sls deploy list
$ sls deploy list functions

Print Serverless.yml file

$ sls print

Print CloudFormation Output variables

$ sls info

service: aws-serverless-python-demo1
stage: dev
region: eu-west-2
stack: aws-serverless-python-demo1-dev
endpoint: POST - https://xj1e535jbd.execute-api.eu-west-2.amazonaws.com/hello
functions:
  hello: aws-serverless-python-demo1-dev-hello
$ aws cloudformation describe-stacks --stack-name aws-serverless-python-demo1-dev --query "Stacks[0].Outputs"

Test your function with Serverless Framework CLI

Deployed function name is specified in line 10 in serverless.yml configuration file above, it is used in Serverless framework CLI, so i used function name as hello in below serverless cli command;

$ serverless invoke -f hello

{
    "statusCode": 200,
    "body": "{\"message\": \"Go Serverless v1.0! Your function executed successfully!\", \"input\": {}}"
}

Test your function with AWS CLI

$ aws lambda invoke \
  --function-name aws-python-demo1-dev-hello \
  --region eu-west-2 \
  --payload $(echo '{"name": "kenan"}' | base64) \
  /dev/stdout

{"statusCode": 200, "body": "{\"message\": \"Go Serverless v1.0! Your function executed successfully!\", \{input\": {\"name\": \"kenan\"}}"}
    "StatusCode": 200,
    "ExecutedVersion": "$LATEST"
}
$ aws lambda invoke \
  --function-name aws-python-demo1-dev-hello \
  --region eu-west-2 \
  --payload $(echo '{"name": "kenan"}' | base64) \
  response.json

{
    "StatusCode": 200,
    "ExecutedVersion": "$LATEST"
}
$ aws lambda invoke \
  --function-name arn:aws:lambda:eu-west-2:111111111111:function:aws-python-demo1-dev-hello \
  --region eu-west-2 \
  --payload $(echo '{"name": "kenan"}' | base64) \
  /dev/stdout

{"statusCode": 200, "body": "{\"message\": \"Go Serverless v1.0! Your function executed successfully!\", \{input\": {\"name\": \"kenan\"}}"}
    "StatusCode": 200,
    "ExecutedVersion": "$LATEST"
}

Test your function with curl

$ curl --request POST https://xj1e535jbd.execute-api.eu-west-2.amazonaws.com/hello --data '{"name":"Azure Rocks"}'

{"message": "Go Serverless v1.0! Your function executed successfully!", "input": {"version": "2.0", "routeKey": "POST /hello", "rawPath": "/hello", "rawQueryString": ...........

Get Summary of Deployed Function App

$ sls info
$ sls info --verbose
$ sls info --stage prod --region eu-west-2

List AWS CloudFormation Stacks with AWS CLI

$ aws cloudformation list-stacks --region eu-west-2

Cleanup

$ sls remove