Created
April 26, 2024 09:00
-
-
Save zxkane/eb55229f4bc55fdb7aa232b7e2c31d41 to your computer and use it in GitHub Desktop.
simple js app access ddb and states API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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