IMPORTANT: Make sure you are on the 'EU (Ireland)' AWS Region
The back end hosted as a Lambda function you built in Task 1
- In a separate browser tab go to the Amazon Dynamo DB Dashboard
- Click on 'Create table'
- In the 'table name' field type
ALX402FactDB
- For 'Partition key' use 'FactId'
- Leave the type set to 'String'
- Leave 'Use Default Settings' checked
- Click the 'Create' button at the bottom of the page. (This can take a couple of minutes.)
- On the right hand side click on the 'Indexes' tab
- Click on the 'Create Index' button
- Type
locale
into the 'Primary key' field- Leave the type set to 'String'
- The index name should have auto populated to 'locale-index'
- Don't change any of the other settings
- Click the 'Create Index' button
- This process will take about 5 minutes
- The 'Status' will change to 'Active' once it's done
- Click on the 'Overview' tab
- Copy the 'Amazon Resource Name (ARN)' for the table, paste it to a text file as we will use it later
- It is located at the bottom of the page
- Click on the 'Items' tab
- Click the 'Create item' button
- In the new form that appears, do the following:
- Type in
0001
for 'FactId' - Type in
en-US
for 'locale' - Click the '+' button and append a string.
- Type
speech
for the 'Field' andManually added fact
for the 'Value' - Click the '+' button and append an additional string.
- Type
text
for the 'Field' andText for manually added fact
for the 'Value'
- Type in
- Click 'Save' button
- You now have a properly formatted item stored in DynamoDB
- In a new tab, navigate to the AWS Identity Access Management Service
- Click on 'Roles' on the left hand side
- Click on the name 'ALX402-Role'
- Click 'Attach Policy' button
- Type
DynamoDBReadOnlyAccess
in the search bar - Click on the checkbox next to 'AmazonDynamoDBReadOnlyAccess'
- Click 'Attach Policy' button
- Go back to the Lambda service and select the function called 'ALX402' (We created it in Task 1)
- Replace the 'GetFact' function with the following code:
- The function should start on line
'GetFact': function () {
let requestLocale = this.event.request.locale; //get locale from request for our DB secondary index
let AWS = require('aws-sdk'); //instantiate the aws sdk
let docClient = new AWS.DynamoDB.DocumentClient(); // create a DynamoDB client
let params = { // Parameters object for a query that will return all facts for a locale
TableName : "ALX402FactDB",
IndexName: "locale-index",
KeyConditionExpression: "locale = :v1",
ExpressionAttributeValues: {
":v1": requestLocale
}
};
let dynamoQuery = docClient.query(params).promise();//create the query and wrap in a promise
dynamoQuery.then(fulfilled.bind(this), rejected.bind(this));//execute query asynchronously
function fulfilled(value) {//function for handling a fulfilled promise
console.log(value);
const factArr = value.Items;//array form DynamoDB
const factIndex = Math.floor(Math.random() * factArr.length); //random int bounded by length of array
const randomFact = factArr[factIndex]; //get the item that matches the random number
const speechOutput = randomFact.speech; //get the speech value form the item
const textOutput = randomFact.text; //get the text value form the item
this.emit(':tellWithCard', speechOutput, 'ALX 402', textOutput);//send a response to the alexa service
};
function rejected(reason) {//function for handling a rejected promise
console.log(reason);
this.emit(':tell', "An Error occurred, please check the console log");
};
},
- Make sure the 'StartSession' event you created is selected
- Click on 'Save and Test' on the top right
- Click the arrow next to 'Details' and review the object being sent back
- Review the content of the console log
- Remove all the hardcoded facts, but leave the other constants