Skip to content

Commit

Permalink
feat: restrict props with type checking (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattzcarey committed Jun 6, 2023
1 parent 2ed1494 commit 38573da
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 15 deletions.
10 changes: 4 additions & 6 deletions constructs/Lambda/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ import { Duration } from 'aws-cdk-lib';
import { Architecture, Runtime, Tracing } from 'aws-cdk-lib/aws-lambda';
import { NodejsFunctionProps } from 'aws-cdk-lib/aws-lambda-nodejs';

import { AllowedLambdaConfig } from './types';

export const allowedConfig: AllowedLambdaConfig = {
export const allowedConfig = {
runtime: [Runtime.NODEJS_18_X],
architecture: Object.values(Architecture) as Architecture[],
tracing: Object.values(Tracing) as Tracing[],
retryAttempts: [0, 1, 2],
architecture: [Architecture.ARM_64, Architecture.X86_64],
tracing: [Tracing.ACTIVE] as const,
retryAttempts: [0, 1, 2] as const,
};

export const requiredConfig: Partial<NodejsFunctionProps> = {
Expand Down
3 changes: 2 additions & 1 deletion constructs/Lambda/lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ import {
errorMessages,
LambdaConfigurationError,
} from './errors';
import { AleiosLambdaProps } from './types';

export class AleiosLambda extends NodejsFunction {
constructor(scope: Construct, id: string, props?: NodejsFunctionProps) {
constructor(scope: Construct, id: string, props: AleiosLambdaProps) {
checkRequiredProperties(
props,
requiredConfig,
Expand Down
26 changes: 20 additions & 6 deletions constructs/Lambda/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
import { Architecture, Runtime, Tracing } from 'aws-cdk-lib/aws-lambda';
import { NodejsFunctionProps } from 'aws-cdk-lib/aws-lambda-nodejs';

export interface AllowedLambdaConfig {
runtime: Runtime[];
architecture: Architecture[];
tracing: Tracing[];
retryAttempts: number[];
import { allowedConfig } from './config';

export interface RestrictedLambdaConfig {
retryAttempts: typeof allowedConfig.retryAttempts[number];
tracing: typeof allowedConfig.tracing[number];
runtime: typeof allowedConfig.runtime[number]; //currently this doesnt restrict the runtime to nodejs
architecture: typeof allowedConfig.architecture[number]; //currently this doesnt restrict the architecture
}

export interface RequiredLambdaConfig extends Partial<NodejsFunctionProps> {
functionName: string;
entry: string;
}

export interface AleiosLambdaProps extends RequiredLambdaConfig {
runtime?: RestrictedLambdaConfig['runtime'];
architecture?: RestrictedLambdaConfig['architecture'];
tracing?: RestrictedLambdaConfig['tracing'];
retryAttempts?: RestrictedLambdaConfig['retryAttempts'];
}
7 changes: 5 additions & 2 deletions functions/helloWorld/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Runtime } from 'aws-cdk-lib/aws-lambda';
import { Architecture, Runtime, Tracing } from 'aws-cdk-lib/aws-lambda';
import { Construct } from 'constructs';
import { join } from 'path';

Expand All @@ -10,8 +10,11 @@ export class HelloWorld extends Construct {

new AleiosLambda(this, 'HelloWorldLambda', {
functionName: 'hello-world',
architecture: Architecture.ARM_64,
tracing: Tracing.PASS_THROUGH,
entry: join(__dirname, 'handler.ts'),
runtime: Runtime.NODEJS_14_X,
runtime: Runtime.NODEJS_16_X,
retryAttempts: 20,
});
}
}

0 comments on commit 38573da

Please sign in to comment.