ITM 300 - Cloud Foundations

Module 05: Product 5 Lab - DynamoDB

list

Quick Oil Change and Repair Photo by Dall-E-3

Product Objective

You will be creating a dynamodb table that will store service requests. We will also update our lambda function to pull from the database.

Instructions

We will do two main steps in this lab. First we will create a dynamodb table. This is where we will store future service requests and information about vehicles.

Second, we'll update the lambda function to read from the dynamodb table to pull in the information.

Create a DynamoDB table

Case Matters

When you create the table and all of the fields, please be careful to follow the uppercase and lowercase names as provided in the instructions. The app expects specific names of fields and the database to work.

ALSO, make sure there are no spaces before or after in your field names or values

search Search for DynamoDB in AWS

Wait for the Status to be: check_circle Active

Update Lambda

search Search for Lambda and open your getServiceRequest lambda function

import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
import {
  DynamoDBDocumentClient,
  ScanCommand,
  PutCommand,
  GetCommand,
  DeleteCommand,
} from "@aws-sdk/lib-dynamodb";

    const client = new DynamoDBClient({});

    const mydynamodb = DynamoDBDocumentClient.from(client);

    const tableName = "VehicleServices";    
export const getDynamoServiceRequests = async () => {
    const statusToExclude = "Completed";
    const statusToExcludeNew = "New Request";
    try {
        const params = {
            TableName: tableName,
            FilterExpression: "attribute_not_exists(service_status) OR (#service_status <> :status AND #service_status <> :statusNew)",
            ExpressionAttributeNames: {
                "#service_status": "service_status"
            },
            ExpressionAttributeValues: {
                ":status": statusToExclude,
                ":statusNew": statusToExcludeNew
            }
        };      
        const body = await mydynamodb.send(new ScanCommand(params));
        return body.Items; // Return JSON string of items
    } catch (error) {
        console.error("Error fetching DynamoDB service requests:", error);
        throw error; // Re-throw the error to handle it further up the call stack
    }
}

We will replace the previous dataService with the new dynamoService.

import { getDynamoServiceRequests } from './dynamoService.mjs';
const jsonArray = await getDynamoServiceRequests();

Click Deploy and then Test the deployment. You should only see the single response in the body instead of the three hardcoded items we had before.

Go to the website and verify that you are getting a single ticket back. You can get the link from your EC2.

Website App Troubleshooting

Your website should be running on your EC2 instance. You can find the link to the website by clicking on the checkmark next to the EC2 and at the bottom of the page finding the Public IPv4 DNS.

If your website isn't running, you'll need to check to see if the httpd service is running and enabled. You can check this by connecting to the EC2 and then running the following commands:

sudo systemctl enable httpd

sudo systemctl start httpd

If the website is up, but not working, then you'll need to dig into the error console (Right click inspect and choose console) to troubleshoot the errors. This and CloudWatch are your best options for tracking down errors.

Add information to the database

Go back to the dynamodb table and click Explore table items Click run. Down below it should return the 8B1111 item

Update the values on this page to this:

Click create item

Go back to the website and you should see both items under service request.

Update a vehicle

Go update the value of the service_status of the 8B1111 vehicle to have a service_status of Completed

Go view the website and you should only see one service request.

Lab Summary:

In this lab, the primary objective was to create a DynamoDB table named VehicleServices to store service requests related to vehicle maintenance. Additionally, the lab focused on updating a Lambda function to interact with this DynamoDB table for fetching and manipulating service request data.

Key Concepts Explained:

DynamoDB

Amazon DynamoDB is a fully managed NoSQL database service provided by Amazon Web Services (AWS). It is designed to provide seamless scalability, low-latency performance, and high availability for applications that require fast and flexible data storage. DynamoDB is suitable for a wide range of use cases, from mobile and web applications to gaming, IoT (Internet of Things), and real-time analytics.

Key features and concepts of DynamoDB include:

Fully Managed Service: DynamoDB is a serverless database service, which means AWS manages all aspects of database provisioning, scaling, and maintenance, allowing developers to focus on building applications without managing infrastructure.

NoSQL Database: DynamoDB is a NoSQL (non-relational) database that offers flexible schema design, allowing developers to store and retrieve data in a schema-less format. It supports various data types, including scalar types (like string, number, boolean) and complex types (like lists and maps).

Scalability and Performance: DynamoDB automatically scales to accommodate growing workloads by distributing data across multiple servers and partitions. It provides single-digit millisecond latency for read and write operations, making it suitable for applications that require high-performance data access.

Data Model: DynamoDB uses primary keys for data retrieval and storage. Tables can have a partition key (and optionally a sort key), enabling efficient querying and indexing of data. Global and local secondary indexes can be defined to support different access patterns.

Consistency and Durability: DynamoDB offers configurable consistency models (strong consistency or eventual consistency) based on application requirements. It also provides built-in replication and backup capabilities for data durability and disaster recovery.

  1. DynamoDB: In this lab, a DynamoDB table was created with specific attributes like license_plate and service_id to store service request details.
  2. DynamoDB Table Configuration: The lab covered the configuration of a DynamoDB table, including defining a primary partition key (license_plate) and a sort key (service_id). Additionally, a global secondary index (StatusIndex) was created to facilitate efficient querying based on the service_status attribute.
  3. Lambda Function Integration with DynamoDB: The Lambda function (getServiceRequest) was updated to use the AWS SDK for DynamoDB to interact with the VehicleServices table. The function was modified to retrieve service requests based on certain filtering conditions (e.g., excluding completed or new requests) using a ScanCommand.
  4. Data Manipulation in DynamoDB: The lab demonstrated how to add and update items in the DynamoDB table directly from the AWS Management Console by duplicating an existing item and updating its attributes (e.g., changing service_status to "Accepted" for a new service request).

Reflection Questions: