Skip to content

Commit

Permalink
Model consumer update for Nodejs 18.x
Browse files Browse the repository at this point in the history
  • Loading branch information
MABatin committed Sep 14, 2024
1 parent 0a98522 commit 849fca7
Show file tree
Hide file tree
Showing 10 changed files with 6,276 additions and 4,197 deletions.
2 changes: 1 addition & 1 deletion consumers/online/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"@types/node": "17.0.9",
"@typescript-eslint/eslint-plugin": "^5.10.0",
"@typescript-eslint/parser": "^5.10.0",
"aws-cdk": "2.76.0",
"aws-cdk": "2.157.0",
"esbuild": "0.17.18",
"eslint": "8.7.0",
"eslint-config-prettier": "^8.3.0",
Expand Down
5 changes: 4 additions & 1 deletion consumers/online/packages/data-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
"license": "MIT-0",
"dependencies": {
"@aws-prototype/model-consumer-online-data-type": "1.0.0",
"aws-sdk": "^2"
"@aws-sdk/client-s3": "^3.651.0",
"@aws-sdk/client-sagemaker-runtime": "^3.0.0",
"@aws-sdk/client-dynamodb": "^3.0.0",
"@aws-sdk/util-dynamodb": "^3.0.0"
},
"scripts": {
"build": "tsc",
Expand Down
67 changes: 37 additions & 30 deletions consumers/online/packages/data-api/src/data.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT-0
import * as AWS from 'aws-sdk';

import { SageMakerRuntimeClient, InvokeEndpointCommand } from '@aws-sdk/client-sagemaker-runtime';
import { DynamoDBClient, PutItemCommand, UpdateItemCommand } from '@aws-sdk/client-dynamodb';
import { marshall, unmarshall } from '@aws-sdk/util-dynamodb'; // Used for converting data to DynamoDB format
import { DataType, AddLabelRequest } from '@aws-prototype/model-consumer-online-data-type';

const sageMakerEndpointName = process.env.SAGEMAKER_ENDPOINT_NAME || '';
const dataTableName = process.env.DATA_TABLE_NAME || '';
const region = process.env.AWS_REGION || '';

const region = process.env.AWS_REGION;
AWS.config.update({ region });

const sagemaker = new AWS.SageMakerRuntime();
const documentClient = new AWS.DynamoDB.DocumentClient();
// Initialize AWS SDK v3 clients
const sagemaker = new SageMakerRuntimeClient({ region });
const dynamoDBClient = new DynamoDBClient({ region });

const getInput = (data: DataType): string => {
return `${data.sex},${data.length},${data.diameter},${data.height},${data.wholeWeight},${data.shuckedWeight},${data.visceraWeight},${data.shellWeight}`;
Expand All @@ -19,48 +21,53 @@ const getInput = (data: DataType): string => {
const getInference = async (id: string, data: DataType) => {
const inputString = getInput(data);
console.log('Input data: ', inputString);
const result = await sagemaker
.invokeEndpoint({
Body: inputString,
EndpointName: sageMakerEndpointName,
ContentType: 'text/csv',
Accept: 'application/json',
})
.promise();

const predict = result.Body.toString();
// Invoke SageMaker endpoint using v3
const invokeCommand = new InvokeEndpointCommand({
Body: Buffer.from(inputString),
EndpointName: sageMakerEndpointName,
ContentType: 'text/csv',
Accept: 'application/json',
});

const result = await sagemaker.send(invokeCommand);
const predict = result.Body?.toString() || '';
console.log('Prediction: ', predict);
//Write to DynamoDB with predict value

// Write to DynamoDB with the predicted value
const record = {
...data,
id,
predict,
};
const dynamoDBPutRequest = {

const dynamoDBPutCommand = new PutItemCommand({
TableName: dataTableName,
Item: {
...record,
},
};
await documentClient.put(dynamoDBPutRequest).promise();
Item: marshall(record),
});

await dynamoDBClient.send(dynamoDBPutCommand);

return record;
};

const addLabel = async (id: string, { actual }: AddLabelRequest) => {
const updateExpression = 'SET actual = :a';
const params = {

const updateCommand = new UpdateItemCommand({
TableName: dataTableName,
Key: {
Key: marshall({
id,
},
}),
UpdateExpression: updateExpression,
ExpressionAttributeValues: {
ExpressionAttributeValues: marshall({
':a': actual,
},
}),
ReturnValues: 'ALL_NEW',
};
const response = await documentClient.update(params).promise();
return response.$response.data;
});

const response = await dynamoDBClient.send(updateCommand);
return response.Attributes ? unmarshall(response.Attributes) : {};
};

export { getInference, addLabel };
4 changes: 2 additions & 2 deletions consumers/online/packages/data-api/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"compilerOptions": {
"target": "ES2018",
"target": "ESNext",
"module": "commonjs",
"lib": [
"es2018"
"esnext", "dom"
],
"declaration": true,
"strict": true,
Expand Down
4 changes: 2 additions & 2 deletions consumers/online/packages/data-type/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"compilerOptions": {
"target": "ES2018",
"target": "ESNext",
"module": "commonjs",
"lib": [
"es2018"
"esnext", "dom"
],
"declaration": true,
"strict": true,
Expand Down
4 changes: 2 additions & 2 deletions consumers/online/packages/infrastructure/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
"lint:fix": "eslint --fix ."
},
"devDependencies": {
"aws-cdk": "2.76.0",
"aws-cdk": "2.157.0",
"esbuild": "0.17.18"
},
"dependencies": {
"aws-cdk-lib": "2.80.0",
"aws-cdk-lib": "2.157.0",
"constructs": "^10.2.5",
"source-map-support": "^0.5.21"
}
Expand Down
4 changes: 2 additions & 2 deletions consumers/online/packages/infrastructure/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"compilerOptions": {
"target": "ES2018",
"target": "ESNext",
"module": "commonjs",
"lib": [
"es2018"
"esnext", "dom"
],
"declaration": true,
"strict": true,
Expand Down
50 changes: 26 additions & 24 deletions consumers/online/packages/website-index-builder/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT-0
import * as AWS from 'aws-sdk';

import { S3Client, PutObjectCommand, DeleteObjectCommand } from '@aws-sdk/client-s3';
import * as fs from 'fs';
import archiver = require('archiver');

AWS.config.update({ region: process.env.AWS_REGION });

const s3 = new AWS.S3();
// Initialize the S3 client with region
const s3 = new S3Client({ region: process.env.AWS_REGION });

interface CustomResourceEvent {
RequestType: string;
Expand Down Expand Up @@ -37,7 +37,7 @@ const createZipFileContent = async (objectKey: string, indexFileContent: string)

archiveHandler.append(indexFileContent, { name: 'index.html' });

archiveHandler.finalize();
await archiveHandler.finalize();
await closed;

return fs.readFileSync(filePath);
Expand All @@ -50,18 +50,19 @@ const createOrUpdateConfig = async (config: Record<string, string>, template: st

if (indexFileContent.search(configContent) < 0) {
console.log('Index file content:', indexFileContent);
throw 'Failed in generating Index file';
throw new Error('Failed in generating Index file');
}

const zipFileContent = await createZipFileContent(objectKey, indexFileContent);

await s3
.putObject({
Bucket: s3BucketName,
Key: objectKey,
Body: zipFileContent,
})
.promise();
// Upload the zip file to S3 using the v3 SDK
const putObjectCommand = new PutObjectCommand({
Bucket: s3BucketName,
Key: objectKey,
Body: zipFileContent,
});

await s3.send(putObjectCommand);

return {
PhysicalResourceId: objectKey,
Expand All @@ -70,12 +71,13 @@ const createOrUpdateConfig = async (config: Record<string, string>, template: st

const deleteConfig = async (configKeyToDelete: string, s3BucketName: string) => {
try {
await s3
.deleteObject({
Bucket: s3BucketName,
Key: configKeyToDelete,
})
.promise();
// Delete the object from S3 using the v3 SDK
const deleteObjectCommand = new DeleteObjectCommand({
Bucket: s3BucketName,
Key: configKeyToDelete,
});

await s3.send(deleteObjectCommand);
} catch (e) {
console.log('Error in deleting the old config object', e);
}
Expand All @@ -84,8 +86,8 @@ const deleteConfig = async (configKeyToDelete: string, s3BucketName: string) =>
};

const dispatch = async (event: CustomResourceEvent, requestType: string) => {
const props = event['ResourceProperties'];
const resourceId = event['PhysicalResourceId'];
const props = event.ResourceProperties;
const resourceId = event.PhysicalResourceId;
switch (requestType) {
case 'Create':
case 'Update': {
Expand All @@ -97,14 +99,14 @@ const dispatch = async (event: CustomResourceEvent, requestType: string) => {
return deleteConfig(resourceId, s3BucketName);
}
default:
throw 'Unsupported RequestType';
throw new Error('Unsupported RequestType');
}
};

exports.handler = async (event: CustomResourceEvent) => {
export const handler = async (event: CustomResourceEvent) => {
console.log('Event: \n' + JSON.stringify(event, null, 2));
try {
const requestType = event['RequestType'];
const requestType = event.RequestType;
const data = await dispatch(event, requestType);
console.log('Response', data);
return data;
Expand Down
4 changes: 2 additions & 2 deletions consumers/online/packages/website-index-builder/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"compilerOptions": {
"target": "ES2018",
"target": "ESNext",
"module": "commonjs",
"lib": [
"es2018"
"esnext", "dom"
],
"declaration": true,
"strict": true,
Expand Down
Loading

0 comments on commit 849fca7

Please sign in to comment.