@byaga/cdk-patterns 0.9.3 → 0.11.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/.fleet/run.json +11 -0
- package/LICENSE.md +21 -0
- package/README.md +43 -0
- package/archive/ApiAttachPoint.ts +9 -0
- package/archive/ApiCertificate.ts +32 -0
- package/{lib/CognitoApiGatewayAuthorizer.d.ts → archive/CognitoApiGatewayAuthorizer.ts} +13 -7
- package/archive/DynamoDbTable.ts +40 -0
- package/archive/ICorsConfig.ts +5 -0
- package/archive/IDomainConfig.ts +8 -0
- package/archive/IHostedZoneConfig.ts +5 -0
- package/archive/NodeJsLambdaLayer.ts +42 -0
- package/archive/Output.ts +11 -0
- package/{lib/RestApi.js → archive/RestApi.ts} +96 -60
- package/{lib/Role.d.ts → archive/Role.ts} +165 -139
- package/archive/StaticWebSite.ts +159 -0
- package/archive/index.ts +18 -0
- package/archive/methods/apply-schema-to-method-options.ts +38 -0
- package/archive/methods/attach-function-to-api.ts +69 -0
- package/lib/build/copy-files.d.ts +8 -0
- package/lib/{methods/empty-directory.js → build/copy-files.js} +22 -15
- package/lib/build/generate-hash.d.ts +7 -0
- package/lib/build/generate-hash.js +31 -0
- package/lib/build/get-files.d.ts +9 -0
- package/lib/build/get-files.js +38 -0
- package/lib/build/get-source-directory.d.ts +18 -0
- package/lib/build/get-source-directory.js +38 -0
- package/lib/build/hash-file.d.ts +18 -0
- package/lib/build/hash-file.js +45 -0
- package/lib/build/nodejs/build-ecmascript.d.ts +9 -0
- package/lib/build/nodejs/build-ecmascript.js +20 -0
- package/lib/build/nodejs/build-node-source.d.ts +12 -0
- package/lib/build/nodejs/build-node-source.js +39 -0
- package/lib/build/nodejs/build-typescript.d.ts +8 -0
- package/lib/build/nodejs/build-typescript.js +33 -0
- package/lib/build/nodejs/install-node-modules.d.ts +12 -0
- package/lib/build/nodejs/install-node-modules.js +30 -0
- package/lib/create-stack.d.ts +38 -0
- package/lib/create-stack.js +46 -0
- package/lib/generate-identifier.d.ts +27 -0
- package/lib/generate-identifier.js +65 -0
- package/lib/index.d.ts +7 -15
- package/lib/index.js +40 -25
- package/lib/lambda/create-function.d.ts +20 -0
- package/lib/lambda/create-function.js +39 -0
- package/lib/lambda/create-nodejs-lambda.d.ts +28 -0
- package/lib/lambda/create-nodejs-lambda.js +38 -0
- package/lib/lambda-layer/apply-honeycomb-to-lambda.d.ts +2 -0
- package/lib/lambda-layer/apply-honeycomb-to-lambda.js +25 -0
- package/lib/lambda-layer/get-layer.d.ts +2 -0
- package/lib/lambda-layer/get-layer.js +20 -0
- package/lib/lambda-layer/layer-cache.d.ts +3 -0
- package/lib/lambda-layer/layer-cache.js +12 -0
- package/lib/ssm/SsmParameter.d.ts +12 -0
- package/lib/ssm/get-existing-parameter.d.ts +9 -0
- package/lib/ssm/get-existing-parameter.js +41 -0
- package/lib/ssm/grant-read.d.ts +7 -0
- package/lib/ssm/grant-read.js +24 -0
- package/lib/ssm/index.d.ts +3 -0
- package/lib/ssm/index.js +9 -0
- package/lib/ssm/parameter-cache.d.ts +13 -0
- package/lib/ssm/parameter-cache.js +23 -0
- package/lib/ssm/string-value.d.ts +8 -0
- package/lib/ssm/string-value.js +14 -0
- package/lib/tools/duration.d.ts +10 -0
- package/lib/tools/duration.js +28 -0
- package/package.json +25 -11
- package/jest.config.js +0 -16
- package/lib/ApiCertificate.d.ts +0 -10
- package/lib/ApiCertificate.js +0 -31
- package/lib/CognitoApiGatewayAuthorizer.js +0 -24
- package/lib/DeployStack.d.ts +0 -23
- package/lib/DeployStack.js +0 -58
- package/lib/DynamoDbTable.d.ts +0 -12
- package/lib/DynamoDbTable.js +0 -34
- package/lib/FunctionIntegration.d.ts +0 -25
- package/lib/FunctionIntegration.js +0 -65
- package/lib/ICorsConfig.d.ts +0 -5
- package/lib/IDeployStack.d.ts +0 -20
- package/lib/IDeployStack.js +0 -48
- package/lib/IDomainConfig.d.ts +0 -7
- package/lib/IDomainConfig.js +0 -2
- package/lib/IHostedZoneConfig.d.ts +0 -5
- package/lib/IHostedZoneConfig.js +0 -2
- package/lib/IStackArguments.d.ts +0 -8
- package/lib/IStackArguments.js +0 -2
- package/lib/NodeJsLambda.d.ts +0 -13
- package/lib/NodeJsLambda.js +0 -28
- package/lib/NodeJsLambdaLayer.d.ts +0 -12
- package/lib/NodeJsLambdaLayer.js +0 -24
- package/lib/Output.d.ts +0 -5
- package/lib/Output.js +0 -13
- package/lib/RestApi.d.ts +0 -28
- package/lib/Role.js +0 -27
- package/lib/SsmParameter.d.ts +0 -18
- package/lib/SsmParameter.js +0 -40
- package/lib/StaticWebSite.d.ts +0 -17
- package/lib/StaticWebSite.js +0 -122
- package/lib/methods/apply-honeycomb-to-lambda.d.ts +0 -3
- package/lib/methods/apply-honeycomb-to-lambda.js +0 -23
- package/lib/methods/apply-honeycomb-to-lambda.spec.d.ts +0 -1
- package/lib/methods/apply-honeycomb-to-lambda.spec.js +0 -61
- package/lib/methods/build-node-source.d.ts +0 -13
- package/lib/methods/build-node-source.js +0 -105
- package/lib/methods/duration.d.ts +0 -8
- package/lib/methods/duration.js +0 -22
- package/lib/methods/empty-directory.d.ts +0 -2
- package/lib/methods/generate-hash.d.ts +0 -15
- package/lib/methods/generate-hash.js +0 -59
- package/lib/methods/get-source-directory.d.ts +0 -4
- package/lib/methods/get-source-directory.js +0 -37
- package/lib/methods/walk-directory.d.ts +0 -14
- package/lib/methods/walk-directory.js +0 -48
- package/test-results/html/index.html +0 -1
- package/test-results/html/jest-html-reporters-attach/index/index.js +0 -3
- package/test-results/html/jest-html-reporters-attach/index/result.js +0 -1
- package/test-results/junit/junit.xml +0 -13
- /package/lib/{ICorsConfig.js → ssm/SsmParameter.js} +0 -0
package/lib/index.js
CHANGED
@@ -1,27 +1,42 @@
|
|
1
1
|
"use strict";
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3
|
+
if (k2 === undefined) k2 = k;
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
7
|
+
}
|
8
|
+
Object.defineProperty(o, k2, desc);
|
9
|
+
}) : (function(o, m, k, k2) {
|
10
|
+
if (k2 === undefined) k2 = k;
|
11
|
+
o[k2] = m[k];
|
12
|
+
}));
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
15
|
+
}) : function(o, v) {
|
16
|
+
o["default"] = v;
|
17
|
+
});
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
19
|
+
if (mod && mod.__esModule) return mod;
|
20
|
+
var result = {};
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
22
|
+
__setModuleDefault(result, mod);
|
23
|
+
return result;
|
24
|
+
};
|
2
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.
|
4
|
-
var
|
5
|
-
Object.defineProperty(exports, "
|
6
|
-
var
|
7
|
-
Object.defineProperty(exports, "
|
8
|
-
var
|
9
|
-
Object.defineProperty(exports, "
|
10
|
-
var
|
11
|
-
Object.defineProperty(exports, "
|
12
|
-
var
|
13
|
-
Object.defineProperty(exports, "
|
14
|
-
var
|
15
|
-
Object.defineProperty(exports, "
|
16
|
-
|
17
|
-
Object.defineProperty(exports, "
|
18
|
-
|
19
|
-
|
20
|
-
var RestApi_1 = require("./RestApi");
|
21
|
-
Object.defineProperty(exports, "RestApi", { enumerable: true, get: function () { return RestApi_1.RestApi; } });
|
22
|
-
var Role_1 = require("./Role");
|
23
|
-
Object.defineProperty(exports, "Role", { enumerable: true, get: function () { return Role_1.Role; } });
|
24
|
-
var SsmParameter_1 = require("./SsmParameter");
|
25
|
-
Object.defineProperty(exports, "SsmParameter", { enumerable: true, get: function () { return SsmParameter_1.SsmParameter; } });
|
26
|
-
var StaticWebSite_1 = require("./StaticWebSite");
|
27
|
-
Object.defineProperty(exports, "StaticWebSite", { enumerable: true, get: function () { return StaticWebSite_1.StaticWebSite; } });
|
26
|
+
exports.ssm = exports.genStackResourceId = exports.genStackResourceName = exports.genId = exports.genName = exports.createStack = exports.createNodeJsLambda = exports.buildTypeScript = exports.buildEcmaScript = exports.buildNodeSource = void 0;
|
27
|
+
var build_node_source_1 = require("./build/nodejs/build-node-source");
|
28
|
+
Object.defineProperty(exports, "buildNodeSource", { enumerable: true, get: function () { return build_node_source_1.buildNodeSource; } });
|
29
|
+
var build_ecmascript_1 = require("./build/nodejs/build-ecmascript");
|
30
|
+
Object.defineProperty(exports, "buildEcmaScript", { enumerable: true, get: function () { return build_ecmascript_1.buildEcmaScript; } });
|
31
|
+
var build_typescript_1 = require("./build/nodejs/build-typescript");
|
32
|
+
Object.defineProperty(exports, "buildTypeScript", { enumerable: true, get: function () { return build_typescript_1.buildTypeScript; } });
|
33
|
+
var create_nodejs_lambda_1 = require("./lambda/create-nodejs-lambda");
|
34
|
+
Object.defineProperty(exports, "createNodeJsLambda", { enumerable: true, get: function () { return create_nodejs_lambda_1.createNodeJsLambda; } });
|
35
|
+
var create_stack_1 = require("./create-stack");
|
36
|
+
Object.defineProperty(exports, "createStack", { enumerable: true, get: function () { return create_stack_1.createStack; } });
|
37
|
+
var generate_identifier_1 = require("./generate-identifier");
|
38
|
+
Object.defineProperty(exports, "genName", { enumerable: true, get: function () { return generate_identifier_1.genName; } });
|
39
|
+
Object.defineProperty(exports, "genId", { enumerable: true, get: function () { return generate_identifier_1.genId; } });
|
40
|
+
Object.defineProperty(exports, "genStackResourceName", { enumerable: true, get: function () { return generate_identifier_1.genStackResourceName; } });
|
41
|
+
Object.defineProperty(exports, "genStackResourceId", { enumerable: true, get: function () { return generate_identifier_1.genStackResourceId; } });
|
42
|
+
exports.ssm = __importStar(require("./ssm"));
|
@@ -0,0 +1,20 @@
|
|
1
|
+
import { Function as Lambda, FunctionProps } from "aws-cdk-lib/aws-lambda";
|
2
|
+
import { Duration } from "aws-cdk-lib";
|
3
|
+
/**
|
4
|
+
* Interface for the properties of the FunctionIntegration class.
|
5
|
+
*/
|
6
|
+
export interface FunctionIntegrationProps {
|
7
|
+
funcProps?: FunctionProps;
|
8
|
+
timeout?: Duration;
|
9
|
+
memory?: number;
|
10
|
+
}
|
11
|
+
export interface FunctionIntegration {
|
12
|
+
id: string;
|
13
|
+
function: Lambda;
|
14
|
+
}
|
15
|
+
/**
|
16
|
+
* Initializes a new function
|
17
|
+
* @param {string} id - The ID of the function.
|
18
|
+
* @param {FunctionIntegrationProps} options - The properties of the function.
|
19
|
+
*/
|
20
|
+
export declare function createFunction(id: string, options: FunctionIntegrationProps): FunctionIntegration;
|
@@ -0,0 +1,39 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.createFunction = void 0;
|
4
|
+
const aws_lambda_1 = require("aws-cdk-lib/aws-lambda");
|
5
|
+
const aws_cdk_lib_1 = require("aws-cdk-lib");
|
6
|
+
const apply_honeycomb_to_lambda_1 = require("../lambda-layer/apply-honeycomb-to-lambda");
|
7
|
+
const aws_logs_1 = require("aws-cdk-lib/aws-logs");
|
8
|
+
const core_1 = require("aws-cdk-lib/core");
|
9
|
+
const generate_identifier_1 = require("../generate-identifier");
|
10
|
+
const create_stack_1 = require("../create-stack");
|
11
|
+
/**
|
12
|
+
* Initializes a new function
|
13
|
+
* @param {string} id - The ID of the function.
|
14
|
+
* @param {FunctionIntegrationProps} options - The properties of the function.
|
15
|
+
*/
|
16
|
+
function createFunction(id, options) {
|
17
|
+
const props = (0, apply_honeycomb_to_lambda_1.applyHoneycombToLambda)({
|
18
|
+
functionName: (0, generate_identifier_1.genStackResourceName)(id),
|
19
|
+
memorySize: options.memory || 256,
|
20
|
+
timeout: options.timeout || aws_cdk_lib_1.Duration.seconds(30),
|
21
|
+
...(options.funcProps || {})
|
22
|
+
});
|
23
|
+
const { stack } = (0, create_stack_1.getCurrentStack)();
|
24
|
+
const details = {
|
25
|
+
id,
|
26
|
+
function: new aws_lambda_1.Function(stack, (0, generate_identifier_1.genStackResourceId)(id, 'lambda'), props)
|
27
|
+
};
|
28
|
+
new aws_cdk_lib_1.CfnOutput(stack, (0, generate_identifier_1.genStackResourceId)(id, 'function-name'), {
|
29
|
+
value: details.function.functionName,
|
30
|
+
exportName: (0, generate_identifier_1.genStackResourceName)(id, 'function-name')
|
31
|
+
});
|
32
|
+
new aws_logs_1.LogRetention(stack, (0, generate_identifier_1.genStackResourceId)(id, 'log-retention'), {
|
33
|
+
logGroupName: details.function.logGroup.logGroupName,
|
34
|
+
retention: aws_logs_1.RetentionDays.ONE_WEEK,
|
35
|
+
removalPolicy: core_1.RemovalPolicy.DESTROY,
|
36
|
+
});
|
37
|
+
return details;
|
38
|
+
}
|
39
|
+
exports.createFunction = createFunction;
|
@@ -0,0 +1,28 @@
|
|
1
|
+
import { FunctionOptions } from "aws-cdk-lib/aws-lambda";
|
2
|
+
import { FunctionIntegration } from "./create-function";
|
3
|
+
import { Duration } from "aws-cdk-lib";
|
4
|
+
/**
|
5
|
+
* Interface for the properties of a Node.js function.
|
6
|
+
*/
|
7
|
+
interface NodeFunctionProps {
|
8
|
+
/**
|
9
|
+
* The properties of the function.
|
10
|
+
*/
|
11
|
+
funcProps?: FunctionOptions;
|
12
|
+
/**
|
13
|
+
* The timeout duration for the function.
|
14
|
+
*/
|
15
|
+
timeout?: Duration;
|
16
|
+
/**
|
17
|
+
* The memory size for the function.
|
18
|
+
*/
|
19
|
+
memory?: number;
|
20
|
+
}
|
21
|
+
/**
|
22
|
+
* Creates a new Node.js AWS Lambda function.
|
23
|
+
* @param {string} id - The ID of the function.
|
24
|
+
* @param {NodeFunctionProps} [options] - The properties of the function.
|
25
|
+
* @returns {FunctionIntegration} The created function.
|
26
|
+
*/
|
27
|
+
export declare function createNodeJsLambda(id: string, options?: NodeFunctionProps): FunctionIntegration;
|
28
|
+
export {};
|
@@ -0,0 +1,38 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
+
exports.createNodeJsLambda = void 0;
|
7
|
+
const aws_lambda_1 = require("aws-cdk-lib/aws-lambda");
|
8
|
+
const create_function_1 = require("./create-function");
|
9
|
+
const build_node_source_1 = require("../build/nodejs/build-node-source");
|
10
|
+
const duration_1 = __importDefault(require("../tools/duration"));
|
11
|
+
/**
|
12
|
+
* Creates a new Node.js AWS Lambda function.
|
13
|
+
* @param {string} id - The ID of the function.
|
14
|
+
* @param {NodeFunctionProps} [options] - The properties of the function.
|
15
|
+
* @returns {FunctionIntegration} The created function.
|
16
|
+
*/
|
17
|
+
function createNodeJsLambda(id, options) {
|
18
|
+
console.log('Defining Node Lambda', id);
|
19
|
+
// Measure the duration of the build process
|
20
|
+
const done = (0, duration_1.default)();
|
21
|
+
// Build the Node.js source code
|
22
|
+
const buildDir = (0, build_node_source_1.buildNodeSource)('lambda', id);
|
23
|
+
console.log('Total Build Duration (ms)', done());
|
24
|
+
// Call the parent constructor with the function properties
|
25
|
+
return (0, create_function_1.createFunction)(id, {
|
26
|
+
...options,
|
27
|
+
funcProps: {
|
28
|
+
...options?.funcProps,
|
29
|
+
// The source code of the function
|
30
|
+
code: aws_lambda_1.Code.fromAsset(buildDir),
|
31
|
+
// The handler of the function
|
32
|
+
handler: `${id}.handler`,
|
33
|
+
// The runtime of the function
|
34
|
+
runtime: aws_lambda_1.Runtime.NODEJS_20_X
|
35
|
+
}
|
36
|
+
});
|
37
|
+
}
|
38
|
+
exports.createNodeJsLambda = createNodeJsLambda;
|
@@ -0,0 +1,25 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.applyHoneycombToLambda = void 0;
|
4
|
+
const ssm_1 = require("../ssm");
|
5
|
+
const create_stack_1 = require("../create-stack");
|
6
|
+
const get_layer_1 = require("./get-layer");
|
7
|
+
function applyHoneycombToLambda(props) {
|
8
|
+
const { stack } = (0, create_stack_1.getCurrentStack)();
|
9
|
+
const honeyCombLayer = (0, get_layer_1.getLayer)('hc-layer', `arn:aws:lambda:${stack.region}:702835727665:layer:honeycomb-lambda-extension-x86_64-v11-1-1:1`);
|
10
|
+
const layers = props.layers ? [...props.layers] : [];
|
11
|
+
const environment = { ...props.environment };
|
12
|
+
const options = {
|
13
|
+
parameterGroup: (0, create_stack_1.getCurrentStack)().project
|
14
|
+
};
|
15
|
+
layers.push(honeyCombLayer);
|
16
|
+
environment.LIBHONEY_API_KEY = (0, ssm_1.stringValue)(`/honeycomb/api-key`, options);
|
17
|
+
environment.LIBHONEY_DATASET = (0, ssm_1.stringValue)(`/honeycomb/dataset`, options);
|
18
|
+
environment.LOGS_API_DISABLE_PLATFORM_MSGS = "true";
|
19
|
+
return {
|
20
|
+
...props,
|
21
|
+
layers,
|
22
|
+
environment
|
23
|
+
};
|
24
|
+
}
|
25
|
+
exports.applyHoneycombToLambda = applyHoneycombToLambda;
|
@@ -0,0 +1,20 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.getLayer = void 0;
|
4
|
+
const aws_lambda_1 = require("aws-cdk-lib/aws-lambda");
|
5
|
+
const create_stack_1 = require("../create-stack");
|
6
|
+
const layer_cache_1 = require("./layer-cache");
|
7
|
+
const generate_identifier_1 = require("../generate-identifier");
|
8
|
+
function getLayer(name, arn) {
|
9
|
+
let layer = (0, layer_cache_1.get)(arn);
|
10
|
+
if (!layer) {
|
11
|
+
layer = createLayer(name, arn);
|
12
|
+
(0, layer_cache_1.store)(arn, layer);
|
13
|
+
}
|
14
|
+
return layer;
|
15
|
+
}
|
16
|
+
exports.getLayer = getLayer;
|
17
|
+
function createLayer(name, arn) {
|
18
|
+
const { stack } = (0, create_stack_1.getCurrentStack)();
|
19
|
+
return aws_lambda_1.LayerVersion.fromLayerVersionArn(stack, (0, generate_identifier_1.genStackResourceId)(name), arn);
|
20
|
+
}
|
@@ -0,0 +1,12 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.store = exports.get = void 0;
|
4
|
+
const layerCache = {};
|
5
|
+
function get(name) {
|
6
|
+
return layerCache[name];
|
7
|
+
}
|
8
|
+
exports.get = get;
|
9
|
+
function store(name, param) {
|
10
|
+
layerCache[name] = param;
|
11
|
+
}
|
12
|
+
exports.store = store;
|
@@ -0,0 +1,12 @@
|
|
1
|
+
import { IStringParameter } from "aws-cdk-lib/aws-ssm";
|
2
|
+
import { IKey } from "aws-cdk-lib/aws-kms";
|
3
|
+
export interface SsmParameterOptions {
|
4
|
+
decryptWithKey?: IKey;
|
5
|
+
parameterGroup?: string;
|
6
|
+
}
|
7
|
+
export interface SsmParameter {
|
8
|
+
shortName: string;
|
9
|
+
parameterArn: string;
|
10
|
+
decryptWithKey?: IKey;
|
11
|
+
parameter: IStringParameter;
|
12
|
+
}
|
@@ -0,0 +1,9 @@
|
|
1
|
+
import { SsmParameter, SsmParameterOptions } from "./SsmParameter";
|
2
|
+
/**
|
3
|
+
* Retrieves an existing SSM parameter.
|
4
|
+
* If the parameter is not in the cache, it references the parameter and stores it in the cache.
|
5
|
+
* @param {string} name - The name of the parameter.
|
6
|
+
* @param {SsmParameterOptions} [options] - The options for the parameter.
|
7
|
+
* @returns {SsmParameter} The SSM parameter.
|
8
|
+
*/
|
9
|
+
export declare function getExistingParameter(name: string, options?: SsmParameterOptions): SsmParameter;
|
@@ -0,0 +1,41 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.getExistingParameter = void 0;
|
4
|
+
const aws_ssm_1 = require("aws-cdk-lib/aws-ssm");
|
5
|
+
const parameter_cache_1 = require("./parameter-cache");
|
6
|
+
const create_stack_1 = require("../create-stack");
|
7
|
+
const generate_identifier_1 = require("../generate-identifier");
|
8
|
+
/**
|
9
|
+
* Retrieves an existing SSM parameter.
|
10
|
+
* If the parameter is not in the cache, it references the parameter and stores it in the cache.
|
11
|
+
* @param {string} name - The name of the parameter.
|
12
|
+
* @param {SsmParameterOptions} [options] - The options for the parameter.
|
13
|
+
* @returns {SsmParameter} The SSM parameter.
|
14
|
+
*/
|
15
|
+
function getExistingParameter(name, options) {
|
16
|
+
let param = (0, parameter_cache_1.get)(name);
|
17
|
+
if (!param) {
|
18
|
+
param = referenceParameter(name, options);
|
19
|
+
(0, parameter_cache_1.store)(name, param);
|
20
|
+
}
|
21
|
+
return param;
|
22
|
+
}
|
23
|
+
exports.getExistingParameter = getExistingParameter;
|
24
|
+
/**
|
25
|
+
* References an SSM parameter and returns it.
|
26
|
+
* @param {string} name - The name of the parameter.
|
27
|
+
* @param {SsmParameterOptions} [options] - The options for the parameter.
|
28
|
+
* @returns {SsmParameter} The SSM parameter.
|
29
|
+
*/
|
30
|
+
function referenceParameter(name, options) {
|
31
|
+
if (name[0] !== '/')
|
32
|
+
name = '/' + name;
|
33
|
+
const { stack } = (0, create_stack_1.getCurrentStack)();
|
34
|
+
const parameterName = `/${options?.parameterGroup ?? stack.stackName}${name}`;
|
35
|
+
return {
|
36
|
+
shortName: name,
|
37
|
+
parameterArn: `arn:aws:ssm:${stack.region}:${stack.account}:parameter${parameterName}`,
|
38
|
+
decryptWithKey: options?.decryptWithKey,
|
39
|
+
parameter: aws_ssm_1.StringParameter.fromStringParameterName(stack, (0, generate_identifier_1.genStackResourceId)(parameterName), parameterName)
|
40
|
+
};
|
41
|
+
}
|
@@ -0,0 +1,7 @@
|
|
1
|
+
import { IGrantable } from "aws-cdk-lib/aws-iam";
|
2
|
+
/**
|
3
|
+
* Grants read access to an SSM parameter for a grantee.
|
4
|
+
* @param {string} name - The name of the SSM parameter.
|
5
|
+
* @param {IGrantable} grantee - The entity to be granted read access.
|
6
|
+
*/
|
7
|
+
export declare function grantRead(name: string, grantee: IGrantable): void;
|
@@ -0,0 +1,24 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.grantRead = void 0;
|
4
|
+
const aws_iam_1 = require("aws-cdk-lib/aws-iam");
|
5
|
+
const parameter_cache_1 = require("./parameter-cache");
|
6
|
+
/**
|
7
|
+
* Grants read access to an SSM parameter for a grantee.
|
8
|
+
* @param {string} name - The name of the SSM parameter.
|
9
|
+
* @param {IGrantable} grantee - The entity to be granted read access.
|
10
|
+
*/
|
11
|
+
function grantRead(name, grantee) {
|
12
|
+
// Retrieve the SSM parameter from the cache
|
13
|
+
const grantor = (0, parameter_cache_1.get)(name);
|
14
|
+
// Add a policy to the grantee's principal that allows 'ssm:GetParameter' action on the SSM parameter
|
15
|
+
grantee.grantPrincipal.addToPrincipalPolicy(new aws_iam_1.PolicyStatement({
|
16
|
+
effect: aws_iam_1.Effect.ALLOW,
|
17
|
+
actions: ['ssm:GetParameter'],
|
18
|
+
resources: [grantor.parameterArn]
|
19
|
+
}));
|
20
|
+
// If the SSM parameter is encrypted with a key, grant the grantee decrypt permissions on the key
|
21
|
+
if (grantor.decryptWithKey)
|
22
|
+
grantor.decryptWithKey.grantDecrypt(grantee);
|
23
|
+
}
|
24
|
+
exports.grantRead = grantRead;
|
package/lib/ssm/index.js
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.stringValue = exports.grantRead = exports.getExistingParameter = void 0;
|
4
|
+
var get_existing_parameter_1 = require("./get-existing-parameter");
|
5
|
+
Object.defineProperty(exports, "getExistingParameter", { enumerable: true, get: function () { return get_existing_parameter_1.getExistingParameter; } });
|
6
|
+
var grant_read_1 = require("./grant-read");
|
7
|
+
Object.defineProperty(exports, "grantRead", { enumerable: true, get: function () { return grant_read_1.grantRead; } });
|
8
|
+
var string_value_1 = require("./string-value");
|
9
|
+
Object.defineProperty(exports, "stringValue", { enumerable: true, get: function () { return string_value_1.stringValue; } });
|
@@ -0,0 +1,13 @@
|
|
1
|
+
import { SsmParameter } from "./SsmParameter";
|
2
|
+
/**
|
3
|
+
* Retrieves an SSM parameter from the cache.
|
4
|
+
* @param {string} name - The name of the SSM parameter.
|
5
|
+
* @returns {SsmParameter} The SSM parameter.
|
6
|
+
*/
|
7
|
+
export declare function get(name: string): SsmParameter;
|
8
|
+
/**
|
9
|
+
* Stores an SSM parameter in the cache.
|
10
|
+
* @param {string} name - The name of the SSM parameter.
|
11
|
+
* @param {SsmParameter} param - The SSM parameter to store.
|
12
|
+
*/
|
13
|
+
export declare function store(name: string, param: SsmParameter): void;
|
@@ -0,0 +1,23 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.store = exports.get = void 0;
|
4
|
+
// Cache for storing SSM parameters
|
5
|
+
const paramCache = {};
|
6
|
+
/**
|
7
|
+
* Retrieves an SSM parameter from the cache.
|
8
|
+
* @param {string} name - The name of the SSM parameter.
|
9
|
+
* @returns {SsmParameter} The SSM parameter.
|
10
|
+
*/
|
11
|
+
function get(name) {
|
12
|
+
return paramCache[name];
|
13
|
+
}
|
14
|
+
exports.get = get;
|
15
|
+
/**
|
16
|
+
* Stores an SSM parameter in the cache.
|
17
|
+
* @param {string} name - The name of the SSM parameter.
|
18
|
+
* @param {SsmParameter} param - The SSM parameter to store.
|
19
|
+
*/
|
20
|
+
function store(name, param) {
|
21
|
+
paramCache[name] = param;
|
22
|
+
}
|
23
|
+
exports.store = store;
|
@@ -0,0 +1,8 @@
|
|
1
|
+
import { SsmParameterOptions } from "./SsmParameter";
|
2
|
+
/**
|
3
|
+
* Retrieves the string value of an existing SSM parameter.
|
4
|
+
* @param {string} name - The name of the SSM parameter.
|
5
|
+
* @param {SsmParameterOptions} [options] - The options for the SSM parameter.
|
6
|
+
* @returns {string} The string value of the SSM parameter.
|
7
|
+
*/
|
8
|
+
export declare function stringValue(name: string, options?: SsmParameterOptions): string;
|
@@ -0,0 +1,14 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.stringValue = void 0;
|
4
|
+
const get_existing_parameter_1 = require("./get-existing-parameter");
|
5
|
+
/**
|
6
|
+
* Retrieves the string value of an existing SSM parameter.
|
7
|
+
* @param {string} name - The name of the SSM parameter.
|
8
|
+
* @param {SsmParameterOptions} [options] - The options for the SSM parameter.
|
9
|
+
* @returns {string} The string value of the SSM parameter.
|
10
|
+
*/
|
11
|
+
function stringValue(name, options) {
|
12
|
+
return (0, get_existing_parameter_1.getExistingParameter)(name, options).parameter.stringValue;
|
13
|
+
}
|
14
|
+
exports.stringValue = stringValue;
|
@@ -0,0 +1,10 @@
|
|
1
|
+
/**
|
2
|
+
* Creates a function that, when called, returns the elapsed time in milliseconds since the function was created.
|
3
|
+
* @function hrDuration
|
4
|
+
* @returns {function} - A function that returns the elapsed time in milliseconds.
|
5
|
+
*/
|
6
|
+
export declare const hrDuration: () => {
|
7
|
+
(): number;
|
8
|
+
time: [number, number];
|
9
|
+
};
|
10
|
+
export default hrDuration;
|
@@ -0,0 +1,28 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.hrDuration = void 0;
|
4
|
+
/**
|
5
|
+
* Creates a function that, when called, returns the elapsed time in milliseconds since the function was created.
|
6
|
+
* @function hrDuration
|
7
|
+
* @returns {function} - A function that returns the elapsed time in milliseconds.
|
8
|
+
*/
|
9
|
+
const hrDuration = () => {
|
10
|
+
// The high-resolution real time at which the function was created
|
11
|
+
const startTime = process.hrtime();
|
12
|
+
/**
|
13
|
+
* Returns the elapsed time in milliseconds since the function was created.
|
14
|
+
* @function duration
|
15
|
+
* @returns {number} - The elapsed time in milliseconds.
|
16
|
+
*/
|
17
|
+
const onEnd = function duration() {
|
18
|
+
// The high-resolution real time at which the function was called
|
19
|
+
const hrTime = process.hrtime(startTime);
|
20
|
+
// Calculate and return the elapsed time in milliseconds
|
21
|
+
return hrTime[0] * 1000 + hrTime[1] / 1000000;
|
22
|
+
};
|
23
|
+
// Attach the start time to the function for reference
|
24
|
+
onEnd.time = startTime;
|
25
|
+
return onEnd;
|
26
|
+
};
|
27
|
+
exports.hrDuration = hrDuration;
|
28
|
+
exports.default = exports.hrDuration;
|
package/package.json
CHANGED
@@ -1,12 +1,15 @@
|
|
1
1
|
{
|
2
2
|
"name": "@byaga/cdk-patterns",
|
3
|
-
"version": "0.
|
3
|
+
"version": "0.11.0",
|
4
4
|
"description": "Collection of common patterns used when making AWS CloudFormation templates using CDK",
|
5
|
-
"main": "lib/index.js",
|
6
|
-
"types": "lib/index.d.ts",
|
5
|
+
"main": "./lib/index.js",
|
6
|
+
"types": "./lib/index.d.ts",
|
7
7
|
"scripts": {
|
8
|
-
"
|
9
|
-
"
|
8
|
+
"clean": "rimraf ./lib",
|
9
|
+
"build": "npm run clean && tsc",
|
10
|
+
"lint": "eslint . --ext .ts",
|
11
|
+
"test": "jest ./src",
|
12
|
+
"preversion": "npm run lint && npm test && npm run build"
|
10
13
|
},
|
11
14
|
"keywords": [
|
12
15
|
"CDK",
|
@@ -16,19 +19,30 @@
|
|
16
19
|
"author": "VeryFineHat",
|
17
20
|
"license": "MIT",
|
18
21
|
"dependencies": {
|
19
|
-
"
|
20
|
-
"aws-cdk": "^2.
|
21
|
-
"aws-cdk-lib": "^2.103.1",
|
22
|
+
"aws-cdk": "^2.122.0",
|
23
|
+
"aws-cdk-lib": "^2.122.0",
|
22
24
|
"constructs": "^10.3.0",
|
23
|
-
"fs-extra": "^11.
|
25
|
+
"fs-extra": "^11.2.0",
|
26
|
+
"glob": "^10.3.10",
|
27
|
+
"ignore": "^5.3.0"
|
24
28
|
},
|
25
29
|
"devDependencies": {
|
30
|
+
"@types/fs-extra": "^11.0.4",
|
31
|
+
"@types/glob": "^8.1.0",
|
26
32
|
"@types/jest": "^29.5.11",
|
27
|
-
"@types/node": "^20.
|
33
|
+
"@types/node": "^20.11.5",
|
34
|
+
"@typescript-eslint/eslint-plugin": "^6.19.0",
|
35
|
+
"@typescript-eslint/parser": "^6.19.0",
|
36
|
+
"eslint": "^8.56.0",
|
37
|
+
"jest": "^29.7.0",
|
28
38
|
"jest-html-reporters": "^3.1.7",
|
29
39
|
"jest-junit": "^16.0.0",
|
30
40
|
"ts-jest": "^29.1.1",
|
31
41
|
"ts-mockito": "^2.6.1",
|
32
|
-
"typescript": "^5.
|
42
|
+
"typescript": "^5.3.3"
|
43
|
+
},
|
44
|
+
"repository": {
|
45
|
+
"type": "git",
|
46
|
+
"url": "https://github.com/veryfine-hat/byaga-cdk-patterns"
|
33
47
|
}
|
34
48
|
}
|
package/jest.config.js
DELETED
@@ -1,16 +0,0 @@
|
|
1
|
-
module.exports = {
|
2
|
-
preset: 'ts-jest',
|
3
|
-
testEnvironment: 'node',
|
4
|
-
reporters: [
|
5
|
-
"default",
|
6
|
-
["jest-junit", {
|
7
|
-
outputDirectory: './test-results/junit',
|
8
|
-
outputName: 'junit.xml',
|
9
|
-
}],
|
10
|
-
["jest-html-reporters", {
|
11
|
-
publicPath: './test-results/html',
|
12
|
-
filename: 'index.html',
|
13
|
-
expand: true
|
14
|
-
}]
|
15
|
-
]
|
16
|
-
};
|
package/lib/ApiCertificate.d.ts
DELETED
@@ -1,10 +0,0 @@
|
|
1
|
-
import { Certificate } from "aws-cdk-lib/aws-certificatemanager";
|
2
|
-
import { IHostedZone } from "aws-cdk-lib/aws-route53";
|
3
|
-
import DeployStack from "./DeployStack";
|
4
|
-
import IDomainConfig from "./IDomainConfig";
|
5
|
-
export declare class ApiCertificate extends Certificate {
|
6
|
-
hostedZone: IHostedZone;
|
7
|
-
domain: string;
|
8
|
-
constructor(stack: DeployStack, id: string, domain: IDomainConfig);
|
9
|
-
}
|
10
|
-
export default ApiCertificate;
|
package/lib/ApiCertificate.js
DELETED
@@ -1,31 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.ApiCertificate = void 0;
|
4
|
-
const aws_certificatemanager_1 = require("aws-cdk-lib/aws-certificatemanager");
|
5
|
-
const aws_route53_1 = require("aws-cdk-lib/aws-route53");
|
6
|
-
const Output_1 = require("./Output");
|
7
|
-
class ApiCertificate extends aws_certificatemanager_1.Certificate {
|
8
|
-
hostedZone;
|
9
|
-
domain;
|
10
|
-
constructor(stack, id, domain) {
|
11
|
-
const certDomain = [domain.domainName];
|
12
|
-
if (domain.subdomain)
|
13
|
-
certDomain.splice(0, 0, domain.subdomain);
|
14
|
-
const domainName = certDomain.join('.');
|
15
|
-
console.log('Getting Hosted Zone', domain.hostedZone.name);
|
16
|
-
const hostedZone = aws_route53_1.HostedZone.fromHostedZoneAttributes(stack, stack.genId(id, 'hosted-zone'), {
|
17
|
-
hostedZoneId: domain.hostedZone.id,
|
18
|
-
zoneName: domain.hostedZone.name
|
19
|
-
});
|
20
|
-
console.log('Defining Certificate For', domainName);
|
21
|
-
super(stack, stack.genId(id), {
|
22
|
-
domainName,
|
23
|
-
validation: aws_certificatemanager_1.CertificateValidation.fromDns(hostedZone)
|
24
|
-
});
|
25
|
-
this.domain = domainName;
|
26
|
-
new Output_1.Output(stack, id, this.certificateArn);
|
27
|
-
this.hostedZone = hostedZone;
|
28
|
-
}
|
29
|
-
}
|
30
|
-
exports.ApiCertificate = ApiCertificate;
|
31
|
-
exports.default = ApiCertificate;
|
@@ -1,24 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.CognitoApiGatewayAuthorizer = void 0;
|
4
|
-
const aws_apigateway_1 = require("aws-cdk-lib/aws-apigateway");
|
5
|
-
/**
|
6
|
-
* Custom construct that implements a Cognito based API Gateway Authorizer.
|
7
|
-
*
|
8
|
-
* @see https://docs.aws.amazon.com/cdk/latest/guide/constructs.html#constructs_author
|
9
|
-
*
|
10
|
-
* @see https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-apigateway.CfnAuthorizer.html
|
11
|
-
* @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-apigateway-authorizer.html
|
12
|
-
* @see https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-integrate-with-cognito.html
|
13
|
-
* @see https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-enable-cognito-user-pool.html
|
14
|
-
*
|
15
|
-
* @see https://github.com/aws/aws-cdk/issues/5618#issuecomment-666922559
|
16
|
-
*/
|
17
|
-
class CognitoApiGatewayAuthorizer extends aws_apigateway_1.CfnAuthorizer {
|
18
|
-
authorizerId;
|
19
|
-
constructor(scope, id, props) {
|
20
|
-
super(scope, id, props);
|
21
|
-
this.authorizerId = this.ref;
|
22
|
-
}
|
23
|
-
}
|
24
|
-
exports.CognitoApiGatewayAuthorizer = CognitoApiGatewayAuthorizer;
|