Use serverless to build web server application
-
Duration: 15 minutes
-
Checkout lab1:
git checkout feature/lab1 -
Install NodeJS
- For *nix OS:
- Install NVM: follow the installation instruction on https://github.com/nvm-sh/nvm
- Install NodeJS 8.x via nvm:
- open terminal
- run
nvm install 8
- For window:
- https://github.com/coreybutler/nvm-windows
- Install NodeJS 8.x or higher via nvm:
nvm install 8
- For *nix OS:
-
Install MySQL
- on macos:
- run in terminal:
brew install mysql - run mysql as daemon:
brew services start mysql
- run in terminal:
- on ubuntu: refer to https://www.digitalocean.com/community/tutorials/how-to-install-mysql-on-ubuntu-16-04
- on window:
- download installer from https://dev.mysql.com/downloads/installer/
- follow the installation wizard.
- on macos:
-
Config aws environment.
- go to 'My Security Credential' page on aws: https://console.aws.amazon.com/iam/home?region=ap-southeast-1#security_credential
- create 'serverless-practice' IAM user
- generate access key
- to know the detail of setup configuration and credential aws account please refer to https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html
- add aws configuration to
~/.aws/configfile. see sample files./workshop/.aws/config - add aws account to
~/.aws/credentialfile. see sample file./workshop/.aws/credential
- Duration: 20 minutes
- Checkout lab2:
git checkout feature/lab2 - Requirement:
- grab the core concept: https://serverless.com/framework/docs/providers/aws/guide/intro/
- setup basic serverless app:
- write a simple function which return hello world
- integration serverless-webpack
- integrate serverless-offline
- test on local:
- refer to scripts section in package.json
- run:
npm run start-offline - open postman (similar tool), make a GET request to
http://localhost:3000/api/hello-world
- deploy onto aws
- make sure your have configured aws credential and configuration
- edit
.my-tools/devops/deploy-staging.shchange value ofAWS_PROFILEat line 'export AWS_PROFILE=tuanquynet' to your profile. - run:
sh .my-tools/devops/deploy-staging.sh
- test deployment:
- Go to API gateway page of aws console, grab the endpoint url.
- Make GET request to hello-world endpoint.
- Duration: 15 minutes
- Checkout lab3:
git checkout feature/lab3 - Requirement:
- Create a new lambda function named 'server-info' returning current server date time
- Create another function named 'my-func'
- Write code for my-func to invoke server-info lambda:
- install aws-sdk:
npm install --save aws-sdk - read docs:
- log the result of lambda invocation
- install aws-sdk:
- Duration: 10 minutes
- Duration: 30 minutes
- Checkout lab4:
git checkout feature/lab4 - Prepare database
- init account: read file .my-tools/devops/init-database.sh
- create database schema: run
sh .my-tools/devops/restore-database.sh
- create
loginendpoint. - create
authorizerfunction- this function is used to authenticate user. All protected endpoint should specify authorizer when configuring in serverless.yml
- refer to https://serverless.com/framework/docs/providers/aws/events/apigateway/
- refer to http://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies.html
- refer to https://dev.to/adnanrahic/a-crash-course-on-securing-serverless-apis-with-json-web-tokens-22fa
- create
getuser endpoint. - create
createuser endpoint - create
listinguser endpoint - create
updateuser endpoint - create
deleteuser endpoint
Notes: If you are using bcrypt on MacOS or Window it will not be working on linux when deploying on aws lambda because bcrypt is a binary module. To fix this issue we have to configure lambda layer, and the node modules in that layer should be generated on linux.
- Configure lambda layer.
- Duration: 30 minutes
- Checkout lab5:
git checkout feature/lab5 - Requirement:
- Write a lambda function to extract faces from images which are uploaded onto s3.
- We use AWS Rekognition to detect faces and base on the analysed results we know the bounding boxes and use sharp lib to copy the appropriate area on the image.
- Save detected faces into image and upload onto another bucket of s3.
- Read docs:
Notes: After deployed lambdas onto aws, you need to upload avatar.png file located in workshop/assets folder onto the serverless-practice.files.rnd.upload s3 bucket.