AWS CDK TypeScript Naming Conventions

AWS CDK TypeScript Naming Conventions

File Names

  • Stack Files: PascalCase with Stack suffix

    AuthStack.ts
    ApiGatewayStack.ts
    
  • Lambda Files: camelCase or kebab-case

    athleteStats.ts
    auth-handler.ts
    

Class Names

  • Stack Classes: PascalCase with Stack suffix

    export class AuthStack extends Stack
    export class UiDeploymentStack extends Stack
    
  • Construct Classes: PascalCase

    export class ApiGatewayConstruct extends Construct
    

Method Names

  • Lambda Handlers: camelCase

    export async function handler(event, context)
    
  • Stack Methods: camelCase

    private createLambdaFunction()
    private addDynamoTable()
    

Variable Names

  • Resource Instances: camelCase

    const authFunction = new Function(...)
    const apiGateway = new RestApi(...)
    
  • Environment Variables: SCREAMING_SNAKE_CASE

    STRAVA_CLIENT_ID
    DYNAMODB_TABLE_NAME
    

Construct IDs

  • Logical IDs: PascalCase

    new Function(this, 'AuthHandler', ...)
    new Table(this, 'AthletesTable', ...)
    

Interface Names

  • Props Interfaces: PascalCase with Props suffix

    interface AuthStackProps extends StackProps
    interface ApiGatewayConstructProps
    
  • Resource Interfaces: PascalCase with descriptive suffix

    interface StravaConfig
    interface AuthorizerContext
    

Constants

  • Configuration Constants: SCREAMING_SNAKE_CASE

    const MAX_RETRIES = 3
    const DEFAULT_REGION = 'eu-central-1'
    
  • Resource Constants: camelCase

    const tableName = 'athletes'
    const apiPath = '/api/v1'
    

Best Practices

  1. Use meaningful and descriptive names
  2. Avoid abbreviations unless widely known
  3. Be consistent with AWS CDK naming patterns
  4. Group related resources with common prefixes
  5. Include purpose or function in resource names

Examples from Real Code

// Stack definition
export class StravaApiStack extends Stack {
    constructor(scope: Construct, id: string, props?: StackProps) {
        super(scope, id, props);

        // Resource naming
        const athletesTable = new Table(this, 'AthletesTable', {
            partitionKey: { 
                name: 'userId', 
                type: AttributeType.STRING 
            }
        });

        const authHandler = new Function(this, 'AuthFunction', {
            runtime: Runtime.NODEJS_18_X,
            handler: 'index.handler',
            code: Code.fromAsset('src/lambda/auth')
        });
    }
}