https://www.serverless.com/blog/cicd-for-serverless-part-1/ https://www.serverless.com/blog/cicd-for-serverless-part-2/
Getting started with Lambda isn't hard at all. But if you are shooting for a super simple API you are still left with a lot of boilerplate things and missing pieces to make it really useful. Each which have their own small gotchas. Testing, deployments, API Gateway, cors, staged deployments, database setup, roles, permissions, javascript modules, dependency install, etc. It can still be a decent amount of work to get to something straightforward: A datasource accessible through lambda driven by a restful API written with Javascript with no hoops for pulling in additional js dependencies. Serverless helps you get there without worrying about all that boilerplate.
- Follow Serverless setup guide for 3 things:
- Install Node
- Install Serverless
- Setup AWS - mostly setting up credentials
- Fork the project.
- Look through the project:
- Understand what is being created in AWS
- Look at
serverless.ymlfor the definitions of things going into AWS. - Make sure to check
service,region, andstageon the provider entry inserverless.ymlto your desired otherwise you'll deploy tous-west-2and the stage will be nameddevThe first time you create all this in AWS you probably should go look through everything that got created. It is all happening with CloudFormation and it is doing a lot of nice things for you and all the names and such are coming from the serverless.yml file and are under your control.
- Look at
- Understand the Lambda
- Look at
exampleFile.jsfor the lambda code - this just prints out the path variable that was passed in from the URI.
- Look at
- Understand which names are what
- The file name of the file with the lambda code in it (
exampleFile.js) - the defined functionName in the serverless.yml file (
exampleFunction) - this will be the name you see in AWS for the lambda. - the name of the exported method on the lambda. (
exampleFile.exampleWithPathParameters) This just allows the serverless.yml fill to understand where to start executing code for the lambda.
- The file name of the file with the lambda code in it (
- Understand the API and the API mocking:
- We use local data file to mock the data we would be getting from an http call...things like the path, the path parameters, query parameters, thing like that. Look at
test/shaefer/get-local.jsonThis file simulates the callhttps://xxxxxxxxxx.execute-api.{region}.amazonaws.com/{stage}/shaefer/{id}for examplehttps://xxxxxxxxxx.execute-api.us-east-1.amazonaws.com/dev/shaefer/15if you don't change any of the default project settings.
- We use local data file to mock the data we would be getting from an http call...things like the path, the path parameters, query parameters, thing like that. Look at
- Understand what is being created in AWS
- Run it locally
serverless invoke local --function exampleFunction --path test/shaefer/get-local.jsonThis is like hitting your lambda live with the earlier mentioned URI. - Run
serverless deployto create the lambda and dynamodb table in AWS. The output for the deploy will show success and give you the url for the deployed API gateway endpoint that you can hit to test it live. - OPTIONAL BUT IMPORTANT: If you forgot something and want to rollback...BEFORE you change anything in the serverless.yml just run a
serverless removeand it will delete all the resources it just created...or try to. Since serverless created everything with Cloudformation it can remove it too. Find more details in the serverless docs.