/serverless-template

Boilerplate template for the serverless-framework

Primary LanguagePythonMIT LicenseMIT

serverless-template

Serverless-Template

GitHub version Build Status License: MIT serverless

Boilerplate template for the serverless-framework.

Want to learn how to get start with the serverless framework? Watch my YouTube video where I explain about how to use the framework, while using this GitHub repository.

Requirements

  1. AWS account

  2. Docker

Getting Started

Goal

Deploy two serverless services (APIs)

  • todo-api - NodeJS 12.x (JavaScript/TypeScript)
    • CRUD app - To keep it simple, we're using an S3 bucket as a database. The contents are saved to the objects' user-defined metadata
  • greet-api - Python 3.8
    • Send a name and get a greeting

Both services have dependencies, and we'll use Lambda Layers to meet these dependencies.

  1. Clone this repository (or Use as template and then clone)

    $ (home) git clone https://github.com/unfor19/serverless-template.git
  2. Use this Docker image

    Image Usage Examples
    • With aws-vault

      $ (serverless-template) aws-vault exec PROFILE_NAME -- bash ./scripts/docker_run.sh
    • AWS Environment variables

      $ (serverless-template) export AWS_SECRET_ACCESS_KEY=A123123
      $ (serverless-template) export AWS_ACCESS_KEY_ID=B1232123123
      $ (serverless-template) export AWS_REGION=eu-west-1
      $ (serverless-template) export AWS_PROFILE=my-profile-name
      $ (serverless-template) bash ./scripts/docker_run.sh
    • AWS Credentials & Config files

      $ (serverless-template) bash ./scripts/docker_run.sh

    Tip: Take a look at the docker_run script

    $ (serverless-template) bash ./scripts/docker_run.sh
    ...                     # Pulling image ...
    $ /code (master)        # We're in!
  3. Build App - this includes installing dependencies

    $ /code (master) bash ./scripts/app_build.sh
    🔎  Identifying services folders ...
    ...
    ✅  Finished
  4. Deploy AWS resources - S3 Bucket and API Gateway

    $ /code/aws-resources (master) yarn deploy:dev
  5. Deploy AWS Lambda Layers

    $ /code/services/todo-api/layer (master)   yarn deploy:dev
    $ /code/services/greet-api/layer (master)  yarn deploy:dev
  6. Deploy AWS Lambda Functions

    $ /code/services/todo-api (master)  yarn deploy:dev
    $ /code/services/greet-api (master) yarn deploy:dev

Usage

Replace ENDPOINT with the API Gateway's endpoint that was generated by serverless-framework, and AWS_REGION with the relevant region.

$ /code (master) APIGATEWAY_ENDPOINT=https://ENDPOINT.execute-api.AWS_REGION.amazonaws.com

Create

Replace MY_CONTENT

MY_CONTENT="some content"

curl --location --request POST ${APIGATEWAY_ENDPOINT}/dev/todo/create \
--header 'Content-Type: application/json' \
--data-raw '{ "content": "'"${MY_CONTENT}"'" }'

Get (Read)

Replace MY_UUID

MY_UUID='cf27a7de-f3f7-43a0-b12f-ff8016b7b7e0'

curl --location --request GET ${APIGATEWAY_ENDPOINT}/dev/todo/get/${MY_UUID}

Update

Replace MY_UUID and MY_CONTENT

MY_UUID='cf27a7de-f3f7-43a0-b12f-ff8016b7b7e0'
MY_CONTENT='wohoo new content!'

curl --location --request POST ${APIGATEWAY_ENDPOINT}/dev/todo/update \
--header 'Content-Type: application/json' \
--data-raw '  { "id": "'${MY_UUID}'", "content": "'"${MY_CONTENT}"'" }'

Delete

Replace MY_UUID

MY_UUID='cf27a7de-f3f7-43a0-b12f-ff8016b7b7e0'

curl --location --request DELETE ${APIGATEWAY_ENDPOINT}/dev/todo/delete/${MY_UUID}

List

curl --location --request GET ${APIGATEWAY_ENDPOINT}/dev/todo/list

Greet

Replace MY_NAME

MY_NAME="Willy"

curl --location --request GET "${APIGATEWAY_ENDPOINT}/dev/greet/${MY_NAME}"

Modify

Expand/Collapse

Requirements

  1. Clone this repository

  2. Use this Docker image

  3. Install dependencies for each API

    $ /code/services/todo-api (master)  yarn install
    $ /code/services/greet-api (master) yarn install
  4. Modify code in src and then build

    $ /code/services/todo-api (master)  yarn build:dev
    $ /code/services/greet-api (master) yarn build:dev

Manage Dependencies

Adding a new dependency

NodeJS - yarn add package_name

$ /code/services/todo-api/layer/nodejs (master) yarn add uuid # or any other package

Python - update the requirements.txt file

$ /code (master) cat ./services/greet-api/layer/python/requirements.txt
greetings==0.1.0

Deploying a new Layer version

$ /code/services/todo-api/layer (master)  yarn deploy:dev
$ /code/services/greet-api/layer (master) yarn deploy:dev

Deploying and Redeploying the API

When updating a Lambda Layer, you must re-deploy the API for it to use the latest Lambda Layer version.

$ /code/services/todo-api (master)  yarn deploy:dev
$ /code/services/greet-api (master) yarn deploy:dev

Cleanup

  1. Destroy AWS Lambda Functions

    $ /code/services/todo-api (master)  yarn destroy:dev
    $ /code/services/greet-api (master) yarn destroy:dev
  2. Destroy AWS Lambda Layers

    $ /code/services/todo-api/layer (master)  yarn destroy:dev
    $ /code/services/greet-api/layer (master) yarn destroy:dev
  3. Destroy S3 Bucket and API Gateway

    IMPORTANT - remove all the objects from the todo S3 bucket before taking this action

    $ /code/aws-resources (master) yarn destroy:dev

Theory

Expand/Collapse

Concepts

Learn how to use the Serverless Framework, while taking advantage of AWS Lambda Function, Lambda Layer, and API Gateway.

AWS Lambda Function

"AWS Lambda lets you run code without provisioning or managing servers. You pay only for the compute time you consume..." [Source]


AWS Lambda Layer

"...A layer is a ZIP archive that contains libraries, a custom runtime, or other dependencies. With layers, you can use libraries in your function without needing to include them in your deployment package..." [Source]


AWS API Gateway

"...API Gateway handles all the tasks involved in accepting and processing up to hundreds of thousands of concurrent API calls, including traffic management, CORS support, authorization, and access control, throttling, monitoring, and API version management..." [Source]


Serverless Framework

"The Serverless Framework helps you build serverless apps with radically less overhead and cost. It provides a powerful, unified experience to develop, deploy, test, secure and monitors your serverless applications..." [Source]

Eventually, the serverless framework produces CloudFormation templates, deploys stacks, and manages them.

Tip After deploying with the serverless framework, check the stacks' templates, they look like a total mess. If you want to 'prettify' those YAML templates, click on View in Designer > Move one of the components, and then look below, your template was automatically 'prettified'

Use Cases

Serverless backend and Cron jobs

Serverless backend and Cron jobs

[Source]

Data processing

Data processing

[Source]

Lambda@Edge Increase web application security

Before

After

APIs

Project Tree

ProjectTree

  • Each API is an isolated service that contains multiple functions
  • All APIs share the same API Gateway - easier to manage
  • The file serverless.common.yml contains mappings that are relevant to all APIs, such as region, allow_origin, user_pool_id, and more

APIs Structure

  • serverless.yml - configuration for deployment - Using Layers
  • layer - deployed separately, these are the dependencies
  • src - source code of API that is deployed by serverless
  • package.json - contains the build, deploy and destroy scripts, and dev-dependencies
  • yarn.lock - contains the list of dev-dependencies and their versions

Layers

  • Never run yarn add some_package in an API folder
  • Always use yarn add --dev some_packagein an API folder; Lambda Layer supplies the "real" dependencies
  • There's no need to create a layer for AWS SDK (e.g., aws-sdk, boto3) - These libraries are provided by AWS automatically

Layer Structure

  • serverless.yml - configuration for deploying the layer - Deploying Layers
  • package.json - contains the scripts for building, deploying and destroying the layer
  • nodejs/package.json - contains the dependencies that are uploaded with this layer
  • nodejs/yarn.lock - contains the list of dependencies and their versions

Also Check

Contributing

Report issues/questions/feature requests on in the Issues section.

Pull requests are welcome! Ideally, create a feature branch and issue for every single change you make. These are the steps:

  1. Fork this repo
  2. Create your feature branch from master (git checkout -b my-new-feature)
  3. Commit your remarkable changes (git commit -am 'Added some feature')
  4. Push to the branch (git push --set-up-stream origin my-new-feature)
  5. Create a new Pull Request and tell us about your changes

Connect

Authors

Created and maintained by Meir Gabay

License

This project is licensed under the MIT License - see the LICENSE file for details