@digitraffic/common 2023.9.13-1 → 2023.10.11-1

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/README.md CHANGED
@@ -2,6 +2,13 @@
2
2
 
3
3
  This is a place for common utilities and classes that can be used in other cdk-projects.
4
4
 
5
+ ## How to build
6
+
7
+ Use `npm` to build the code i.e.
8
+
9
+ npm run build
10
+ npm run test
11
+
5
12
  ## How to use
6
13
 
7
14
  In package.json dependencies:
@@ -48,7 +48,7 @@ class DigitrafficDLQueue {
48
48
  });
49
49
  const dlqFunctionName = `${dlqName}-Function`;
50
50
  const lambda = monitoredfunction_1.MonitoredFunction.create(stack, dlqFunctionName, {
51
- runtime: aws_lambda_1.Runtime.NODEJS_14_X,
51
+ runtime: aws_lambda_1.Runtime.NODEJS_16_X,
52
52
  logRetention: aws_logs_1.RetentionDays.ONE_YEAR,
53
53
  functionName: dlqFunctionName,
54
54
  code: getDlqCode(dlqBucket.bucketName),
@@ -15,7 +15,10 @@ export interface ProxyConfiguration {
15
15
  */
16
16
  export declare class DbProxyStack extends Stack {
17
17
  readonly isc: InfraStackConfiguration;
18
+ static PROXY_READER_EXPORT_NAME: string;
19
+ static PROXY_WRITER_EXPORT_NAME: string;
18
20
  constructor(scope: Construct, id: string, isc: InfraStackConfiguration, configuration: ProxyConfiguration);
21
+ setOutputs(proxy: DatabaseProxy): void;
19
22
  createProxy(vpc: IVpc, secret: ISecret, configuration: ProxyConfiguration): DatabaseProxy;
20
23
  createProxyEndpoints(vpc: IVpc, proxy: DatabaseProxy, securityGroupId: string): CfnDBProxyEndpoint;
21
24
  }
@@ -28,6 +28,12 @@ class DbProxyStack extends core_1.Stack {
28
28
  const readerEndpoint = this.createProxyEndpoints(vpc, proxy, configuration.securityGroupId);
29
29
  (0, parameters_1.createParameter)(this, "proxy.reader", readerEndpoint.attrEndpoint);
30
30
  (0, parameters_1.createParameter)(this, "proxy.writer", proxy.endpoint);
31
+ this.setOutputs(proxy);
32
+ }
33
+ setOutputs(proxy) {
34
+ // if only one instance, then there is no reader-endpoint
35
+ (0, import_util_1.exportValue)(this, this.isc.environmentName, DbProxyStack.PROXY_READER_EXPORT_NAME, proxy.endpoint);
36
+ (0, import_util_1.exportValue)(this, this.isc.environmentName, DbProxyStack.PROXY_WRITER_EXPORT_NAME, proxy.endpoint);
31
37
  }
32
38
  createProxy(vpc, secret, configuration) {
33
39
  const proxyId = `${this.isc.environmentName}-proxy`;
@@ -66,4 +72,6 @@ class DbProxyStack extends core_1.Stack {
66
72
  }
67
73
  }
68
74
  exports.DbProxyStack = DbProxyStack;
75
+ DbProxyStack.PROXY_READER_EXPORT_NAME = "db-reader-endpoint";
76
+ DbProxyStack.PROXY_WRITER_EXPORT_NAME = "db-writer-endpoint";
69
77
  //# sourceMappingURL=db-proxy-stack.js.map
@@ -6,6 +6,7 @@ import { InfraStackConfiguration } from "./intra-stack-configuration";
6
6
  import { Stack } from "aws-cdk-lib/core";
7
7
  export interface DbConfiguration {
8
8
  readonly cluster?: ClusterConfiguration;
9
+ readonly clusterImport?: ClusterImportConfiguration;
9
10
  readonly customParameterGroups: AuroraPostgresEngineVersion[];
10
11
  readonly workmem?: number;
11
12
  /** superuser username and password are fetched from this secret, using keys
@@ -23,6 +24,10 @@ export interface ClusterConfiguration {
23
24
  readonly dbVersion: AuroraPostgresEngineVersion;
24
25
  readonly storageEncrypted?: boolean;
25
26
  }
27
+ export interface ClusterImportConfiguration {
28
+ readonly clusterReadEndpoint: string;
29
+ readonly clusterWriteEndpoint: string;
30
+ }
26
31
  /**
27
32
  * Stack that creates DatabaseCluster.
28
33
  *
@@ -39,7 +44,7 @@ export declare class DbStack extends Stack {
39
44
  static CLUSTER_WRITE_ENDPOINT_EXPORT_NAME: string;
40
45
  clusterIdentifier: string;
41
46
  constructor(scope: Construct, id: string, isc: InfraStackConfiguration, configuration: DbConfiguration);
42
- createParamaterGroups(customVersions: AuroraPostgresEngineVersion[], workmem: number): IParameterGroup[];
47
+ createParameterGroups(customVersions: AuroraPostgresEngineVersion[], workmem: number): IParameterGroup[];
43
48
  createClusterParameters(secretArn: string, clusterConfiguration: ClusterConfiguration, instanceName: string, vpc: IVpc, securityGroup: ISecurityGroup, parameterGroup: IParameterGroup): DatabaseClusterProps;
44
49
  createAuroraCluster(isc: InfraStackConfiguration, configuration: DbConfiguration, clusterConfiguration: ClusterConfiguration, parameterGroups: IParameterGroup[]): DatabaseCluster;
45
50
  }
@@ -22,7 +22,11 @@ class DbStack extends core_1.Stack {
22
22
  env: isc.env,
23
23
  });
24
24
  this.clusterIdentifier = "";
25
- const parameterGroups = this.createParamaterGroups(configuration.customParameterGroups, configuration.workmem ?? 524288);
25
+ const parameterGroups = this.createParameterGroups(configuration.customParameterGroups, configuration.workmem ?? 524288);
26
+ if ((configuration.cluster && configuration.clusterImport) ||
27
+ (!configuration.cluster && !configuration.clusterImport)) {
28
+ throw new Error("Configure either cluster or clusterImport");
29
+ }
26
30
  // create cluster if this is wanted, should do it only once
27
31
  if (configuration.cluster) {
28
32
  const cluster = this.createAuroraCluster(isc, configuration, configuration.cluster, parameterGroups);
@@ -34,18 +38,28 @@ class DbStack extends core_1.Stack {
34
38
  (0, parameters_1.createParameter)(this, "cluster.identifier", cluster.clusterIdentifier);
35
39
  this.clusterIdentifier = cluster.clusterIdentifier;
36
40
  }
41
+ if (configuration.clusterImport) {
42
+ (0, parameters_1.createParameter)(this, "cluster.reader", configuration.clusterImport.clusterReadEndpoint);
43
+ (0, parameters_1.createParameter)(this, "cluster.writer", configuration.clusterImport.clusterWriteEndpoint);
44
+ }
37
45
  }
38
- createParamaterGroups(customVersions, workmem) {
39
- return customVersions.map((version) => new aws_rds_1.ParameterGroup(this, `parameter-group-${version.auroraPostgresMajorVersion}`, {
40
- engine: aws_rds_1.DatabaseClusterEngine.auroraPostgres({
41
- version,
42
- }),
43
- parameters: {
44
- "pg_stat_statements.track": "ALL",
45
- random_page_cost: "1",
46
- work_mem: workmem.toString(),
47
- },
48
- }));
46
+ createParameterGroups(customVersions, workmem) {
47
+ return customVersions.map((version) => {
48
+ const pg = new aws_rds_1.ParameterGroup(this, `parameter-group-${version.auroraPostgresMajorVersion}`, {
49
+ engine: aws_rds_1.DatabaseClusterEngine.auroraPostgres({
50
+ version,
51
+ }),
52
+ parameters: {
53
+ "pg_stat_statements.track": "ALL",
54
+ random_page_cost: "1",
55
+ work_mem: workmem.toString(),
56
+ },
57
+ });
58
+ // create both cluster parameter group and instance parameter group
59
+ pg.bindToCluster({});
60
+ pg.bindToInstance({});
61
+ return pg;
62
+ });
49
63
  }
50
64
  createClusterParameters(secretArn, clusterConfiguration, instanceName, vpc, securityGroup, parameterGroup) {
51
65
  const secret = aws_secretsmanager_1.Secret.fromSecretCompleteArn(this, "DBSecret", secretArn);
@@ -1,4 +1,8 @@
1
1
  import { DtLogger } from "./dt-logger";
2
+ /**
3
+ * You can use this for method name definition to match DtLogger LoggableType.method parameter.
4
+ */
5
+ export type { LoggerMethodType } from "./dt-logger";
2
6
  /**
3
7
  * You can use this for your logging needs or create one locally and configure it as you wish.
4
8
  */
@@ -2,6 +2,7 @@
2
2
  import { Writable } from "stream";
3
3
  /** Logging level */
4
4
  export type LOG_LEVEL = "DEBUG" | "INFO" | "WARN" | "ERROR";
5
+ export type LoggerMethodType = `${string}.${string}`;
5
6
  /**
6
7
  * Configuration object for configuring the Digitraffic logging utility
7
8
  * @see {@link DtLogger}
@@ -39,7 +40,7 @@ export interface CustomParams {
39
40
  */
40
41
  export interface LoggableType extends CustomParams {
41
42
  /** Name of the method logging the message */
42
- method: `${string}.${string}`;
43
+ method: LoggerMethodType;
43
44
  /** Message to log, optional */
44
45
  message?: string;
45
46
  /** Type of message, optional */
@@ -15,6 +15,7 @@ var RetryLogError;
15
15
  */
16
16
  exports.timeoutFunctions = (function () {
17
17
  return {
18
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
18
19
  noTimeout: (retryCount) => {
19
20
  return 0;
20
21
  },
@@ -40,6 +41,7 @@ exports.retryPredicates = (function () {
40
41
  }
41
42
  return false;
42
43
  },
44
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
43
45
  alwaysRetry: (error) => {
44
46
  return true;
45
47
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@digitraffic/common",
3
- "version": "2023.9.13-1",
3
+ "version": "2023.10.11-1",
4
4
  "description": "",
5
5
  "repository": {
6
6
  "type": "git",
@@ -61,7 +61,7 @@ export class DigitrafficDLQueue {
61
61
 
62
62
  const dlqFunctionName = `${dlqName}-Function`;
63
63
  const lambda = MonitoredFunction.create(stack, dlqFunctionName, {
64
- runtime: Runtime.NODEJS_14_X,
64
+ runtime: Runtime.NODEJS_16_X,
65
65
  logRetention: RetentionDays.ONE_YEAR,
66
66
  functionName: dlqFunctionName,
67
67
  code: getDlqCode(dlqBucket.bucketName),
@@ -9,7 +9,7 @@ import { ISecret, Secret } from "aws-cdk-lib/aws-secretsmanager";
9
9
  import { IVpc, SecurityGroup } from "aws-cdk-lib/aws-ec2";
10
10
  import { InfraStackConfiguration } from "./intra-stack-configuration";
11
11
  import { DbStack } from "./db-stack";
12
- import { importVpc } from "../import-util";
12
+ import { exportValue, importVpc } from "../import-util";
13
13
  import { createParameter } from "../stack/parameters";
14
14
  import { Stack, Duration } from "aws-cdk-lib/core";
15
15
  import { Construct } from "constructs/lib/construct";
@@ -27,6 +27,9 @@ export interface ProxyConfiguration {
27
27
  export class DbProxyStack extends Stack {
28
28
  readonly isc: InfraStackConfiguration;
29
29
 
30
+ public static PROXY_READER_EXPORT_NAME = "db-reader-endpoint";
31
+ public static PROXY_WRITER_EXPORT_NAME = "db-writer-endpoint";
32
+
30
33
  constructor(
31
34
  scope: Construct,
32
35
  id: string,
@@ -57,6 +60,24 @@ export class DbProxyStack extends Stack {
57
60
 
58
61
  createParameter(this, "proxy.reader", readerEndpoint.attrEndpoint);
59
62
  createParameter(this, "proxy.writer", proxy.endpoint);
63
+
64
+ this.setOutputs(proxy);
65
+ }
66
+
67
+ setOutputs(proxy: DatabaseProxy) {
68
+ // if only one instance, then there is no reader-endpoint
69
+ exportValue(
70
+ this,
71
+ this.isc.environmentName,
72
+ DbProxyStack.PROXY_READER_EXPORT_NAME,
73
+ proxy.endpoint
74
+ );
75
+ exportValue(
76
+ this,
77
+ this.isc.environmentName,
78
+ DbProxyStack.PROXY_WRITER_EXPORT_NAME,
79
+ proxy.endpoint
80
+ );
60
81
  }
61
82
 
62
83
  createProxy(vpc: IVpc, secret: ISecret, configuration: ProxyConfiguration) {
@@ -26,6 +26,8 @@ import { createParameter } from "../stack/parameters";
26
26
 
27
27
  export interface DbConfiguration {
28
28
  readonly cluster?: ClusterConfiguration;
29
+ readonly clusterImport?: ClusterImportConfiguration;
30
+
29
31
  readonly customParameterGroups: AuroraPostgresEngineVersion[];
30
32
  readonly workmem?: number; // default 524288, 512MiB
31
33
 
@@ -47,6 +49,11 @@ export interface ClusterConfiguration {
47
49
  readonly storageEncrypted?: boolean; /// default true
48
50
  }
49
51
 
52
+ export interface ClusterImportConfiguration {
53
+ readonly clusterReadEndpoint: string;
54
+ readonly clusterWriteEndpoint: string;
55
+ }
56
+
50
57
  /**
51
58
  * Stack that creates DatabaseCluster.
52
59
  *
@@ -78,11 +85,18 @@ export class DbStack extends Stack {
78
85
  env: isc.env,
79
86
  });
80
87
 
81
- const parameterGroups = this.createParamaterGroups(
88
+ const parameterGroups = this.createParameterGroups(
82
89
  configuration.customParameterGroups,
83
90
  configuration.workmem ?? 524288
84
91
  );
85
92
 
93
+ if (
94
+ (configuration.cluster && configuration.clusterImport) ||
95
+ (!configuration.cluster && !configuration.clusterImport)
96
+ ) {
97
+ throw new Error("Configure either cluster or clusterImport");
98
+ }
99
+
86
100
  // create cluster if this is wanted, should do it only once
87
101
  if (configuration.cluster) {
88
102
  const cluster = this.createAuroraCluster(
@@ -131,29 +145,47 @@ export class DbStack extends Stack {
131
145
 
132
146
  this.clusterIdentifier = cluster.clusterIdentifier;
133
147
  }
148
+
149
+ if (configuration.clusterImport) {
150
+ createParameter(
151
+ this,
152
+ "cluster.reader",
153
+ configuration.clusterImport.clusterReadEndpoint
154
+ );
155
+ createParameter(
156
+ this,
157
+ "cluster.writer",
158
+ configuration.clusterImport.clusterWriteEndpoint
159
+ );
160
+ }
134
161
  }
135
162
 
136
- createParamaterGroups(
163
+ createParameterGroups(
137
164
  customVersions: AuroraPostgresEngineVersion[],
138
165
  workmem: number
139
166
  ): IParameterGroup[] {
140
- return customVersions.map(
141
- (version: AuroraPostgresEngineVersion) =>
142
- new ParameterGroup(
143
- this,
144
- `parameter-group-${version.auroraPostgresMajorVersion}`,
145
- {
146
- engine: DatabaseClusterEngine.auroraPostgres({
147
- version,
148
- }),
149
- parameters: {
150
- "pg_stat_statements.track": "ALL",
151
- random_page_cost: "1",
152
- work_mem: workmem.toString(),
153
- },
154
- }
155
- )
156
- );
167
+ return customVersions.map((version: AuroraPostgresEngineVersion) => {
168
+ const pg = new ParameterGroup(
169
+ this,
170
+ `parameter-group-${version.auroraPostgresMajorVersion}`,
171
+ {
172
+ engine: DatabaseClusterEngine.auroraPostgres({
173
+ version,
174
+ }),
175
+ parameters: {
176
+ "pg_stat_statements.track": "ALL",
177
+ random_page_cost: "1",
178
+ work_mem: workmem.toString(),
179
+ },
180
+ }
181
+ );
182
+
183
+ // create both cluster parameter group and instance parameter group
184
+ pg.bindToCluster({});
185
+ pg.bindToInstance({});
186
+
187
+ return pg;
188
+ });
157
189
  }
158
190
 
159
191
  createClusterParameters(
@@ -1,5 +1,10 @@
1
1
  import { DtLogger } from "./dt-logger";
2
2
 
3
+ /**
4
+ * You can use this for method name definition to match DtLogger LoggableType.method parameter.
5
+ */
6
+ export type { LoggerMethodType } from "./dt-logger";
7
+
3
8
  /**
4
9
  * You can use this for your logging needs or create one locally and configure it as you wish.
5
10
  */
@@ -3,6 +3,7 @@ import _ from "lodash";
3
3
 
4
4
  /** Logging level */
5
5
  export type LOG_LEVEL = "DEBUG" | "INFO" | "WARN" | "ERROR";
6
+ export type LoggerMethodType = `${string}.${string}`;
6
7
 
7
8
  /**
8
9
  * Configuration object for configuring the Digitraffic logging utility
@@ -55,7 +56,7 @@ export interface CustomParams {
55
56
  */
56
57
  export interface LoggableType extends CustomParams {
57
58
  /** Name of the method logging the message */
58
- method: `${string}.${string}`;
59
+ method: LoggerMethodType;
59
60
  /** Message to log, optional */
60
61
  message?: string;
61
62
  /** Type of message, optional */
@@ -16,6 +16,7 @@ export type RetryPredicate = (error: unknown) => boolean;
16
16
  */
17
17
  export const timeoutFunctions = (function () {
18
18
  return {
19
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
19
20
  noTimeout: (retryCount: number): number => {
20
21
  return 0;
21
22
  },
@@ -42,6 +43,7 @@ export const retryPredicates = (function () {
42
43
  }
43
44
  return false;
44
45
  },
46
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
45
47
  alwaysRetry: (error: unknown): boolean => {
46
48
  return true;
47
49
  },