| description | Guidelines for writing, reviewing, and maintaining AWS CDK (TypeScript) code in the cdk package |
|---|---|
| applyTo | packages/cdk/**/*.ts |
This file is mastered in https://github.com/NHSDigital/eps-copilot-instructions and is automatically synced to all EPS repositories. To suggest changes, please open an issue or pull request in the eps-copilot-instructions repository.
This file provides instructions for generating, reviewing, and maintaining AWS CDK code in the packages/cdk folder. It covers best practices, code standards, architecture, and validation for infrastructure-as-code using AWS CDK in TypeScript.
- Use AWS CDK v2 constructs and idioms
- Prefer high-level CDK constructs over raw CloudFormation resources
- Organize code by logical infrastructure components (e.g., stacks, constructs, resources)
- Document public APIs and exported constructs
- Use environment variables and context for configuration, not hardcoded values
- Use CDK Aspects for cross-cutting concerns (e.g., security, tagging)
- Suppress warnings with
nagSuppressions.tsonly when justified and documented - Use
bin/for entrypoint apps,constructs/for reusable components, andstacks/for stack definitions - Prefer
propsinterfaces for construct configuration
- Classes: PascalCase (e.g.,
LambdaFunction) - Files: PascalCase for classes, kebab-case for utility files
- Variables: camelCase
- Stacks: Suffix with
Stack(e.g.,CptsApiAppStack) - Entry points: Suffix with
App(e.g.,CptsApiApp.ts)
bin/: CDK app entry pointsconstructs/: Custom CDK constructsstacks/: Stack definitionsresources/: Resource configuration and constantslib/: Shared utilities and code
export class LambdaFunction extends Construct {
constructor(scope: Construct, id: string, props: LambdaFunctionProps) {
super(scope, id);
// ...implementation...
}
}const lambda = new cdk.CfnResource(this, 'Lambda', {
type: 'AWS::Lambda::Function',
// ...properties...
});export class CptsApiAppStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);
// ...add constructs...
}
}- Use least privilege IAM policies for all resources
- Avoid wildcard permissions in IAM statements
- Store secrets in AWS Secrets Manager, not in code or environment variables
- Enable encryption for all data storage resources
- Use provisioned concurrency for Lambda functions when needed
- Prefer VPC endpoints for private connectivity
- Minimize resource creation in test environments
- Build:
make cdk-synth - Lint:
npm run lint --workspace packages/cdk
- Update dependencies regularly
- Remove deprecated constructs and suppressions
- Document changes in
nagSuppressions.tswith reasons