@aligent/nx-cdk 0.2.0 → 0.3.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.
- package/package.json +2 -2
- package/src/generators/preset/files/application/bin/main.ts.template +9 -8
- package/src/generators/preset/files/application/_internal/log-group-defaults-aspect.ts.template +0 -83
- package/src/generators/preset/files/application/_internal/nodejs-function-defaults-aspect.ts.template +0 -72
- package/src/generators/preset/files/application/_internal/step-function-defaults-aspect.ts.template +0 -83
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aligent/nx-cdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"type": "commonjs",
|
|
5
5
|
"main": "./src/index.js",
|
|
6
6
|
"typings": "./src/index.d.ts",
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
"directory": "packages/nx-cdk"
|
|
11
11
|
},
|
|
12
12
|
"dependencies": {
|
|
13
|
-
"@nx/devkit": "22.4.
|
|
13
|
+
"@nx/devkit": "22.4.2",
|
|
14
14
|
"ts-morph": "^27.0.2"
|
|
15
15
|
},
|
|
16
16
|
"generators": "./generators.json",
|
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
LambdaAndStepFunctionVersioningAspect,
|
|
4
|
+
LogGroupDefaultsAspect,
|
|
5
|
+
MicroserviceChecks,
|
|
6
|
+
NodeJsFunctionDefaultsAspect,
|
|
7
|
+
StepFunctionsDefaultsAspect,
|
|
8
|
+
} from '@aligent/cdk-aspects';
|
|
3
9
|
import { App, Aspects, Tags } from 'aws-cdk-lib';
|
|
4
10
|
import { Runtime } from 'aws-cdk-lib/aws-lambda';
|
|
5
11
|
import { ApplicationStage } from '../lib/service-stacks.js';
|
|
6
12
|
|
|
7
|
-
// TODO: [MI-277] Move these aspects to @aligent/cdk-aspects package for reuse across projects
|
|
8
|
-
import { LogGroupDefaultsAspect } from '../_internal/log-group-defaults-aspect.js';
|
|
9
|
-
import { NodeJsFunctionDefaultsAspect } from '../_internal/nodejs-function-defaults-aspect.js';
|
|
10
|
-
import { StepFunctionDefaultsAspect } from '../_internal/step-function-defaults-aspect.js';
|
|
11
|
-
|
|
12
13
|
const APPLICATION_CONTEXT = { NAME: '<%= name %>-int', OWNER: 'aligent' } as const;
|
|
13
14
|
const STAGE_NAMES = { DEV: 'dev', STG: 'stg', PRD: 'prd' } as const;
|
|
14
15
|
|
|
@@ -20,7 +21,7 @@ Tags.of(app).add('OWNER', APPLICATION_CONTEXT.OWNER);
|
|
|
20
21
|
* These aspects are applied to all resources across all stages
|
|
21
22
|
*/
|
|
22
23
|
Aspects.of(app).add(new NodeJsFunctionDefaultsAspect({ runtime: Runtime.NODEJS_<%= nodeRuntime %> }));
|
|
23
|
-
Aspects.of(app).add(new
|
|
24
|
+
Aspects.of(app).add(new StepFunctionsDefaultsAspect());
|
|
24
25
|
Aspects.of(app).add(new MicroserviceChecks());
|
|
25
26
|
|
|
26
27
|
/**
|
|
@@ -65,4 +66,4 @@ Aspects.of(stg).add(new LogGroupDefaultsAspect({ duration: 'MEDIUM' }));
|
|
|
65
66
|
*/
|
|
66
67
|
const prd = new ApplicationStage(app, STAGE_NAMES.PRD);
|
|
67
68
|
Aspects.of(prd).add(new LogGroupDefaultsAspect({ duration: 'LONG' }));
|
|
68
|
-
Aspects.of(prd).add(new
|
|
69
|
+
Aspects.of(prd).add(new LambdaAndStepFunctionVersioningAspect());
|
package/src/generators/preset/files/application/_internal/log-group-defaults-aspect.ts.template
DELETED
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
import { RemovalPolicy, type IAspect } from 'aws-cdk-lib';
|
|
2
|
-
import { CfnLogGroup, LogGroup, RetentionDays, type LogGroupProps } from 'aws-cdk-lib/aws-logs';
|
|
3
|
-
import { IConstruct } from 'constructs';
|
|
4
|
-
|
|
5
|
-
interface Config {
|
|
6
|
-
duration: 'SHORT' | 'MEDIUM' | 'LONG';
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Aspect that automatically applies configuration-aware defaults to CloudWatch Log Groups
|
|
11
|
-
*
|
|
12
|
-
* Visits all constructs in the scope and automatically applies configuration-specific to log groups.
|
|
13
|
-
* Different configurations balance between cost optimization and data retention needs.
|
|
14
|
-
*
|
|
15
|
-
* @example
|
|
16
|
-
* ```typescript
|
|
17
|
-
* // Apply configuration-specific defaults to all log groups
|
|
18
|
-
* Aspects.of(app).add(new LogGroupDefaultsAspect({ duration: 'SHORT' }));
|
|
19
|
-
*
|
|
20
|
-
* // Log groups automatically inherit configuration defaults
|
|
21
|
-
* new LogGroup(stack, 'MyLogGroup', {
|
|
22
|
-
* // retention and removal policy applied automatically
|
|
23
|
-
* });
|
|
24
|
-
* ```
|
|
25
|
-
*
|
|
26
|
-
* @see https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_logs.LogGroup.html
|
|
27
|
-
*/
|
|
28
|
-
export class LogGroupDefaultsAspect implements IAspect {
|
|
29
|
-
private readonly defaultProps: LogGroupProps;
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Creates a new LogGroupDefaultsAspect
|
|
33
|
-
*
|
|
34
|
-
* @param config - Configuration identifier used to select appropriate defaults.
|
|
35
|
-
*/
|
|
36
|
-
constructor(config: Config) {
|
|
37
|
-
const props = this.retentionProperties(config.duration);
|
|
38
|
-
this.defaultProps = { ...props };
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Get duration-specific log group properties
|
|
43
|
-
*
|
|
44
|
-
* @param duration - The duration to get the log group properties for
|
|
45
|
-
* @returns The log group properties for the duration
|
|
46
|
-
*/
|
|
47
|
-
private retentionProperties(duration: 'SHORT' | 'MEDIUM' | 'LONG') {
|
|
48
|
-
switch (duration) {
|
|
49
|
-
case 'SHORT':
|
|
50
|
-
return { retention: RetentionDays.ONE_WEEK, removalPolicy: RemovalPolicy.DESTROY };
|
|
51
|
-
case 'MEDIUM':
|
|
52
|
-
return {
|
|
53
|
-
retention: RetentionDays.SIX_MONTHS,
|
|
54
|
-
removalPolicy: RemovalPolicy.DESTROY,
|
|
55
|
-
};
|
|
56
|
-
default:
|
|
57
|
-
return { retention: RetentionDays.TWO_YEARS, removalPolicy: RemovalPolicy.RETAIN };
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Visits a construct and applies configuration-appropriate defaults
|
|
63
|
-
*
|
|
64
|
-
* Applies configuration-specific retention and removal policies to log groups
|
|
65
|
-
* that don't already have these properties explicitly set.
|
|
66
|
-
*
|
|
67
|
-
* @param node - The construct to potentially modify
|
|
68
|
-
*/
|
|
69
|
-
visit(node: IConstruct): void {
|
|
70
|
-
if (node instanceof LogGroup) {
|
|
71
|
-
if (this.defaultProps.removalPolicy) {
|
|
72
|
-
node.applyRemovalPolicy(this.defaultProps.removalPolicy);
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
if (this.defaultProps.retention) {
|
|
76
|
-
const cfnLogGroup = node.node.defaultChild as CfnLogGroup;
|
|
77
|
-
if (cfnLogGroup && cfnLogGroup.retentionInDays === undefined) {
|
|
78
|
-
cfnLogGroup.retentionInDays = this.defaultProps.retention;
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
}
|
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
import { type IAspect } from 'aws-cdk-lib';
|
|
2
|
-
import { CfnFunction, Runtime, Tracing } from 'aws-cdk-lib/aws-lambda';
|
|
3
|
-
import { NodejsFunction } from 'aws-cdk-lib/aws-lambda-nodejs';
|
|
4
|
-
import { IConstruct } from 'constructs';
|
|
5
|
-
|
|
6
|
-
interface Config {
|
|
7
|
-
runtime: Runtime;
|
|
8
|
-
sourceMap?: boolean;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Aspect that automatically applies configuration-aware defaults to Node.js Lambda functions
|
|
13
|
-
*
|
|
14
|
-
* Visits all constructs in the scope and automatically applies configuration-specific runtime, tracing, and environment settings to Node.js Lambda functions.
|
|
15
|
-
* Different configurations can optimize for different priorities such as build speed, bundle size, or debugging capabilities.
|
|
16
|
-
*
|
|
17
|
-
* @example
|
|
18
|
-
* ```typescript
|
|
19
|
-
* // Apply configuration-specific defaults to all Node.js functions
|
|
20
|
-
* Aspects.of(app).add(new NodeJsFunctionDefaultsAspect({
|
|
21
|
-
* runtime: Runtime.NODEJS_24_X,
|
|
22
|
-
* sourceMap: true,
|
|
23
|
-
* }));
|
|
24
|
-
*
|
|
25
|
-
* // Functions automatically inherit configuration defaults
|
|
26
|
-
* new NodejsFunction(stack, 'MyFunction', {
|
|
27
|
-
* code: Code.fromAsset('src/lambda'),
|
|
28
|
-
* handler: 'index.handler',
|
|
29
|
-
* // runtime, tracing, and source map support applied automatically
|
|
30
|
-
* });
|
|
31
|
-
* ```
|
|
32
|
-
*
|
|
33
|
-
* @see https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_lambda_nodejs.NodejsFunction.html
|
|
34
|
-
*/
|
|
35
|
-
export class NodeJsFunctionDefaultsAspect implements IAspect {
|
|
36
|
-
private readonly config: Required<Config>;
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Creates a new NodeJsFunctionDefaultsAspect
|
|
40
|
-
*
|
|
41
|
-
* @param config - Configuration identifier used to select appropriate defaults.
|
|
42
|
-
*/
|
|
43
|
-
constructor(config: Config) {
|
|
44
|
-
this.config = { ...config, sourceMap: config.sourceMap ?? true };
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Visits a construct and applies runtime and tracing settings if it's a NodejsFunction
|
|
49
|
-
*
|
|
50
|
-
* Applies configuration-specific runtime, tracing, and environment settings to Node.js
|
|
51
|
-
* Lambda functions that don't already have these properties explicitly set.
|
|
52
|
-
*
|
|
53
|
-
* @param node - The construct to potentially modify
|
|
54
|
-
*/
|
|
55
|
-
visit(node: IConstruct): void {
|
|
56
|
-
if (node instanceof NodejsFunction) {
|
|
57
|
-
const cfnFunction = node.node.defaultChild as CfnFunction;
|
|
58
|
-
|
|
59
|
-
if (cfnFunction) {
|
|
60
|
-
cfnFunction.runtime = this.config.runtime.name;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
if (cfnFunction && cfnFunction.tracingConfig === undefined) {
|
|
64
|
-
cfnFunction.tracingConfig = { mode: Tracing.ACTIVE };
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
if (this.config.sourceMap) {
|
|
68
|
-
node.addEnvironment('NODE_OPTIONS', '--enable-source-maps');
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
}
|
package/src/generators/preset/files/application/_internal/step-function-defaults-aspect.ts.template
DELETED
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
import { type IAspect } from 'aws-cdk-lib';
|
|
2
|
-
import { CfnLogGroup, LogGroup } from 'aws-cdk-lib/aws-logs';
|
|
3
|
-
import {
|
|
4
|
-
CfnStateMachine,
|
|
5
|
-
LogLevel,
|
|
6
|
-
StateMachine,
|
|
7
|
-
StateMachineType,
|
|
8
|
-
} from 'aws-cdk-lib/aws-stepfunctions';
|
|
9
|
-
import { IConstruct } from 'constructs';
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Aspect that automatically applies tracing and logging settings to Step Functions
|
|
13
|
-
*
|
|
14
|
-
* Visits all constructs in the scope and automatically applies X-Ray tracing to all
|
|
15
|
-
* state machines. For EXPRESS state machines, automatically creates log groups and
|
|
16
|
-
* configures comprehensive logging for enhanced observability.
|
|
17
|
-
*
|
|
18
|
-
* @example
|
|
19
|
-
* ```typescript
|
|
20
|
-
* // Apply configuration-specific defaults to all Step Functions
|
|
21
|
-
* Aspects.of(app).add(new StepFunctionDefaultsAspect());
|
|
22
|
-
*
|
|
23
|
-
* // Step Functions automatically inherit defaults
|
|
24
|
-
* new StateMachine(stack, 'MyWorkflow', {
|
|
25
|
-
* stateMachineType: StateMachineType.EXPRESS,
|
|
26
|
-
* definitionBody: DefinitionBody.fromFile('workflow.asl.yaml'),
|
|
27
|
-
* // tracing enabled and logging configured automatically for EXPRESS
|
|
28
|
-
* });
|
|
29
|
-
* ```
|
|
30
|
-
*
|
|
31
|
-
* @see https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_stepfunctions.StateMachine.html
|
|
32
|
-
*/
|
|
33
|
-
export class StepFunctionDefaultsAspect implements IAspect {
|
|
34
|
-
/**
|
|
35
|
-
* Visits a construct and applies tracing and logging settings if it's a StateMachine
|
|
36
|
-
*
|
|
37
|
-
* Enables X-Ray tracing for all state machines. For EXPRESS state machines that don't
|
|
38
|
-
* already have logging configured, automatically creates log groups and configures
|
|
39
|
-
* comprehensive logging with full execution data capture.
|
|
40
|
-
*
|
|
41
|
-
* @param node - The construct to potentially modify
|
|
42
|
-
*/
|
|
43
|
-
visit(node: IConstruct): void {
|
|
44
|
-
if (node instanceof StateMachine) {
|
|
45
|
-
const cfnStateMachine = node.node.defaultChild as CfnStateMachine;
|
|
46
|
-
|
|
47
|
-
if (cfnStateMachine) {
|
|
48
|
-
// Apply tracing if not already set
|
|
49
|
-
if (cfnStateMachine.tracingConfiguration === undefined) {
|
|
50
|
-
cfnStateMachine.tracingConfiguration = {
|
|
51
|
-
enabled: true,
|
|
52
|
-
};
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
// For EXPRESS state machines, configure logging if not already set
|
|
56
|
-
if (
|
|
57
|
-
cfnStateMachine.stateMachineType === StateMachineType.EXPRESS &&
|
|
58
|
-
cfnStateMachine.loggingConfiguration === undefined
|
|
59
|
-
) {
|
|
60
|
-
// Create a log group for the state machine
|
|
61
|
-
const logGroup = new LogGroup(node, 'LogGroup', {
|
|
62
|
-
logGroupName: `/aws/vendedlogs/states/${node.node.id}`,
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
// Get the underlying CFN log group to access its ARN
|
|
66
|
-
const cfnLogGroup = logGroup.node.defaultChild as CfnLogGroup;
|
|
67
|
-
|
|
68
|
-
cfnStateMachine.loggingConfiguration = {
|
|
69
|
-
destinations: [
|
|
70
|
-
{
|
|
71
|
-
cloudWatchLogsLogGroup: {
|
|
72
|
-
logGroupArn: cfnLogGroup.attrArn,
|
|
73
|
-
},
|
|
74
|
-
},
|
|
75
|
-
],
|
|
76
|
-
level: LogLevel.ALL,
|
|
77
|
-
includeExecutionData: true,
|
|
78
|
-
};
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
}
|