documents/dev/serverless.md
Table of Contents
AWS
Serverless Usage
sls deploy
curl -v -F key1=value1 -F file=@/Users/dominick/Work/scriptsync/_notes.md https://liwhmuu2og.execute-api.us-east-1.amazonaws.com/upload
sls logs -f upload sls dev
The Serverless Framework translates all syntax in serverless.yml to a single AWS CloudFormation template. https://www.serverless.com/framework/docs/providers/aws/guide/serverless.yml
https://github.com/serverless/examples/blob/v4/aws-node-http-api-dynamodb/serverless.yml
# create a fully set up Serverless Framework Service
serverless
# retrieve info/URLs to function endpoints
sls info
# -F/--form <name=content> Specify HTTP multipart POST data (H)
curl -v -X POST -H "Content-Type: multipart/form-data" -F file=@index.mjs https://liwhmuu2og.execute-api.us-east-1.amazonaws.com/upload
# https://www.serverless.com/framework/docs/providers/aws/cli-reference/invoke
# if function doesn't have API endpoint:
sls invoke -f functionName
# invoke and display logs
sls invoke -f functionName --log
# streaming logs
sls logs -f [Function name in serverless.yml] -t
# invoke locally in dev mode
sls invoke local --function functionName --data '{"a":"bar"}'
# to delete service & all AWS resources created by project
sls remove
Deployment
https://www.serverless.com/framework/docs/providers/aws/guide/deploying
In order to deploy the example, you need to run the following command:
serverless deploy
After running deploy, you should see output similar to:
Deploying "serverless-http-api" to stage "dev" (us-east-1)
✔ Service deployed to stack serverless-http-api-dev (91s)
endpoint: GET - https://xxxxxxxxxx.execute-api.us-east-1.amazonaws.com/
functions:
hello: serverless-http-api-dev-hello (1.6 kB)
Note: In current form, after deployment, your API is public and can be invoked by anyone. For production deployments, you might want to configure an authorizer. For details on how to do that, refer to HTTP API (API Gateway V2) event docs.
Invocation
After successful deployment, you can call the created application via HTTP:
curl https://xxxxxxx.execute-api.us-east-1.amazonaws.com/
Which should result in response similar to:
{ "message": "Go Serverless v4! Your function executed successfully!" }
Local development
The easiest way to develop and test your function is to use the dev command:
serverless dev
This will start a local emulator of AWS Lambda and tunnel your requests to and from AWS Lambda, allowing you to interact with your function as if it were running in the cloud.
Now you can invoke the function as before, but this time the function will be executed locally. Now you can develop your function locally, invoke it, and see the results immediately without having to re-deploy.
When you are done developing, don't forget to run serverless deploy to deploy the function to the cloud.