A demo of an AWS Lambda service that performs CRUD on a DynamoDB table. Using betty for build/deploying.
This demo is based on a microservice skeleton we've built for Stampr.
Video of getting started inaction:
- Create an AWS CLI profile described here
- Open terminal and run
export AWS_PROFILE=__YOUR_PROFILE_NAME_CREATED_ABOVE__; export AWS_REGION=__WHATEVER_REGION__
npm i
node ./provision/create-table.js
Finally, run ./betty deploy
. This is a shortcut to running ./betty build && ./betty update
.
Betty will create the AWS Lambda function along with the necessary IAM policies/permissions, and upload the function with the latest build.
To test the function, open the AWS console and go to Lambda and invoke using the test button.
You can run your lambda function locally through lambda emulator bundled with betty, but that's not described here.
Running ./betty logs
will stream the CloudWatch logs for the lambda function to the console using bunyan and if your microservice also uses bunyan for logging, it will work how you expect.
If you get errors running in AWS Lambda that you can't troubleshoot, try running the function locally node ./dist/index.js
.
These problems are often related to missing dependencies or other errors that only surface after build.
Crud events that can be used in the AWS console as lambda test events.
{
"method": "create",
"body": {
"email": "[email protected]",
"name": "Foo Bar",
"age": 21
}
}
{
"method": "read",
"body": {
"email": "[email protected]"
}
}
{
"method": "update",
"body": {
"email": "[email protected]",
"age": 22
}
}
NOTE Delete will not work as-is because the permission has not been granted to the microservice. To allow delete, you must add the dynamodb:DeleteItem
permission in resource.js
.
{
"method": "delete",
"body": {
"email": "[email protected]"
}
}