Skip to content

Commit 6e5e6c9

Browse files
committed
docs(examples): refresh cdk example
1 parent dab4736 commit 6e5e6c9

25 files changed

+689
-262
lines changed

examples/cdk/functions/common/clients/dynamodb.ts

Lines changed: 0 additions & 27 deletions
This file was deleted.

examples/cdk/functions/common/constants.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.

examples/cdk/functions/common/powertools.ts

Lines changed: 0 additions & 27 deletions
This file was deleted.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { DynamoDBClient } from '@aws-sdk/client-dynamodb';
2+
import { DynamoDBDocumentClient } from '@aws-sdk/lib-dynamodb';
3+
import { tracer } from '#powertools';
4+
5+
// Create DynamoDB Client and patch it for tracing
6+
const ddbClient = tracer.captureAWSv3Client(new DynamoDBClient({}));
7+
8+
// Create the DynamoDB Document client.
9+
const docClient = DynamoDBDocumentClient.from(ddbClient, {
10+
marshallOptions: {
11+
removeUndefinedValues: true,
12+
},
13+
});
14+
15+
export { ddbClient, docClient };
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { getStringFromEnv } from '#helpers/utils';
2+
3+
// Get the DynamoDB table name from environment variables
4+
const itemsTableName = getStringFromEnv('TABLE_NAME');
5+
const ssmParameterName = getStringFromEnv('SSM_PARAMETER_NAME');
6+
7+
export { itemsTableName, ssmParameterName };
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class ItemNotFound extends Error {
2+
public constructor(message: string) {
3+
super(message);
4+
this.name = 'ItemNotFound';
5+
}
6+
}
7+
8+
export { ItemNotFound };
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { docClient } from '#clients/dynamodb';
2+
import { itemsTableName } from '#constants';
3+
import type { DebugLogger } from '#types';
4+
import { GetCommand, type GetCommandOutput } from '@aws-sdk/lib-dynamodb';
5+
6+
/**
7+
* Fetch an item from the DynamoDB table.
8+
*
9+
* @param id The ID of the item to fetch from the DynamoDB table
10+
* @param logger A logger instance
11+
*/
12+
const getItemDynamoDB = async (
13+
id: string,
14+
logger: DebugLogger
15+
): Promise<GetCommandOutput['Item']> => {
16+
const response = await docClient.send(
17+
new GetCommand({
18+
TableName: itemsTableName,
19+
Key: {
20+
id,
21+
},
22+
})
23+
);
24+
25+
logger.debug(`ddb response`, {
26+
response,
27+
});
28+
29+
return response.Item;
30+
};
31+
32+
export { getItemDynamoDB };
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { getParameter } from '@aws-lambda-powertools/parameters/ssm';
2+
3+
/**
4+
* Fetch a string from an SSM parameter.
5+
*
6+
* Throws an error if the parameter is not found.
7+
*
8+
* @param name The name of the parameter
9+
*/
10+
const getSSMStringParameter = async (name: string): Promise<string> => {
11+
const parameter = await getParameter(name, { maxAge: 600 });
12+
13+
if (!parameter) {
14+
throw new Error(`Parameter ${name} not found`);
15+
}
16+
17+
return parameter;
18+
};
19+
20+
export { getSSMStringParameter };
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { docClient } from '#clients/dynamodb';
2+
import { itemsTableName } from '#constants';
3+
import type { DebugLogger } from '#types';
4+
import { PutCommand } from '@aws-sdk/lib-dynamodb';
5+
import { randomUUID } from 'node:crypto';
6+
7+
/**
8+
* Put an item in the DynamoDB table.
9+
*
10+
* When the item is put in the table, the item's ID and name are returned.
11+
*
12+
* @param name The name of the item to put in the DynamoDB table
13+
* @param logger A logger instance
14+
*/
15+
const putItemInDynamoDB = async (
16+
name: string,
17+
logger: DebugLogger
18+
): Promise<{ id: string; name: string }> => {
19+
const item = {
20+
id: randomUUID(),
21+
name,
22+
};
23+
24+
const response = await docClient.send(
25+
new PutCommand({
26+
TableName: itemsTableName,
27+
Item: item,
28+
})
29+
);
30+
31+
logger.debug(`ddb response`, {
32+
response,
33+
});
34+
35+
return item;
36+
};
37+
38+
export { putItemInDynamoDB };
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { docClient } from '#clients/dynamodb';
2+
import { itemsTableName } from '#constants';
3+
import type { DebugLogger } from '#types';
4+
import { ScanCommand, type ScanCommandOutput } from '@aws-sdk/lib-dynamodb';
5+
6+
/**
7+
* Scan the DynamoDB table and return all items.
8+
*
9+
* @note this function is purposefully not paginated to keep the example simple
10+
*
11+
* @param logger A logger instance
12+
*/
13+
const scanItemsDynamoDB = async (
14+
logger: DebugLogger
15+
): Promise<ScanCommandOutput['Items']> => {
16+
const response = await docClient.send(
17+
new ScanCommand({
18+
TableName: itemsTableName,
19+
})
20+
);
21+
22+
logger.debug(`ddb response`, {
23+
response,
24+
});
25+
26+
return response.Items;
27+
};
28+
29+
export { scanItemsDynamoDB };
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Assert that the given value is an error.
3+
*
4+
* TypeScript treats errors as `unknown` when caught,
5+
* so we need to assert that the value is an error.
6+
*
7+
* We need this because in JavaScript, any value can be thrown
8+
* using the `throw` keyword i.e. `throw 1` is valid.
9+
*
10+
* @param error The value to assert is an error
11+
*/
12+
// eslint-disable-next-line func-style -- type assertions cannot use arrow functions microsoft/TypeScript#34523
13+
function assertIsError(error: unknown): asserts error is Error {
14+
if (!(error instanceof Error)) {
15+
throw error;
16+
}
17+
}
18+
19+
/**
20+
* Fetch a string from an environment variable.
21+
*
22+
* Throws an error if the environment variable is not set.
23+
*
24+
* @param name The name of the environment variable
25+
*/
26+
const getStringFromEnv = (name: string): string => {
27+
const value = process.env[name];
28+
if (!value) {
29+
throw new Error(`${name} environment variable is not set`);
30+
}
31+
32+
return value;
33+
};
34+
35+
export { assertIsError, getStringFromEnv };
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Service name for the application to use in logs, metrics, and traces.
3+
*
4+
* Can also be configured via POWERTOOLS_SERVICE_NAME environment variable.
5+
*/
6+
const serviceName = 'items-store';
7+
/**
8+
* Namespace for metrics to use in metrics.
9+
*
10+
* Can also be configured via POWERTOOLS_METRICS_NAMESPACE environment variable.
11+
*/
12+
const metricsNamespace = 'pwwertools-example';
13+
/**
14+
* Key-value pairs to include in all metrics and logs.
15+
*/
16+
const defaultValues = {
17+
executionEnv: process.env.AWS_EXECUTION_ENV || 'N/A',
18+
};
19+
20+
export { serviceName, metricsNamespace, defaultValues };
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* We have single entry point for all the powertools modules so that functions that need only one
3+
* can bundle only that one that they need and keep the bundle size small.
4+
*/
5+
import { logger } from './logger.js';
6+
import { metrics } from './metrics.js';
7+
import { tracer } from './tracer.js';
8+
9+
// We export all three modules for those functions who need to use all of them
10+
export { logger, metrics, tracer };
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { Logger } from '@aws-lambda-powertools/logger';
2+
import { PT_VERSION as version } from '@aws-lambda-powertools/commons';
3+
import { serviceName, defaultValues } from './constants.js';
4+
5+
/**
6+
* Create logger instance with centralized configuration so that
7+
* all functions have consistent logging behavior.
8+
*/
9+
const logger = new Logger({
10+
logLevel: 'debug',
11+
serviceName,
12+
persistentLogAttributes: {
13+
...defaultValues,
14+
region: process.env.AWS_REGION || 'N/A',
15+
logger: {
16+
name: '@aws-lambda-powertools/logger',
17+
version,
18+
},
19+
},
20+
});
21+
22+
export { logger };
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { Metrics } from '@aws-lambda-powertools/metrics';
2+
import { serviceName, metricsNamespace, defaultValues } from './constants.js';
3+
4+
/**
5+
* Create metrics instance with centralized configuration so that
6+
* all functions have the same dimensions and namespace.
7+
*/
8+
const metrics = new Metrics({
9+
serviceName,
10+
namespace: metricsNamespace,
11+
defaultDimensions: defaultValues,
12+
});
13+
14+
export { metrics };
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { Tracer } from '@aws-lambda-powertools/tracer';
2+
import { serviceName } from './constants.js';
3+
4+
/**
5+
* Create tracer instance with centralized configuration so that
6+
* all traces have the same service name as an annotation.
7+
*/
8+
const tracer = new Tracer({
9+
serviceName,
10+
});
11+
12+
export { tracer };
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import type { LogItemExtraInput } from '@aws-lambda-powertools/logger/types';
2+
3+
type DebugLogger = {
4+
debug: (message: string, ...extraInput: LogItemExtraInput) => void;
5+
};
6+
7+
export type { DebugLogger };

0 commit comments

Comments
 (0)