Skip to content

Instantly share code, notes, and snippets.

@zxkane
Created April 26, 2024 09:00
Show Gist options
  • Save zxkane/eb55229f4bc55fdb7aa232b7e2c31d41 to your computer and use it in GitHub Desktop.
Save zxkane/eb55229f4bc55fdb7aa232b7e2c31d41 to your computer and use it in GitHub Desktop.
simple js app access ddb and states API
import { DynamoDBClient} from '@aws-sdk/client-dynamodb';
import { GetCommand } from '@aws-sdk/lib-dynamodb';
import { marshall, unmarshall } from '@aws-sdk/util-dynamodb';
import { SFNClient, ListExecutionsCommand } from '@aws-sdk/client-sfn';
const logRequestMiddleware = (next, _context) => async (args) => {
console.log('Request:', args.request);
return next(args);
};
const sfnClient = new SFNClient();
sfnClient.middlewareStack.add(logRequestMiddleware, { step: 'finalizeRequest' });
const client = new DynamoDBClient();
client.middlewareStack.add(logRequestMiddleware, { step: 'finalizeRequest' });
const tableName = 'tableName';
const stateMachineArn = 'stateArn';
export const handler = async (event) => {
try {
const res = await sfnClient.send(new ListExecutionsCommand({
stateMachineArn,
statusFilter: 'RUNNING',
}));
console.log(res);
return await getItem(event);
} catch (err) {
console.error(err);
return {
statusCode: 500,
body: 'Internal Server Error'
};
}
};
const getItem = async (event) => {
const params = {
TableName: tableName,
Key: marshall({ s3_uri: event.id })
};
const getCommand = new GetCommand(params);
const { Item } = await client.send(getCommand);
return {
statusCode: 200,
body: JSON.stringify(unmarshall(Item))
};
};
// handler({ id: '1' });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment