Skip to content

Commit fd3781a

Browse files
author
Florian Chazal
committed
Add CDK example
1 parent 91ce691 commit fd3781a

File tree

4 files changed

+43
-83
lines changed

4 files changed

+43
-83
lines changed

Diff for: examples/cdk/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ node_modules
66
# CDK asset staging directory
77
.cdk.staging
88
cdk.out
9+
lib

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

+36-17
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,54 @@
1-
import { custom_resources, aws_iam } from 'aws-cdk-lib';
1+
import { custom_resources, aws_iam, Stack } from 'aws-cdk-lib';
22
import { Events } from '@aws-lambda-powertools/commons';
33
import { Construct } from 'constructs';
44
import { NodejsFunction, NodejsFunctionProps } from 'aws-cdk-lib/aws-lambda-nodejs';
5-
import { Tracing, Runtime } from 'aws-cdk-lib/aws-lambda';
5+
import { Tracing, Runtime, LayerVersion } from 'aws-cdk-lib/aws-lambda';
66

77
interface ExampleFunctionProps {
88
readonly functionName: string
99
readonly tracingActive?: boolean
10+
readonly useLayer?: boolean
1011
readonly invocations?: number
1112
readonly fnProps?: Partial<NodejsFunctionProps>
1213
}
1314

1415
class ExampleFunction extends Construct {
15-
1616
public constructor(scope: Construct, id: string, props: ExampleFunctionProps) {
1717
super(scope, id);
1818

19-
const { functionName, tracingActive, invocations, fnProps } = Object.assign({
20-
tracingActive: false,
21-
invocations: 2
22-
}, props);
19+
const { functionName, tracingActive, invocations, fnProps } = Object.assign(
20+
{
21+
tracingActive: false,
22+
invocations: 2,
23+
},
24+
props
25+
);
2326

24-
const fn = new NodejsFunction(this, functionName, {
27+
const fnOptions = {
2528
tracing: tracingActive ? Tracing.ACTIVE : Tracing.DISABLED,
2629
runtime: Runtime.NODEJS_16_X,
27-
...fnProps
28-
});
30+
...fnProps,
31+
};
32+
33+
if (props.useLayer) {
34+
fnOptions.bundling = {
35+
externalModules: [
36+
'@aws-lambda-powertools/commons',
37+
'@aws-lambda-powertools/logger',
38+
'@aws-lambda-powertools/metrics',
39+
'@aws-lambda-powertools/tracer',
40+
],
41+
};
42+
fnOptions.layers = [
43+
LayerVersion.fromLayerVersionArn(
44+
this,
45+
'AWSLambdaPowertoolsTypeScript',
46+
`arn:aws:lambda:${Stack.of(this).region}:094274105915:layer:AWSLambdaPowertoolsTypeScript:1`
47+
),
48+
];
49+
}
50+
51+
const fn = new NodejsFunction(this, functionName, fnOptions);
2952

3053
for (let i = 0; i < invocations; i++) {
3154
new custom_resources.AwsCustomResource(this, `Invoke-${functionName}-${i}`, {
@@ -37,14 +60,12 @@ class ExampleFunction extends Construct {
3760
FunctionName: fn.functionName,
3861
InvocationType: 'RequestResponse',
3962
Payload: JSON.stringify(Events.Custom.CustomEvent),
40-
}
63+
},
4164
},
4265
policy: custom_resources.AwsCustomResourcePolicy.fromStatements([
4366
new aws_iam.PolicyStatement({
4467
effect: aws_iam.Effect.ALLOW,
45-
resources: [
46-
fn.functionArn,
47-
],
68+
resources: [fn.functionArn],
4869
actions: ['lambda:InvokeFunction'],
4970
}),
5071
]),
@@ -53,6 +74,4 @@ class ExampleFunction extends Construct {
5374
}
5475
}
5576

56-
export {
57-
ExampleFunction
58-
};
77+
export { ExampleFunction };

Diff for: examples/cdk/src/example-layer.ts

-64
This file was deleted.

Diff for: examples/cdk/src/example-stack.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Stack, StackProps } from 'aws-cdk-lib';
22
import { Construct } from 'constructs';
3-
import { LayeredFunction } from './example-layer';
3+
import path from 'path';
44
import { ExampleFunction } from './example-function';
55

66
export class CdkAppStack extends Stack {
@@ -67,9 +67,13 @@ export class CdkAppStack extends Stack {
6767
tracingActive: true,
6868
});
6969

70-
new LayeredFunction(this, 'MyLayeredFunction', {
70+
new ExampleFunction(this, 'MyLayeredFunction', {
7171
functionName: 'MyLayeredFunction',
7272
tracingActive: true,
73+
useLayer: true,
74+
fnProps: {
75+
entry: path.join(__dirname, 'example-function.MyFunction.ts')
76+
}
7377
});
7478
}
7579
}

0 commit comments

Comments
 (0)