Now that we have added the datastore, let’s add some data.
NOTE: This code is for demonstration purposes only. The code below does not follow DynamoDB best practices for schema design / data manipulation.
Let’s add a few packages. Run the following command in the terminal:
cd ~/environment/api/hello-world
npm install uuid
In the Cloud9 file navigation pane, open the api/hello-world/app.js file.
Add the following line to the top of the page to add the AWS SDK and initialise the dynamodb object:
const AWS = require('aws-sdk');
const ddb = new AWS.DynamoDB();
const uuid = require('uuid');
const tableName = process.env.TABLE_NAME;
Now let’s add the following lines to add call to put a row into the table. Add this inside the try block, at the top.
const put_params = {
TableName: tableName,
Item: {
'id': {S: uuid.v1() },
'description' : {S: 'added'},
'timestamp' : {S: 'now'}
}
};
await ddb.putItem(put_params).promise();
Now let’s get all the items to return back. Add this section right below the above code:
const get_params = {
TableName: tableName,
Select: "ALL_ATTRIBUTES"
};
const list_response = await ddb.scan(get_params).promise();
Now, let’s add the count to the response, right next to the message:
count: list_response.Items.length,
Your final Lambda function will look like this:
const AWS = require('aws-sdk');
const ddb = new AWS.DynamoDB();
const uuid = require('uuid');
const tableName = process.env.TABLE_NAME;
// const axios = require('axios')
// const url = 'http://checkip.amazonaws.com/';
let response;
/**
*
* Event doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-input-format
* @param {Object} event - API Gateway Lambda Proxy Input Format
*
* Context doc: https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-context.html
* @param {Object} context
*
* Return doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html
* @returns {Object} object - API Gateway Lambda Proxy Output Format
*
*/
exports.lambdaHandler = async (event, context) => {
try {
const put_params = {
TableName: tableName,
Item: {
'id': {S: uuid.v1() },
'description' : {S: 'added'},
'timestamp' : {S: 'now'}
}
};
await ddb.putItem(put_params).promise();
const get_params = {
TableName: tableName,
Select: "ALL_ATTRIBUTES"
};
const list_response = await ddb.scan(get_params).promise();
// const ret = await axios(url);
response = {
'statusCode': 200,
'body': JSON.stringify({
message: 'hello serverless friends',
count: list_response.Items.length,
// location: ret.data.trim()
})
}
} catch (err) {
console.log(err);
return err;
}
return response
};
Finally, let’s deploy the solution.
cd ~/environment/api
sam build
sam deploy
Click the link in the output section and select Open from the dropdown menu. This will open a browser with the following body: {“message”:“hello serverless friends”,“count”:1}.
Refresh the page. You will see that the count will be going higher every time you hit the endpoint.
In this section, you have learnt how to: