Skip to content

Commit 2e192df

Browse files
committed
add some documentation
1 parent 7f8ccac commit 2e192df

File tree

1 file changed

+80
-20
lines changed

1 file changed

+80
-20
lines changed

packages/cdkConstructs/src/constructs/TypescriptLambdaFunction.ts

Lines changed: 80 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,17 @@ import {NagSuppressions} from "cdk-nag"
2323

2424
export interface TypescriptLambdaFunctionProps {
2525
/**
26-
* Name of the Lambda function.
26+
* Name of the lambda function. The log group name is also based on this name.
2727
*
2828
*/
2929
readonly functionName: string
3030
/**
31-
* The relative path to the base of the package where the Lambda function code is located.
31+
* The base directory for resolving the package base path and entry point.
32+
* Should point to the monorepo root.
33+
*/
34+
readonly baseDir: string
35+
/**
36+
* The relative path from baseDir to the base folder where the lambda function code is located.
3237
*
3338
*/
3439
readonly packageBasePath: string
@@ -38,11 +43,11 @@ export interface TypescriptLambdaFunctionProps {
3843
*/
3944
readonly entryPoint: string
4045
/**
41-
* A map of environment variables to set for the Lambda function.
46+
* A map of environment variables to set for the lambda function.
4247
*/
4348
readonly environmentVariables: {[key: string]: string}
4449
/**
45-
* Optional additional IAM policies to attach to the Lambda function.
50+
* Optional additional IAM policies to attach to role the lambda executes as.
4651
*/
4752
readonly additionalPolicies?: Array<IManagedPolicy>
4853
/**
@@ -51,7 +56,7 @@ export interface TypescriptLambdaFunctionProps {
5156
*/
5257
readonly logRetentionInDays: number
5358
/**
54-
* The log level for the Lambda function.
59+
* The log level for the lambda function.
5560
* @default "INFO"
5661
*/
5762
readonly logLevel: string,
@@ -68,19 +73,22 @@ export interface TypescriptLambdaFunctionProps {
6873
*/
6974
readonly layers?: Array<ILayerVersion>
7075
/**
71-
* The base directory for resolving the package base path and entry point.
72-
* Should point to the monorepo root.
76+
* Optional timeout in seconds for the Lambda function.
77+
* @default 50 seconds
7378
*/
74-
readonly baseDir: string
79+
readonly timeoutInSeconds?: number
7580
}
7681

7782
const insightsLayerArn = "arn:aws:lambda:eu-west-2:580247275435:layer:LambdaInsightsExtension:60"
78-
const getDefaultLambdaOptions = (packageBasePath: string, baseDir: string):NodejsFunctionProps => {
83+
const getDefaultLambdaOptions = (
84+
packageBasePath: string,
85+
baseDir: string,
86+
timeoutInSeconds: number):NodejsFunctionProps => {
7987
return {
8088
runtime: Runtime.NODEJS_22_X,
8189
projectRoot: baseDir,
8290
memorySize: 256,
83-
timeout: Duration.seconds(50),
91+
timeout: Duration.seconds(timeoutInSeconds),
8492
architecture: Architecture.X86_64,
8593
handler: "handler",
8694
bundling: {
@@ -92,16 +100,71 @@ const getDefaultLambdaOptions = (packageBasePath: string, baseDir: string):Nodej
92100
}
93101
}
94102

103+
/**
104+
* A construct that creates a TypeScript-based AWS Lambda function with all necessary AWS resources.
105+
*
106+
* This construct creates:
107+
* - A Lambda function with TypeScript bundling
108+
* - CloudWatch log group with KMS encryption
109+
* - IAM role for execution with necessary permissions
110+
* - Subscription filter on logs so they are forwarded to splunk
111+
* - Lambda Insights layer for monitoring
112+
*
113+
* @example
114+
* ```typescript
115+
* const lambdaFunction = new TypescriptLambdaFunction(this, 'MyFunction', {
116+
* functionName: 'my-lambda',
117+
* packageBasePath: 'packages/my-lambda',
118+
* entryPoint: 'src/handler.ts',
119+
* environmentVariables: {
120+
* TABLE_NAME: 'my-table'
121+
* },
122+
* logRetentionInDays: 30,
123+
* logLevel: 'INFO',
124+
* version: '1.0.0',
125+
* commitId: 'abc123',
126+
* baseDir: '/path/to/monorepo'
127+
* });
128+
* ```
129+
*/
95130
export class TypescriptLambdaFunction extends Construct {
96131
/**
97-
* The managed policy that allows execution of the Lambda function.
98-
*/
132+
* The managed policy that allows execution of the Lambda function.
133+
*
134+
* Use this policy to grant other AWS resources permission to invoke this Lambda function.
135+
*
136+
* @example
137+
* ```typescript
138+
* // Grant API Gateway permission to invoke the Lambda
139+
* apiGatewayRole.addManagedPolicy(lambdaConstruct.executionPolicy);
140+
* ```
141+
*/
99142
public readonly executionPolicy: ManagedPolicy
143+
100144
/**
101-
* The Lambda function instance.
102-
*/
145+
* The Lambda function instance.
146+
*
147+
* Provides access to the underlying AWS Lambda function for additional configuration
148+
* or to reference its ARN, name, or other properties.
149+
*
150+
* @example
151+
* ```typescript
152+
* // Get the function ARN
153+
* const functionArn = lambdaConstruct.function.functionArn;
154+
*
155+
* // Add additional environment variables
156+
* lambdaConstruct.function.addEnvironment('NEW_VAR', 'value');
157+
* ```
158+
*/
103159
public readonly function: NodejsFunction
104160

161+
/**
162+
* Creates a new TypescriptLambdaFunction construct.
163+
*
164+
* @param scope - The scope in which to define this construct
165+
* @param id - The scoped construct ID. Must be unique amongst siblings in the same scope
166+
* @param props - Configuration properties for the Lambda function
167+
*/
105168
public constructor(scope: Construct, id: string, props: TypescriptLambdaFunctionProps){
106169
super(scope, id)
107170

@@ -117,7 +180,8 @@ export class TypescriptLambdaFunction extends Construct {
117180
version,
118181
commitId,
119182
layers = [], // Default to empty array
120-
baseDir
183+
baseDir,
184+
timeoutInSeconds = 50
121185
} = props
122186

123187
// Imports
@@ -136,9 +200,6 @@ export class TypescriptLambdaFunction extends Construct {
136200
const cloudwatchEncryptionKMSPolicy = ManagedPolicy.fromManagedPolicyArn(
137201
this, "cloudwatchEncryptionKMSPolicyArn", Fn.importValue("account-resources:CloudwatchEncryptionKMSPolicyArn"))
138202

139-
const lambdaDecryptSecretsKMSPolicy = ManagedPolicy.fromManagedPolicyArn(
140-
this, "lambdaDecryptSecretsKMSPolicy", Fn.importValue("account-resources:LambdaDecryptSecretsKMSPolicy"))
141-
142203
const insightsLambdaLayer = LayerVersion.fromLayerVersionArn(
143204
this, "LayerFromArn", insightsLayerArn)
144205

@@ -195,13 +256,12 @@ export class TypescriptLambdaFunction extends Construct {
195256
putLogsManagedPolicy,
196257
lambdaInsightsLogGroupPolicy,
197258
cloudwatchEncryptionKMSPolicy,
198-
lambdaDecryptSecretsKMSPolicy,
199259
...(additionalPolicies)
200260
]
201261
})
202262

203263
const lambdaFunction = new NodejsFunction(this, functionName, {
204-
...getDefaultLambdaOptions(packageBasePath, baseDir),
264+
...getDefaultLambdaOptions(packageBasePath, baseDir, timeoutInSeconds),
205265
functionName: `${functionName}`,
206266
entry: join(baseDir, packageBasePath, entryPoint),
207267
role,

0 commit comments

Comments
 (0)