Skip to content

Commit df65b1f

Browse files
committed
Auto lint and build examples
1 parent 4cdaaea commit df65b1f

24 files changed

+9627
-17233
lines changed

Diff for: CONTRIBUTING.md

+24
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,30 @@ You can run the end-to-end tests automatically on your forked project by followi
147147
4. In the run-e2e-tests workflow page, select "Run workflow" and run it on the desired branch.
148148

149149
> :warning: **Don't automatically run end-to-end tests on branch push or PRs**. A malicious attacker can submit a pull request to attack your AWS account. Ideally, use a blank account without any important workload/data, and limit `AWS_ROLE_ARN_TO_ASSUME` permission to least minimum privilege.
150+
151+
152+
### Examples
153+
154+
As part of the repo you will find an examples folder at the root. This folder contains examples (written with CDK for now) of deployable AWS Lambda functions using Powertools.
155+
156+
To test your updates with these examples you just have to:
157+
158+
1. Build your local version of *aws-lambda-powertools-typescript* npm packages with `npm run lerna-package`
159+
1. Update their references in examples
160+
```
161+
cd examples/cdk
162+
npm install ../../packages/**/dist/aws-lambda-powertools-*
163+
```
164+
1. Run cdk tests
165+
```
166+
npm run test
167+
```
168+
1. Deploy
169+
```
170+
npm run cdk deploy
171+
```
172+
173+
Previous command will deploy AWS resources therefore you will need an AWS account and it might incur in some costs which should be 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). 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/).
150174
### Conventions
151175
152176
Category | Convention

Diff for: examples/cdk/bin/cdk-app.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env node
22
import 'source-map-support/register';
33
import * as cdk from 'aws-cdk-lib';
4-
import { CdkAppStack } from '../lib/example-stack';
4+
import { CdkAppStack } from '../src/example-stack';
55

66
const app = new cdk.App();
77
new CdkAppStack(app, 'CdkAppStack', {});

Diff for: examples/cdk/jest.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module.exports = {
22
testEnvironment: 'node',
3-
roots: ['<rootDir>/test'],
3+
roots: ['<rootDir>/tests'],
44
testMatch: ['**/*.test.ts'],
55
transform: {
66
'^.+\\.tsx?$': 'ts-jest'

Diff for: examples/cdk/package-lock.json

+6,806-17,177
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: examples/cdk/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
{
22
"name": "cdk-app",
3-
"version": "0.1.0",
3+
"version": "0.2.0",
44
"bin": {
55
"cdk-app": "bin/cdk-app.js"
66
},
77
"scripts": {
88
"build": "tsc --skipLibCheck",
99
"watch": "tsc -w",
10-
"test": "jest",
10+
"test": "npm run build && jest",
1111
"cdk": "cdk"
1212
},
1313
"devDependencies": {

Diff for: examples/cdk/lib/example-function.MyFunctionWithMiddy.ts renamed to examples/cdk/src/example-function.MyFunctionWithMiddy.ts

+12-10
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const metrics = new Metrics({ namespace: namespace, serviceName: serviceName });
1212
const logger = new Logger({ logLevel: 'INFO', serviceName: serviceName });
1313
const tracer = new Tracer({ serviceName: serviceName });
1414

15-
const lambdaHandler = async (event: typeof Events.Custom.CustomEvent, context: Context) => {
15+
const lambdaHandler = async (event: typeof Events.Custom.CustomEvent, context: Context): Promise<unknown> => {
1616
// ### Experiment with Logger
1717
// AWS Lambda context is automatically injected by the middleware
1818

@@ -32,7 +32,7 @@ const lambdaHandler = async (event: typeof Events.Custom.CustomEvent, context: C
3232
const metricWithItsOwnDimensions = metrics.singleMetric();
3333
metricWithItsOwnDimensions.addDimension('InnerDimension', 'true');
3434
metricWithItsOwnDimensions.addMetric('single-metric', MetricUnits.Percent, 50);
35-
35+
3636
// ### Experiment with Tracer
3737

3838
// Service & Cold Start annotations will be added for you by the decorator/middleware
@@ -58,16 +58,18 @@ const lambdaHandler = async (event: typeof Events.Custom.CustomEvent, context: C
5858
tracer.setSegment(handlerSegment);
5959
// The main segment (facade) will be closed for you at the end by the decorator/middleware
6060
}
61-
61+
6262
return res;
63-
}
63+
};
6464

6565
// We instrument the handler with the various Middy middlewares
6666
export const handler = middy(lambdaHandler)
6767
.use(captureLambdaHandler(tracer))
68-
.use(logMetrics(metrics, {
69-
captureColdStartMetric: true,
70-
throwOnEmptyMetrics: true,
71-
defaultDimensions: { environment: 'example', type: 'withDecorator' },
72-
}))
73-
.use(injectLambdaContext(logger));
68+
.use(
69+
logMetrics(metrics, {
70+
captureColdStartMetric: true,
71+
throwOnEmptyMetrics: true,
72+
defaultDimensions: { environment: 'example', type: 'withDecorator' },
73+
})
74+
)
75+
.use(injectLambdaContext(logger));

Diff for: examples/cdk/lib/example-function.Tracer.Disabled.ts renamed to examples/cdk/src/example-function.Tracer.Disabled.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@ import { captureLambdaHandler, Tracer } from '@aws-lambda-powertools/tracer';
77
const tracer = new Tracer({ serviceName: 'tracerDisabledFn', enabled: false });
88

99
// In this example we are using the middleware pattern but the same applies also the captureLambdaHandler decorator or to manual instrumentation
10-
const lambdaHandler = async (event: typeof Events.Custom.CustomEvent, context: Context) => {
10+
const lambdaHandler = async (event: typeof Events.Custom.CustomEvent, context: Context): Promise<unknown> => {
1111
// No tracing will be done and the commands will be ignored, this is useful for testing
1212
tracer.putAnnotation('awsRequestId', context.awsRequestId);
1313
tracer.putMetadata('eventPayload', event);
14-
14+
1515
let res;
1616
try {
1717
res = { foo: 'bar' };
1818
} catch (err) {
1919
throw err;
2020
}
21-
21+
2222
return res;
23-
}
23+
};
2424

25-
export const handler = middy(lambdaHandler).use(captureLambdaHandler(tracer));
25+
export const handler = middy(lambdaHandler).use(captureLambdaHandler(tracer));

Diff for: examples/cdk/lib/example-function.Tracer.Middleware.ts renamed to examples/cdk/src/example-function.Tracer.Middleware.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,24 @@ const tracer = new Tracer({ serviceName: 'tracerMiddlewareFn' });
77
// Alternatively, you can also set the service name using the POWERTOOLS_SERVICE_NAME environment variable
88
// Learn more at: https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html
99

10-
const lambdaHandler = async (event: typeof Events.Custom.CustomEvent, context: Context) => {
10+
const lambdaHandler = async (event: typeof Events.Custom.CustomEvent, context: Context): Promise<unknown> => {
1111
// Add custom annotation & metadata
1212
tracer.putAnnotation('awsRequestId', context.awsRequestId);
1313
tracer.putMetadata('eventPayload', event);
14-
14+
1515
let res;
1616
try {
1717
res = { foo: 'bar' };
1818
} catch (err) {
1919
throw err;
2020
}
21-
21+
2222
return res;
23-
}
23+
};
2424

2525
// We instrument the handler with the Middy middleware and the Tracer will automatically:
2626
// * handle the lifecycle of the subsegment
2727
// * create a ColdStart annotation to easily filter traces that have had an initialization overhead
2828
// * create a Service annotation to easily filter traces that have a specific service name
2929
// * captures any response, or full exceptions generated by the handler, and include them as tracing metadata
30-
export const handler = middy(lambdaHandler).use(captureLambdaHandler(tracer));
30+
export const handler = middy(lambdaHandler).use(captureLambdaHandler(tracer));

Diff for: examples/cdk/lib/example-function.ts renamed to examples/cdk/src/example-function.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ interface ExampleFunctionProps {
1212
}
1313

1414
class ExampleFunction extends Construct {
15-
public function: NodejsFunction;
1615

1716
public constructor(scope: Construct, id: string, props: ExampleFunctionProps) {
1817
super(scope, id);
@@ -32,7 +31,7 @@ class ExampleFunction extends Construct {
3231
onUpdate: {
3332
service: 'Lambda',
3433
action: 'invoke',
35-
physicalResourceId: custom_resources.PhysicalResourceId.of(new Date().toISOString()),
34+
physicalResourceId: custom_resources.PhysicalResourceId.of(`${functionName}-${i}`),
3635
parameters: {
3736
FunctionName: fn.functionName,
3837
InvocationType: 'RequestResponse',
File renamed without changes.

Diff for: examples/cdk/test/cdk-app.test.ts

-7
This file was deleted.

0 commit comments

Comments
 (0)