description |
---|
Loppuvastus |
We're going to build a simple CRUD or create, read, update, delete application using the serverless technologies that we have learned to use today. Here is a picture:
Our database uses id (String) as its primary key (partition key). We use HTTP verbs GET, PUT and DELETE to create, read, update and delete items in the database.
The instructions will be very short, as we have covered these steps in other labs.
Create a table called http-crud-tutorial-items.
Primary key (partition key) is id (String).
Create a Lambda function.
Name: http-crud-tutorial-function
Runtime: Node.js
Execution role: create new from templates. Use template simple microservice permissions. Name of role is http-crud-tutorial-role.
Code:
{% code title="index.js" %}
const AWS = require("aws-sdk");
const dynamo = new AWS.DynamoDB.DocumentClient();
exports.handler = async (event, context) => {
let body;
let statusCode = 200;
const headers = {
"Content-Type": "application/json"
};
try {
switch (event.routeKey) {
case "DELETE /items/{id}":
await dynamo
.delete({
TableName: "http-crud-tutorial-items",
Key: {
id: event.pathParameters.id
}
})
.promise();
body = `Deleted item ${event.pathParameters.id}`;
break;
case "GET /items/{id}":
body = await dynamo
.get({
TableName: "http-crud-tutorial-items",
Key: {
id: event.pathParameters.id
}
})
.promise();
break;
case "GET /items":
body = await dynamo.scan({ TableName: "http-crud-tutorial-items" }).promise();
break;
case "PUT /items":
let requestJSON = JSON.parse(event.body);
await dynamo
.put({
TableName: "http-crud-tutorial-items",
Item: {
id: requestJSON.id,
price: requestJSON.price,
name: requestJSON.name
}
})
.promise();
body = `Put item ${requestJSON.id}`;
break;
default:
throw new Error(`Unsupported route: "${event.routeKey}"`);
}
} catch (err) {
statusCode = 400;
body = err.message;
} finally {
body = JSON.stringify(body);
}
return {
statusCode,
body,
headers
};
};
{% endcode %}
Create an HTTP API and give it the name http-crud-tutorial-api.
Routes are a way to send incoming API requests to backend resources. Routes consist of two parts: an HTTP method and a resource path, for example, GET /items
. For this CRUD API, we create four routes:
GET /items/{id}
get item with a specific idGET /items
get all the itemsPUT /items
create or update an itemDELETE /items/{id}
delete an item
The steps are the same for all routes. For example GET /items/{id}:
- go to API gateway and the HTTP API we just created
- Click Routes > create
- For Method choose GET
- For path enter
/items/{id}
- Click create
Repeat the steps for these methods and paths:
GET /items
DELETE /items/{id}
PUT /items
You should end up with the following:
We're still in API gateway and the HTTP API.
- Choose Integrations
- Choose Manage integrations > create
- Skip "Attach this integration to a route"
- Integration type: Lambda
- Lambda function is the http-crud-tutorial-function
- Create.
We're still in API gateway and the HTTP API.
- Choose Integrations
- Choose Attach integrations to routes
- Choose a route (e.g. GET /items)
- Under choose an existing integration, choose the Lambda http-crud-tutorial-function
- Choose attach integration
Repeat steps for all routes. This is what it looks like when GET /items route has an integration:
Everything should be working now! You will find the invoke URL of your API from API Gateway. We can test it using cURL (any tool of your choice). If you want more information from cURL, use curl -v.
The new items need to have
- an id (which is sent as a String, therefore it needs quotes around it)
- a price
- a name.
curl -X "PUT" -H "Content-Type: application/json" -d "{"id": "1", "key1": "value1", "foo": "bar"}" https://invoke-url/items
curl -X "PUT" -H "Content-Type: application/json" -d "{"id": "1", "stuff": "here", "replace": "me"}" https://invoke-url/items
curl https://invoke-url/items
curl -X "DELETE" https://abcdef123.execute-api.us-west-2.amazonaws.com/items/abcdef234
Go ahead and play around: create an item, update it, get it, delete it, get all items to make sure it's been deleted etc.
Once you are done admiring your awesome creation, go ahead and delete
- The API
- the Lambda function
- the DynamoDB table.