This example demonstrates how to run a service locally, using the serverless-offline plugin. It provides an HTTP API to manage Todos stored in a DynamoDB, similar to the aws-node-http-api-dynamodb example. A local DynamoDB instance is provided with a Docker image by the AWS team.
- Nodejs
- aws-cli
- Docker (required only for offline dynamodb instance)
- jq (required only for import/export scripts)
# Install dependencies in root directory
npm install
# Open a new terminal to run the dynamo offline image
cd ./docker && docker-compose up
# Create a new dynamo table with schema in root directory
npm run create-table-local
# Start up local dev server in root directory
npm run dev
# Create .env file and fill in the aws key / secret
cp ./example.env .env
# Deploy resources
npm run deploy
You can create, retrieve, update, or delete todos with the following commands. Note that the URL Can be replaced with your live resources after they have been deployed.
# Local
curl -X POST -H "Content-Type:application/json" http://localhost:3001/todos --data '{ "text": "Learn Serverless" }'
Example Result:
{"text":"Learn Serverless","id":"cna490d011e6deiwz051af86","createdAt":1479138570824,"checked":false,"updatedAt":1479138570824}%
curl -H "Content-Type:application/json" http://localhost:3001/todos
curl -H "Content-Type:application/json" https://xyxyxyxyxy.execute-api.us-east-2.amazonaws.com/todos
Example output:
[{"text":"Deploy my first service","id":"clh105j349i1nzuqm298vt11","checked":true,"updatedAt":1479139961304},{"text":"Learn Serverless","id":"cna490d011e6deiwz051af86","createdAt":1479139943241,"checked":false,"updatedAt":1479139943241}]
# Replace the <id> part with a real id from your todos table
curl -H "Content-Type:application/json" http://localhost:3001/todos/<id>
Example Result:
{"text":"Learn Serverless","id":"cna490d011e6deiwz051af86","createdAt":1479138570824,"checked":false,"updatedAt":1479138570824}%
# Replace the <id> part with a real id from your todos table
curl -X PUT -H "Content-Type:application/json" http://localhost:3001/todos/<id> --data '{ "text": "Learn Serverless", "checked": true }'
Example Result:
{"text":"Learn Serverless","id":"cna490d011e6deiwz051af86","createdAt":1479138570824,"checked":true,"updatedAt":1479138570824}%
# Replace the <id> part with a real id from your todos table
curl -X DELETE -H "Content-Type:application/json" http://localhost:3001/todos/<id>
- add gsi1pk & gsi1sk with
TODO#${id}
&TODO#${id}
- PK: USER#joematune
- SK: TODO#cuidq1w2e3
- GSI1PK: TODO
- GSI1SK: TODO#2022-05-07T12:12:12.876Z
- modify resource so that it can also be the migration yaml
- tidy helper to / from functions
- remove unnecessary node_modules
- tidy webpack config
- add tsuid
- add aws-lambda-mock test
- remove IS_LOCAL from env because you're setting it in
npm run dev