serverless
Servless Framework
Setup
AWS - Credentials
- Create an IAM Group
Supprimer les accees admin de ce groupe :
- Create an IAM User, then setup
- Add a username
- Access Type : Programmatic access
- Ajouter le user dans le groupe
- Créer le user
- Copier le keyId et le secretKeyId pour configurer l’environnement locale de developpement
1 | serverless config credentials --provider aws --key AKIAIOSFODNN7EXAMPLE --secret wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY |
Le configuation se retrouve dans le fichier ~/.aws/credentials
Install Servless Framework
1 | npm install -g serverless |
Create a new Project
Simple AWS Lambda
Create a new Serverless Service/Project
1
serverless create --template aws-nodejs --path my-service
Le fichier serverless.yml est crée : Contient la configuration du projet
- Ajuster le region aws dans le fichier
- Un fichier handlerjs est egalement présent: Contient un exemple de lambda
Il est alors possible de deployer le projet (voir section deploy)
Add Api Gateay to access to lambda
Dans le fichier serverless.yml1
2
3
4
5
6
7
8...
functions:
listUsers:
handler: users/handlers/list.handler
events:
- http:
path: users
method: get
Lors du deploy, le endpoint est affiché1
2
3...
endpoints:
GET - https://ta9skrzmkl.execute-api.us-east-1.amazonaws.com/dev/users
Manage
deploy
To deploy the service for the first time:
sls deploy
- Un Bucket S3 est alors crée contenant les sources du projet
- La/Les fonctions déployées sont visibles dans la console, section lambda
To deploy an updated function:1
sls deploy -f functionName
Invoke
deployed function
sls invoke -f createArticle -p articles/myEvent.json
sls invoke -f readArticle -p articles/events/read.event.json
Local function
sls invoke local -f readArticle -p articles/events/read.event.json
Logs
sls logs -f readArticle
Ref
- https://github.com/markusklems/programming-aws-lambda
- https://github.com/bishbashbosh123/lambda-example
- Video LEARNING PATH: AWS: AWS ENVIRONMENTS WITH AWS LAMBDA
Generate Event when file is uploaded in S3 Bucket
- Open the bucket in the AWS console, select Properties, click Events, and click Add Notification.
- Give your event a name, such as File Upload, and then under Events select ObjectCreate (All).
- Select Lambda Function from the Send To drop-down.
Use env variable
process.env.VARIABL_ENAME
Authentification && Authorization
Ref
- http://docs.aws.amazon.com/apigateway/latest/developerguide/use-custom-authorizer.html
- https://yos.io/2017/09/03/serverless-authentication-with-jwt/
Working with dynamoDb
Configure new table
in serverless.yml, in resources section
1 | resources: |
Si la table n’existe pas, il est nécessaire de faire un deploy pour que la table soit créée automatiquement
Works locally
- npm install –save serverless-dynamodb-local
plugins:
serverless-dynamodb-local
Install
1
sls dynamodb install
Start
1
sls dynamodb start --migrate
DynamoDB JavaScript Shell
1 | var params = { |
Configure
Ref
- https://serverless.com/blog/node-rest-api-with-serverless-lambda-and-dynamodb/
- [serverless-dynamodb-local|https://www.npmjs.com/package/serverless-dynamodb-local]
- https://www.gellock.com/2015/11/25/building-a-user-management-microservice-using-aws-part-2/
- https://www.gellock.com/2015/12/09/building-a-user-management-microservice-using-aws-part-4/
Variable d environnement
https://serverless.com/framework/docs/providers/aws/guide/variables/
https://serverless.com/framework/docs/providers/aws/guide/variables/#referencing-s3-objects
https://medium.com/@purplecones/serverless-environment-variables-4ec818f67388
https://github.com/serverless/serverless/issues/493
https://github.com/serverless/examples/issues/25
https://www.npmjs.com/package/serverless-env-generator