From de85af38fedaeb5b987862cf490ea7e8dfbd87ec Mon Sep 17 00:00:00 2001 From: Alex Maragakis Date: Fri, 14 Jan 2022 16:17:34 +0000 Subject: [PATCH 1/3] Improve examples/cdk/README.md --- examples/cdk/README.md | 27 ++++++++++++------- examples/cdk/bin/cdk-app.ts | 2 +- packages/metrics/src/Metrics.ts | 4 +-- .../tests/e2e/decorator.test.MyFunction.ts | 2 +- .../e2e/standardFunctions.test.MyFunction.ts | 2 +- 5 files changed, 22 insertions(+), 15 deletions(-) diff --git a/examples/cdk/README.md b/examples/cdk/README.md index 3247665185..57628ffe27 100644 --- a/examples/cdk/README.md +++ b/examples/cdk/README.md @@ -1,14 +1,21 @@ -# Welcome to your CDK TypeScript project! +# AWS Lambda Powertools (TypeScript) examples in CDK -This is a blank project for TypeScript development with CDK. +This is a deployable CDK app that deploys AWS Lambda functions as part of a CloudFormation stack. These Lambda functions use the utilities made available as part of AWS Lambda Powertools (TypeScript) to demonstrate their usage. -The `cdk.json` file tells the CDK Toolkit how to execute your app. +The example functions, located in the `lib` folder, are triggered automatically when deployed using the CDK construct defined in `lib/example-function.ts`. -## Useful commands +## Deploying the stack - * `npm run build` compile typescript to js - * `npm run watch` watch for changes and compile - * `npm run test` perform the jest unit tests - * `cdk deploy` deploy this stack to your default AWS account/region - * `cdk diff` compare deployed stack with current state - * `cdk synth` emits the synthesized CloudFormation template + * Navigate to this location of the repo in your terminal (`examples/cdk`) + * `npm install` + * `npx cdk deploy --all --profile ` + +Note: Prior to deploying you may need to run `npx cdk bootstrap aws:/// --profile ` if you have not already bootstrapped your account for CDK. + +## Viewing Utility Outputs + +All utility outputs can be viewed from the CloudWatch console. + + * `Logger` outputs to Logs > Log groups + * `Metrics` outputs to Metrics > All metrics > LambdaPowertoolsTypeScript-CDKExample + * `Tracer` outputs to X-Ray traces > Traces diff --git a/examples/cdk/bin/cdk-app.ts b/examples/cdk/bin/cdk-app.ts index bbcff99e06..9d8060953b 100644 --- a/examples/cdk/bin/cdk-app.ts +++ b/examples/cdk/bin/cdk-app.ts @@ -4,4 +4,4 @@ import * as cdk from 'aws-cdk-lib'; import { CdkAppStack } from '../lib/example-stack'; const app = new cdk.App(); -new CdkAppStack(app, 'CdkAppStack', {}); \ No newline at end of file +new CdkAppStack(app, 'LambdaPowertoolsTypeScript-ExamplesCdkStack', {}); \ No newline at end of file diff --git a/packages/metrics/src/Metrics.ts b/packages/metrics/src/Metrics.ts index 3afb21a6f4..be966ecfc0 100644 --- a/packages/metrics/src/Metrics.ts +++ b/packages/metrics/src/Metrics.ts @@ -210,7 +210,7 @@ class Metrics implements MetricsInterface { * import { Metrics, MetricUnits } from '@aws-lambda-powertools/metrics'; * import { Callback, Context } from 'aws-lambda'; * - * const metrics = new Metrics({namespace:"CDKExample", serviceName:"withDecorator"}); + * const metrics = new Metrics({namespace:"LambdaPowertoolsTypeScript-CDKExample", serviceName:"withDecorator"}); * * export class MyFunctionWithDecorator { * @@ -267,7 +267,7 @@ class Metrics implements MetricsInterface { * ```typescript * import { Metrics, MetricUnits } from '@aws-lambda-powertools/metrics'; * - * const metrics = new Metrics({namespace: "CDKExample", serviceName: "MyFunction"}); // Sets metric namespace, and service as a metric dimension + * const metrics = new Metrics({namespace: "LambdaPowertoolsTypeScript-CDKExample", serviceName: "MyFunction"}); // Sets metric namespace, and service as a metric dimension * * export const handler = async (_event: any, _context: any) => { * metrics.addMetric('test-metric', MetricUnits.Count, 10); diff --git a/packages/metrics/tests/e2e/decorator.test.MyFunction.ts b/packages/metrics/tests/e2e/decorator.test.MyFunction.ts index 61f1720017..5c6e7a2d29 100644 --- a/packages/metrics/tests/e2e/decorator.test.MyFunction.ts +++ b/packages/metrics/tests/e2e/decorator.test.MyFunction.ts @@ -2,7 +2,7 @@ import { Metrics, MetricUnits } from '../../src'; import { Context } from 'aws-lambda'; import { LambdaInterface } from '../../examples/utils/lambda/LambdaInterface'; -const namespace = process.env.EXPECTED_NAMESPACE ?? 'CDKExample'; +const namespace = process.env.EXPECTED_NAMESPACE ?? 'LambdaPowertoolsTypeScript-CDKExample'; const serviceName = process.env.EXPECTED_SERVICE_NAME ?? 'MyFunctionWithStandardHandler'; const metricName = process.env.EXPECTED_METRIC_NAME ?? 'MyMetric'; const metricUnit = (process.env.EXPECTED_METRIC_UNIT as MetricUnits) ?? MetricUnits.Count; diff --git a/packages/metrics/tests/e2e/standardFunctions.test.MyFunction.ts b/packages/metrics/tests/e2e/standardFunctions.test.MyFunction.ts index 732ab872b4..ec26dbb898 100644 --- a/packages/metrics/tests/e2e/standardFunctions.test.MyFunction.ts +++ b/packages/metrics/tests/e2e/standardFunctions.test.MyFunction.ts @@ -1,7 +1,7 @@ import { Metrics, MetricUnits } from '../../src'; import { Context } from 'aws-lambda'; -const namespace = process.env.EXPECTED_NAMESPACE ?? 'CDKExample'; +const namespace = process.env.EXPECTED_NAMESPACE ?? 'LambdaPowertoolsTypeScript-CDKExample'; const serviceName = process.env.EXPECTED_SERVICE_NAME ?? 'MyFunctionWithStandardHandler'; const metricName = process.env.EXPECTED_METRIC_NAME ?? 'MyMetric'; const metricUnit = (process.env.EXPECTED_METRIC_UNIT as MetricUnits) ?? MetricUnits.Count; From bd217ef31950d6237b46cce6745263dbfbc423e1 Mon Sep 17 00:00:00 2001 From: Alex Maragakis Date: Fri, 14 Jan 2022 17:27:43 +0000 Subject: [PATCH 2/3] Address PR comments on README update --- examples/cdk/README.md | 17 ++++++++++------- packages/metrics/src/Metrics.ts | 4 ++-- .../tests/e2e/decorator.test.MyFunction.ts | 2 +- .../e2e/standardFunctions.test.MyFunction.ts | 2 +- 4 files changed, 14 insertions(+), 11 deletions(-) diff --git a/examples/cdk/README.md b/examples/cdk/README.md index 57628ffe27..cc7da9abc9 100644 --- a/examples/cdk/README.md +++ b/examples/cdk/README.md @@ -2,20 +2,23 @@ This is a deployable CDK app that deploys AWS Lambda functions as part of a CloudFormation stack. These Lambda functions use the utilities made available as part of AWS Lambda Powertools (TypeScript) to demonstrate their usage. -The example functions, located in the `lib` folder, are triggered automatically when deployed using the CDK construct defined in `lib/example-function.ts`. +You will need to have a valid AWS Account in order to deploy these resources. These resources may incur costs to your AWS Account. + +The example functions, located in the `lib` folder, are invoked automatically, twice, when deployed using the CDK construct defined in `lib/example-function.ts`. The first invocation demonstrates the effect on logs/metrics/annotations when the Lambda function has a cold start, and the latter without a cold start. ## Deploying the stack + * Ensure that CDK v2 is installed globally on your machine (if not, run `npm install -g aws-cdk`) * Navigate to this location of the repo in your terminal (`examples/cdk`) * `npm install` - * `npx cdk deploy --all --profile ` + * `cdk deploy --all --profile ` -Note: Prior to deploying you may need to run `npx cdk bootstrap aws:/// --profile ` if you have not already bootstrapped your account for CDK. +Note: Prior to deploying you may need to run `cdk bootstrap aws:/// --profile ` if you have not already bootstrapped your account for CDK. ## Viewing Utility Outputs -All utility outputs can be viewed from the CloudWatch console. +All utility outputs can be viewed in the Amazon CloudWatch console. - * `Logger` outputs to Logs > Log groups - * `Metrics` outputs to Metrics > All metrics > LambdaPowertoolsTypeScript-CDKExample - * `Tracer` outputs to X-Ray traces > Traces + * `Logger` output can be found in Logs > Log groups + * `Metrics` output can be found in Metrics > All metrics > CdkExample + * `Tracer` output can be found in X-Ray traces > Traces diff --git a/packages/metrics/src/Metrics.ts b/packages/metrics/src/Metrics.ts index be966ecfc0..764d840b10 100644 --- a/packages/metrics/src/Metrics.ts +++ b/packages/metrics/src/Metrics.ts @@ -210,7 +210,7 @@ class Metrics implements MetricsInterface { * import { Metrics, MetricUnits } from '@aws-lambda-powertools/metrics'; * import { Callback, Context } from 'aws-lambda'; * - * const metrics = new Metrics({namespace:"LambdaPowertoolsTypeScript-CDKExample", serviceName:"withDecorator"}); + * const metrics = new Metrics({namespace:"CdkExample", serviceName:"withDecorator"}); * * export class MyFunctionWithDecorator { * @@ -267,7 +267,7 @@ class Metrics implements MetricsInterface { * ```typescript * import { Metrics, MetricUnits } from '@aws-lambda-powertools/metrics'; * - * const metrics = new Metrics({namespace: "LambdaPowertoolsTypeScript-CDKExample", serviceName: "MyFunction"}); // Sets metric namespace, and service as a metric dimension + * const metrics = new Metrics({namespace: "CdkExample", serviceName: "MyFunction"}); // Sets metric namespace, and service as a metric dimension * * export const handler = async (_event: any, _context: any) => { * metrics.addMetric('test-metric', MetricUnits.Count, 10); diff --git a/packages/metrics/tests/e2e/decorator.test.MyFunction.ts b/packages/metrics/tests/e2e/decorator.test.MyFunction.ts index 5c6e7a2d29..b9015ab956 100644 --- a/packages/metrics/tests/e2e/decorator.test.MyFunction.ts +++ b/packages/metrics/tests/e2e/decorator.test.MyFunction.ts @@ -2,7 +2,7 @@ import { Metrics, MetricUnits } from '../../src'; import { Context } from 'aws-lambda'; import { LambdaInterface } from '../../examples/utils/lambda/LambdaInterface'; -const namespace = process.env.EXPECTED_NAMESPACE ?? 'LambdaPowertoolsTypeScript-CDKExample'; +const namespace = process.env.EXPECTED_NAMESPACE ?? 'CdkExample'; const serviceName = process.env.EXPECTED_SERVICE_NAME ?? 'MyFunctionWithStandardHandler'; const metricName = process.env.EXPECTED_METRIC_NAME ?? 'MyMetric'; const metricUnit = (process.env.EXPECTED_METRIC_UNIT as MetricUnits) ?? MetricUnits.Count; diff --git a/packages/metrics/tests/e2e/standardFunctions.test.MyFunction.ts b/packages/metrics/tests/e2e/standardFunctions.test.MyFunction.ts index ec26dbb898..997b5775d9 100644 --- a/packages/metrics/tests/e2e/standardFunctions.test.MyFunction.ts +++ b/packages/metrics/tests/e2e/standardFunctions.test.MyFunction.ts @@ -1,7 +1,7 @@ import { Metrics, MetricUnits } from '../../src'; import { Context } from 'aws-lambda'; -const namespace = process.env.EXPECTED_NAMESPACE ?? 'LambdaPowertoolsTypeScript-CDKExample'; +const namespace = process.env.EXPECTED_NAMESPACE ?? 'CdkExample'; const serviceName = process.env.EXPECTED_SERVICE_NAME ?? 'MyFunctionWithStandardHandler'; const metricName = process.env.EXPECTED_METRIC_NAME ?? 'MyMetric'; const metricUnit = (process.env.EXPECTED_METRIC_UNIT as MetricUnits) ?? MetricUnits.Count; From 5bf828e5629a768fff021bbc1818bc3c8c8ead62 Mon Sep 17 00:00:00 2001 From: Alex Maragakis Date: Mon, 17 Jan 2022 12:04:46 +0000 Subject: [PATCH 3/3] Addres PR comments #2 --- examples/cdk/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/cdk/README.md b/examples/cdk/README.md index cc7da9abc9..c6bcbbbe3c 100644 --- a/examples/cdk/README.md +++ b/examples/cdk/README.md @@ -2,7 +2,7 @@ This is a deployable CDK app that deploys AWS Lambda functions as part of a CloudFormation stack. These Lambda functions use the utilities made available as part of AWS Lambda Powertools (TypeScript) to demonstrate their usage. -You will need to have a valid AWS Account in order to deploy these resources. These resources may incur costs to your AWS Account. +You will need to have a valid AWS Account in order to deploy these resources. These resources may incur costs to your AWS Account. The cost from **some services** are covered by the [AWS Free Tier](https://aws.amazon.com/free/?all-free-tier.sort-by=item.additionalFields.SortRank&all-free-tier.sort-order=asc&awsf.Free%20Tier%20Types=*all&awsf.Free%20Tier%20Categories=*all) but not all of them. If you don't have an AWS Account follow [these instructions to create one](https://aws.amazon.com/premiumsupport/knowledge-center/create-and-activate-aws-account/). The example functions, located in the `lib` folder, are invoked automatically, twice, when deployed using the CDK construct defined in `lib/example-function.ts`. The first invocation demonstrates the effect on logs/metrics/annotations when the Lambda function has a cold start, and the latter without a cold start.