In this walk-through, we will:

Getting Started

To get started, you’ll need the Serverless Framework installed. You’ll also need your environment configured with AWS credentials.

Install as a standalone binary Serverless Framework (Windows)

Install with Chocolatey:

choco install serverless

For other operating systems , please visit the link.

Create a new directory

Let’s start by deploying a single endpoint.

First, create a new directory with a package.json file

mkdir hello-world && cd hello-world
npm init -f

Then, install a few dependencies. We’re going to use the serverless-wsgi plugin for negotiating the API Gateway event type into the WSGI format. We’ll also use the serverless-python-requirements plugin for handling our Python packages on deployment.

npm install --save-dev serverless-wsgi serverless-python-requirements

Create an IAM User and Access Key

The Serverless Framework (Windows) needs access to AWS account so that it can create and manage resources. Follow these steps to create an IAM user for the Serverless Framework (Windows):

  1. Login to your AWS account and go to the Identity & Access Management (IAM) page.
  2. Click on Users and then Add user. Enter a name in the first field to remind you this User is related to the Service you are deploying with the Serverless Framework (Windows), like serverless-servicename-agent. Enable Programmatic access by clicking the checkbox. Click Next to go through to the Permissions page. Click on Create policy. Select the JSON tab, and add a JSON file.
{
    "Statement": [
        {
            "Action": [
                "cloudformation:CancelUpdateStack",
                "cloudformation:ContinueUpdateRollback",
                "cloudformation:CreateChangeSet",
                "cloudformation:CreateStack",
                "cloudformation:CreateUploadBucket",
                "cloudformation:DeleteStack",
                "cloudformation:Describe*",
                "cloudformation:EstimateTemplateCost",
                "cloudformation:ExecuteChangeSet",
                "cloudformation:Get*",
                "cloudformation:List*",
                "cloudformation:UpdateStack",
                "cloudformation:UpdateTerminationProtection",
                "cloudformation:ValidateTemplate",
                "iam:AttachRolePolicy",
                "iam:CreateRole",
                "iam:DeleteRole",
                "iam:DeleteRolePolicy",
                "iam:DetachRolePolicy",
                "iam:GetRole",
                "iam:PassRole",
                "iam:PutRolePolicy",
                "lambda:*",
                "logs:CreateLogGroup",
                "logs:DeleteLogGroup",
                "logs:DescribeLogGroups",
                "logs:DescribeLogStreams",
                "logs:FilterLogEvents",
                "logs:GetLogEvents",
                "logs:PutSubscriptionFilter",
                "s3:CreateBucket",
                "s3:DeleteBucket",
                "s3:DeleteBucketPolicy",
                "s3:DeleteObject",
                "s3:DeleteObjectVersion",
                "s3:GetObject",
                "s3:GetObjectVersion",
                "s3:ListAllMyBuckets",
                "s3:ListBucket",
                "s3:PutBucketNotification",
                "s3:PutBucketPolicy",
                "s3:PutBucketTagging",
                "s3:PutBucketWebsite",
                "s3:PutEncryptionConfiguration",
                "s3:PutObject",
                "apigateway:*"
            ],
            "Effect": "Allow",
            "Resource": "*"
        }
    ],
    "Version": "2012-10-17"
}

Check to make sure everything looks good and click Create user. View and copy the API Key & Secret to a temporary place. You can export them as environment variables, so they would be accessible to Serverless and the AWS SDK in your shell:

set AWS_ACCESS_KEY_ID=<your-key-here>
set AWS_SECRET_ACCESS_KEY=<your-secret-key-here>
# AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY are now available for serverless to use

# 'set' command is valid only for Windows. In Unix - use 'export' instead of 'set'

Deploy Hello World using Serverless Framework (Windows)

AWS – Create

Creates a new service in the current working directory based on the provided template.

serverless create --template aws-python3 --name hello-service
handler.py
import json


def hello(event, context):
    body = {
        "message": "Go Serverless v1.0! Your hello-world function executed successfully!",
        "input": event
    }

    response = {
        "statusCode": 200,
        "body": json.dumps(body)
    }

    return response
serverless.yml
# Welcome to Serverless!
#
# This file is the main config file for your service.

service: hello-service # NOTE: update this with your service name
provider:
  name: aws
  runtime: python3.8
  stage: dev
  region: us-east-1

# you can add packaging information here
package:
#  include:
#    - include-me.py
#    - include-me-dir/**
  exclude:
    - .vscode/**

functions:
  hello-function:
    handler: handler.hello
    memorySize: 128
    timeout: 30

#    The following are a few example events you can configure
#    NOTE: Please make sure to change your handler code to work with those events
#    Check the event documentation for details
    events:
      - http:
          path: hello-path
          method: get

Deploy the app
serverless deploy

Delete Endpoint

serverless remove