@aligent/nx-cdk 0.0.1 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/README.md +4 -4
  2. package/generators.json +1 -1
  3. package/package.json +1 -1
  4. package/src/generators/helpers/configs/nxJson.js +7 -17
  5. package/src/generators/helpers/configs/packageJson.d.ts +27 -5
  6. package/src/generators/helpers/configs/packageJson.js +22 -5
  7. package/src/generators/helpers/configs/tsConfigs.js +3 -3
  8. package/src/generators/helpers/utilities.d.ts +24 -3
  9. package/src/generators/helpers/utilities.js +7 -4
  10. package/src/generators/preset/files/.github/workflows/pull-request.yml.template +11 -0
  11. package/src/generators/preset/files/.github/workflows/release.yml.template +17 -0
  12. package/src/generators/preset/files/.github/workflows/stg-deployment.yml.template +17 -0
  13. package/src/generators/preset/files/.gitignore.template +5 -1
  14. package/src/generators/preset/files/README.md.template +17 -2
  15. package/src/generators/preset/files/application/README.md.template +0 -17
  16. package/src/generators/preset/files/application/_internal/log-group-defaults-aspect.ts.template +83 -0
  17. package/src/generators/preset/files/application/_internal/nodejs-function-defaults-aspect.ts.template +72 -0
  18. package/src/generators/preset/files/application/_internal/step-function-defaults-aspect.ts.template +83 -0
  19. package/src/generators/preset/files/application/bin/main.ts.template +34 -38
  20. package/src/generators/preset/files/application/cdk.json.template +1 -1
  21. package/src/generators/preset/files/application/lib/service-stacks.ts.template +3 -0
  22. package/src/generators/preset/files/application/package.json.template +0 -6
  23. package/src/generators/preset/files/eslint.config.mjs.template +1 -2
  24. package/src/generators/preset/files/libs/infra/README.md.template +61 -0
  25. package/src/generators/preset/files/libs/infra/eslint.config.mjs.template +20 -0
  26. package/src/generators/preset/files/libs/infra/package.json.template +19 -0
  27. package/src/generators/preset/files/libs/infra/src/index.ts.template +44 -0
  28. package/src/generators/preset/files/libs/infra/tsconfig.json.template +6 -0
  29. package/src/generators/preset/files/parameters/dev.csv.template +2 -0
  30. package/src/generators/preset/files/tsconfig.json.template +1 -1
  31. package/src/generators/preset/preset.js +2 -4
  32. package/src/generators/service/files/README.md.template +2 -2
  33. package/src/generators/service/files/package.json.template +3 -2
  34. package/src/generators/service/files/src/index.ts.template +2 -1
  35. package/src/generators/service/files/vitest.config.mjs.template +1 -1
  36. package/src/generators/preset/files/.github/workflows/ci-cd.yml.template +0 -27
  37. package/src/generators/preset/files/application/_internal/log-group-defaults-injector.ts.template +0 -100
  38. package/src/generators/preset/files/application/_internal/microservice-checks.ts.template +0 -58
  39. package/src/generators/preset/files/application/_internal/nodejs-function-defaults-injector.ts.template +0 -110
  40. package/src/generators/preset/files/application/_internal/step-function-defaults-injector.ts.template +0 -126
  41. package/src/generators/preset/files/application/_internal/version-functions-aspect.ts.template +0 -74
  42. package/src/generators/preset/files/cdk-config.yml.template +0 -16
@@ -1,58 +0,0 @@
1
- import { CfnResource } from 'aws-cdk-lib';
2
- import { NagMessageLevel, NagPack, rules, type NagPackProps } from 'cdk-nag';
3
- import type { IConstruct } from 'constructs';
4
-
5
- /**
6
- * Microservice Checks are a compilation of rules to validate infrastructure-as-code template
7
- * against recommended practices using the cdk-nag library.
8
- *
9
- * @see https://github.com/cdk-patterns/cdk-nag/
10
- *
11
- * @example
12
- * const app = new App();
13
- * const stack = new Stack(app, 'MyStack');
14
- * Aspects.of(stack).add(new MicroservicesChecks());
15
- */
16
- export class MicroserviceChecks extends NagPack {
17
- constructor(props?: NagPackProps) {
18
- super(props);
19
- this.packName = 'Microservices';
20
- }
21
-
22
- public visit(node: IConstruct) {
23
- if (node instanceof CfnResource) {
24
- this.applyRule({
25
- info: 'The Lambda function does not have an explicit memory value configured.',
26
- explanation:
27
- "Lambda allocates CPU power in proportion to the amount of memory configured. By default, your functions have 128 MB of memory allocated. You can increase that value up to 10 GB. With more CPU resources, your Lambda function's duration might decrease. You can use tools such as AWS Lambda Power Tuning to test your function at different memory settings to find the one that matches your cost and performance requirements the best.",
28
- level: NagMessageLevel.ERROR,
29
- rule: rules.lambda.LambdaDefaultMemorySize,
30
- node: node,
31
- });
32
- this.applyRule({
33
- info: 'The Lambda function does not have an explicitly defined timeout value.',
34
- explanation:
35
- 'Lambda functions have a default timeout of 3 seconds. If your timeout value is too short, Lambda might terminate invocations prematurely. On the other side, setting the timeout much higher than the average execution may cause functions to execute for longer upon code malfunction, resulting in higher costs and possibly reaching concurrency limits depending on how such functions are invoked. You can also use AWS Lambda Power Tuning to test your function at different timeout settings to find the one that matches your cost and performance requirements the best.',
36
- level: NagMessageLevel.ERROR,
37
- rule: rules.lambda.LambdaDefaultTimeout,
38
- node: node,
39
- });
40
- this.applyRule({
41
- info: 'The Lambda function does not have tracing set to Tracing.ACTIVE.',
42
- explanation:
43
- 'When a Lambda function has ACTIVE tracing, Lambda automatically samples invocation requests, based on the sampling algorithm specified by X-Ray.',
44
- level: NagMessageLevel.ERROR,
45
- rule: rules.lambda.LambdaTracing,
46
- node: node,
47
- });
48
- this.applyRule({
49
- info: 'The CloudWatch Log Group does not have an explicit retention policy defined.',
50
- explanation:
51
- 'By default, logs are kept indefinitely and never expire. You can adjust the retention policy for each log group, keeping the indefinite retention, or choosing a retention period between one day and 10 years. For Lambda functions, this applies to their automatically created CloudWatch Log Groups.',
52
- level: NagMessageLevel.ERROR,
53
- rule: rules.cloudwatch.CloudWatchLogGroupRetentionPeriod,
54
- node: node,
55
- });
56
- }
57
- }
58
- }
@@ -1,110 +0,0 @@
1
- import { type IPropertyInjector } from 'aws-cdk-lib';
2
- import { Runtime, Tracing } from 'aws-cdk-lib/aws-lambda';
3
- import { NodejsFunction, type NodejsFunctionProps } from 'aws-cdk-lib/aws-lambda-nodejs';
4
-
5
- interface Config {
6
- runtime: Runtime;
7
- sourceMap?: boolean;
8
- }
9
-
10
- /**
11
- * Property injector for Node.js Lambda functions with configuration-aware defaults
12
- *
13
- * Applies configuration-specific bundling and runtime settings to Lambda functions.
14
- * Different configurations can optimize for different priorities such as build speed,
15
- * bundle size, or debugging capabilities.
16
- *
17
- * @example
18
- * ```typescript
19
- * // Apply configuration-specific defaults
20
- * PropertyInjectors.of(scope).add(
21
- * new NodeJsFunctionDefaultsInjector({
22
- * sourceMaps: true,
23
- * esm: true,
24
- * minify: true,
25
- * }).withProps({
26
- * timeout: Duration.seconds(30),
27
- * memorySize: 256,
28
- * })
29
- * );
30
- *
31
- * // Functions automatically inherit configuration defaults
32
- * new Function(stack, 'MyFunction', {
33
- * code: Code.fromAsset('src/lambda'),
34
- * handler: 'index.handler',
35
- * // bundling and runtime config applied automatically
36
- * });
37
- * ```
38
- *
39
- * @see https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_lambda_nodejs.NodejsFunction.html
40
- */
41
- export class NodeJsFunctionDefaultsInjector implements IPropertyInjector {
42
- public readonly constructUniqueId = NodejsFunction.PROPERTY_INJECTION_ID;
43
- private readonly config: Required<Config>;
44
- private defaultProps: NodejsFunctionProps;
45
-
46
- /**
47
- * Creates a new NodeJsFunctionDefaultsInjector
48
- *
49
- * @param config - Configuration identifier used to select appropriate defaults. Uses production defaults if not specified.
50
- */
51
- constructor(config: Config) {
52
- this.config = { ...config, sourceMap: config.sourceMap || true };
53
- this.defaultProps = { runtime: config.runtime, tracing: Tracing.ACTIVE };
54
- }
55
-
56
- /**
57
- * Creates a new injector instance with additional properties
58
- *
59
- * Returns a new injector that inherits the current configuration but includes
60
- * additional properties that override the configuration defaults.
61
- *
62
- * @param props - Additional properties to merge with configuration defaults
63
- * @returns A new injector instance with merged properties
64
- *
65
- * @example
66
- * ```typescript
67
- * const customInjector = new NodeJsFunctionDefaultsInjector({
68
- * sourceMaps: false,
69
- * })
70
- * .withProps({
71
- * timeout: Duration.minutes(5),
72
- * memorySize: 1024,
73
- * });
74
- * ```
75
- *
76
- * TODO: Provide a nice way to inherit global properties from previous injectors
77
- */
78
- public withProps(props: NodejsFunctionProps) {
79
- const modifiedInjector = new NodeJsFunctionDefaultsInjector(this.config);
80
- modifiedInjector.defaultProps = { ...this.defaultProps, ...props };
81
- return modifiedInjector;
82
- }
83
-
84
- /**
85
- * Injects configuration-appropriate defaults into Lambda function properties
86
- *
87
- * Merges configuration-specific defaults with user-provided properties,
88
- * automatically configuring bundling options and runtime settings.
89
- *
90
- * @param originalProps - Properties provided when creating the function
91
- * @returns Merged properties with injected defaults
92
- */
93
- public inject(originalProps: NodejsFunctionProps) {
94
- // The NodeJsFunction constructor pre-sets runtime to 16.x or LATEST depending on feature flags
95
- // We assume that using this injector means you want to standardise the runtime across all lambdas
96
- const props = {
97
- ...this.defaultProps,
98
- ...originalProps,
99
- runtime: this.defaultProps.runtime,
100
- };
101
-
102
- // If source maps are enabled in our bundling, add the required NODE_OPTIONS flag to the environment
103
- const environment = {
104
- ...props.environment,
105
- ...(this.config.sourceMap ? { NODE_OPTIONS: '--enable-source-maps' } : {}),
106
- };
107
-
108
- return { ...props, environment };
109
- }
110
- }
@@ -1,126 +0,0 @@
1
- import { type InjectionContext, type IPropertyInjector } from 'aws-cdk-lib';
2
- import { LogGroup } from 'aws-cdk-lib/aws-logs';
3
- import {
4
- LogLevel,
5
- StateMachine,
6
- StateMachineType,
7
- type StateMachineProps,
8
- } from 'aws-cdk-lib/aws-stepfunctions';
9
- import type { Construct } from 'constructs';
10
-
11
- type StepFunctionDefaults = Omit<StateMachineProps, 'definition' | 'definitionSubstitutions'>;
12
-
13
- /**
14
- * Property injector for Step Functions with configuration-aware defaults
15
- *
16
- * Applies configuration-specific settings to Step Functions. All configurations enable
17
- * X-Ray tracing by default for observability. For EXPRESS state machines, automatically
18
- * creates log groups and configures comprehensive logging.
19
- *
20
- * @example
21
- * ```typescript
22
- * // Apply configuration-specific defaults
23
- * PropertyInjectors.of(scope).add(
24
- * new StepFunctionDefaultsInjector('dev').withProps({
25
- * timeout: Duration.minutes(30),
26
- * })
27
- * );
28
- *
29
- * // Step Functions automatically inherit defaults
30
- * new StateMachine(stack, 'MyWorkflow', {
31
- * stateMachineType: StateMachineType.EXPRESS,
32
- * definitionBody: DefinitionBody.fromFile('workflow.asl.yaml'),
33
- * // tracing enabled and logging configured automatically for EXPRESS
34
- * });
35
- * ```
36
- *
37
- * @see https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_stepfunctions.StateMachine.html
38
- */
39
- export class StepFunctionDefaultsInjector implements IPropertyInjector {
40
- public readonly constructUniqueId = StateMachine.PROPERTY_INJECTION_ID;
41
- private readonly config: Record<string, never>;
42
- private defaultProps: StateMachineProps;
43
-
44
- /**
45
- * Creates a new StepFunctionDefaultsInjector
46
- *
47
- * @param config - Configuration identifier (currently unused but maintained for consistency).
48
- * All configurations enable tracing by default.
49
- */
50
- constructor(config: Record<string, never> = {}) {
51
- this.config = { ...config };
52
- this.defaultProps = { tracingEnabled: true };
53
- }
54
-
55
- /**
56
- * Creates a new injector instance with additional properties
57
- *
58
- * Returns a new injector that inherits the current configuration but includes
59
- * additional properties that override the configuration defaults.
60
- *
61
- * @param props - Additional properties to merge with configuration defaults
62
- * @returns A new injector instance with merged properties
63
- *
64
- * @example
65
- * ```typescript
66
- * const customInjector = new StepFunctionDefaultsInjector('prod')
67
- * .withProps({
68
- * timeout: Duration.hours(1),
69
- * stateMachineName: 'custom-workflow',
70
- * });
71
- * ```
72
- */
73
- public withProps(props: StateMachineProps) {
74
- const modifiedInjector = new StepFunctionDefaultsInjector(this.config);
75
- modifiedInjector.defaultProps = { ...this.defaultProps, ...props };
76
- return modifiedInjector;
77
- }
78
-
79
- /**
80
- * Injects configuration-appropriate defaults into Step Function properties
81
- *
82
- * Enables X-Ray tracing for all state machines. For EXPRESS state machines,
83
- * automatically creates log groups and configures comprehensive logging.
84
- *
85
- * @param originalProps - Properties provided when creating the state machine
86
- * @param context - CDK injection context containing construct information
87
- * @returns Merged properties with injected defaults and logging configuration
88
- */
89
- public inject(originalProps: StateMachineProps, context: InjectionContext) {
90
- // Prepare logging configuration for EXPRESS step functions
91
- const logging = expressLogProperties(context.scope, context.id, originalProps);
92
- return { ...this.defaultProps, ...originalProps, ...logging };
93
- }
94
- }
95
-
96
- /**
97
- * Build the logging configuration for an express step function, respecting
98
- * existing logging configuration.
99
- *
100
- * @param machineScope - The scope of the step function.
101
- * @param machineId - The id of the step function.
102
- * @param props - The properties for the step function.
103
- * @returns The logging configuration for an express step function
104
- */
105
- function expressLogProperties(
106
- machineScope: Construct,
107
- machineId: string,
108
- props: StepFunctionDefaults
109
- ) {
110
- if (props.stateMachineType !== StateMachineType.EXPRESS) {
111
- return {};
112
- }
113
-
114
- // Create a new log group if one is not provided
115
- const logGroup =
116
- props.logs?.destination ?? new LogGroup(machineScope, `/aws/states/${machineId}`);
117
-
118
- return {
119
- logs: {
120
- destination: logGroup,
121
- level: LogLevel.ALL,
122
- includeExecutionData: true,
123
- ...props.logs,
124
- },
125
- };
126
- }
@@ -1,74 +0,0 @@
1
- import type { IAspect } from 'aws-cdk-lib';
2
- import { Function } from 'aws-cdk-lib/aws-lambda';
3
- import {
4
- CfnStateMachineAlias,
5
- CfnStateMachineVersion,
6
- StateMachine,
7
- } from 'aws-cdk-lib/aws-stepfunctions';
8
- import { IConstruct } from 'constructs';
9
-
10
- /**
11
- * Aspect that automatically adds versioning and aliases to Lambda and Step Functions
12
- *
13
- * Visits all constructs in the scope and automatically creates versions and aliases
14
- * for supported resource types. This enables blue-green deployments, traffic shifting,
15
- * and provides stable ARNs for external integrations.
16
- *
17
- * Currently supports:
18
- * - Lambda Functions: Creates function aliases
19
- * - Step Functions: Creates versions and aliases with 100% traffic routing
20
- *
21
- * @example
22
- * ```typescript
23
- * // Apply to entire app for automatic versioning
24
- * Aspects.of(app).add(new VersionFunctionsAspect());
25
- *
26
- * // Or with custom alias name
27
- * Aspects.of(app).add(new VersionFunctionsAspect({ alias: 'PROD' }));
28
- * ```
29
- */
30
- export class VersionFunctionsAspect implements IAspect {
31
- /**
32
- * Creates a new VersionFunctionsAspect
33
- *
34
- * @param props - Configuration for the aspect
35
- * @param props.alias - Name for the alias to create. Defaults to 'LATEST'
36
- */
37
- constructor(
38
- private readonly props: {
39
- alias: string;
40
- } = {
41
- alias: 'LATEST',
42
- }
43
- ) {}
44
-
45
- /**
46
- * Visits a construct and applies versioning if it's a supported resource type
47
- *
48
- * For Lambda Functions: Adds a function alias pointing to $LATEST
49
- * For Step Functions: Creates a version and alias with 100% traffic routing
50
- *
51
- * @param node - The construct to potentially add versioning to
52
- */
53
- visit(node: IConstruct): void {
54
- if (node instanceof StateMachine) {
55
- const version = new CfnStateMachineVersion(node, `Version`, {
56
- stateMachineArn: node.stateMachineArn,
57
- });
58
-
59
- new CfnStateMachineAlias(node, `Alias`, {
60
- name: this.props.alias,
61
- routingConfiguration: [
62
- {
63
- stateMachineVersionArn: version.attrArn,
64
- weight: 100,
65
- },
66
- ],
67
- });
68
- }
69
-
70
- if (node instanceof Function) {
71
- node.addAlias(this.props.alias);
72
- }
73
- }
74
- }
@@ -1,16 +0,0 @@
1
- cdk-pipe:
2
- commands:
3
- cdk:
4
- bootstrap: yarn nx run application:cdk bootstrap
5
- deploy: yarn nx run application:cdk deploy
6
- synth: yarn nx run application:cdk synth
7
- diff: yarn nx run application:cdk diff
8
- npm:
9
- checks:
10
- lint: yarn lint
11
- format: yarn lint # Formatting is controlled by the lint step, we turn this off in the pipeline anyway
12
- install: yarn install --immutable
13
- beforeScripts:
14
- - yarn --version
15
- afterScripts:
16
- - echo "Deployment is completed"