Skip to content

Commit

Permalink
return the Lambda request ID instead of DDB request ID
Browse files Browse the repository at this point in the history
  • Loading branch information
tmclaugh committed Oct 22, 2024
1 parent 5550c0c commit 029399c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 18 deletions.
14 changes: 5 additions & 9 deletions src/handlers/CreateEntity/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
DynamoDBClient,
PutItemCommand,
PutItemCommandInput,
PutItemCommandOutput,
DynamoDBServiceException
} from '@aws-sdk/client-dynamodb'
import {
Expand All @@ -32,8 +31,7 @@ interface Item extends Entity {
itemType: string
}

export async function putEntity(entity: Entity): Promise<PutItemCommandOutput> {
let output: PutItemCommandOutput
export async function putEntity(entity: Entity): Promise<void> {

const namespace = entity.metadata.namespace
const kind = entity.kind.toLowerCase()
Expand All @@ -54,7 +52,7 @@ export async function putEntity(entity: Entity): Promise<PutItemCommandOutput> {

try {
const command = new PutItemCommand(params)
output = await DDB_CLIENT.send(command)
const output = await DDB_CLIENT.send(command)
LOGGER.debug('PutItemCommand succeeded', { output })
} catch (error) {
LOGGER.error({
Expand All @@ -64,13 +62,12 @@ export async function putEntity(entity: Entity): Promise<PutItemCommandOutput> {
})
throw error
}

return output
}


export async function handler (event: APIGatewayProxyEvent, _: Context): Promise<APIGatewayProxyResult> {
export async function handler (event: APIGatewayProxyEvent, context: Context): Promise<APIGatewayProxyResult> {
LOGGER.debug('Received event', { event })
const event_id = context.awsRequestId

const entity: Entity = JSON.parse(event.body || '{}') // Already validated body at Gateway

Expand All @@ -79,7 +76,7 @@ export async function handler (event: APIGatewayProxyEvent, _: Context): Promise
try {
const output = await putEntity(entity)
statusCode = 201
body = JSON.stringify({'request_id': output.$metadata.requestId})
body = JSON.stringify({'request_id': event_id})
} catch (error) {
LOGGER.error("Operation failed", { event })
const fault = (<DynamoDBServiceException>error).$fault
Expand All @@ -97,7 +94,6 @@ export async function handler (event: APIGatewayProxyEvent, _: Context): Promise
})
}


return {
statusCode,
body
Expand Down
14 changes: 5 additions & 9 deletions src/handlers/DeleteEntity/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
DynamoDBClient,
DeleteItemCommand,
DeleteItemCommandInput,
DeleteItemCommandOutput,
DynamoDBServiceException
} from '@aws-sdk/client-dynamodb'

Expand All @@ -24,9 +23,7 @@ export async function deleteEntity(
namespace: string,
kind: string,
name: string
): Promise<DeleteItemCommandOutput> {
let output: DeleteItemCommandOutput

): Promise<void> {
const params: DeleteItemCommandInput = {
TableName: DDB_TABLE_NAME,
Key: {
Expand All @@ -37,7 +34,7 @@ export async function deleteEntity(

try {
const command = new DeleteItemCommand(params)
output = await DDB_CLIENT.send(command)
const output = await DDB_CLIENT.send(command)
LOGGER.debug('DeleteItemCommand succeeded', { output })
} catch (error) {
LOGGER.error({
Expand All @@ -47,13 +44,12 @@ export async function deleteEntity(
})
throw error
}

return output
}


export async function handler (event: APIGatewayProxyEvent, _: Context): Promise<APIGatewayProxyResult> {
export async function handler (event: APIGatewayProxyEvent, context: Context): Promise<APIGatewayProxyResult> {
LOGGER.debug('Received event', { event })
const event_id = context.awsRequestId

const namespace = event.pathParameters?.namespace as string
const kind = event.pathParameters?.kind as string
Expand All @@ -68,7 +64,7 @@ export async function handler (event: APIGatewayProxyEvent, _: Context): Promise
name
)
statusCode = 200
body = JSON.stringify({"request_id": output.$metadata.requestId})
body = JSON.stringify({"request_id": event_id})
} catch (error) {
LOGGER.error("Operation failed", { event })
const fault = (<DynamoDBServiceException>error).$fault
Expand Down

0 comments on commit 029399c

Please sign in to comment.