@sylvesterllc/aws-constructs 1.1.11 → 1.1.13
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/.github/workflows/publish.yml +58 -38
- package/__tests__/app-config.test.ts +32 -32
- package/__tests__/data/testConfig.ts +146 -146
- package/__tests__/mocks/ResourceMock.ts +9 -9
- package/__tests__/router.test.ts +194 -194
- package/dist/config/AppConfig.js +1 -1
- package/dist/config/Constants.js +1 -1
- package/dist/config/Environments.js +1 -1
- package/dist/config/MicroserviceAppConfig.js +1 -1
- package/dist/config/ServiceBundleConfig.js +1 -1
- package/dist/config/bishopConfig.js +1 -1
- package/dist/config/customConfigs/ApiAppConfig.js +1 -1
- package/dist/config/customConfigs/GlobalAppConfig.js +1 -1
- package/dist/config/customConfigs/IAppConfig.js +1 -1
- package/dist/config/customConfigs/ResourceAppConfig.js +1 -1
- package/dist/config/microServiceConfig.js +1 -1
- package/dist/config/types/LogDuration.js +1 -1
- package/dist/config/types/TsgAuthorizerProp.js +1 -1
- package/dist/config/types/TsgAuthorizerType.js +1 -1
- package/dist/config/types/TsgCdkConfigType.js +1 -1
- package/dist/config/types/TsgCognitoConfig.js +1 -1
- package/dist/config/types/TsgDnsConfig.js +1 -1
- package/dist/config/types/TsgDynamoDbProp.js +1 -1
- package/dist/config/types/TsgDynamoIndex.js +1 -1
- package/dist/config/types/TsgDynamoProp.js +1 -1
- package/dist/config/types/TsgDynamoTable.js +1 -1
- package/dist/config/types/TsgDynamoTableRef.js +1 -1
- package/dist/config/types/TsgEc2Prop.js +1 -1
- package/dist/config/types/TsgKeyPair.js +1 -1
- package/dist/config/types/TsgLambda.js +1 -1
- package/dist/config/types/TsgLambdaLayerProp.js +1 -1
- package/dist/config/types/TsgLambdaMap.js +1 -1
- package/dist/config/types/TsgLambdaName.js +1 -1
- package/dist/config/types/TsgLambdaNode.js +1 -1
- package/dist/config/types/TsgLambdaProp.js +1 -1
- package/dist/config/types/TsgLambdaProps.js +1 -1
- package/dist/config/types/TsgLambdaRoutable.js +1 -1
- package/dist/config/types/index.js +1 -1
- package/dist/constants/aws-service-principal-constants.d.ts +3 -0
- package/dist/constants/aws-service-principal-constants.js +7 -0
- package/dist/constructs/DynamoDbSingleTable.js +1 -1
- package/dist/constructs/MicroService.js +1 -1
- package/dist/constructs/index.d.ts +1 -0
- package/dist/constructs/index.js +4 -2
- package/dist/constructs/timer-job.d.ts +6 -0
- package/dist/constructs/timer-job.js +15 -0
- package/dist/helpers/logger.js +1 -1
- package/dist/helpers/util-helper.js +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +18 -4
- package/dist/interfaces/ApiLambdaResult.js +1 -1
- package/dist/interfaces/BaseResourceProps.js +1 -1
- package/dist/interfaces/CreateLambdaFunctionInput.js +1 -1
- package/dist/interfaces/MicroserviceProps.js +1 -1
- package/dist/interfaces/timer-job/index.d.ts +1 -0
- package/dist/interfaces/timer-job/index.js +18 -0
- package/dist/interfaces/timer-job/timer-job-props.d.ts +14 -0
- package/dist/interfaces/timer-job/timer-job-props.js +3 -0
- package/dist/resources/base/baseResource.js +1 -1
- package/dist/resources/base/tsgBaseResource.js +1 -1
- package/dist/resources/certificate/createCertificate.js +1 -1
- package/dist/resources/cognito/createCognito.js +1 -1
- package/dist/resources/cognito/createCognitoProps.js +1 -1
- package/dist/resources/customResource/createSeedDatabaseCustomResource.js +1 -1
- package/dist/resources/dynamodb/CreateDynamo.js +1 -1
- package/dist/resources/dynamodb/CreateDynamoSingleTableDesign.js +1 -1
- package/dist/resources/ec2/create-ec2-instance.js +3 -3
- package/dist/resources/ecr/CreateEcr.js +2 -2
- package/dist/resources/gateway/CreateApiAndAttachLambdas.js +1 -1
- package/dist/resources/gateway/createApi.js +1 -1
- package/dist/resources/gateway/createApiKey.js +1 -1
- package/dist/resources/helpers/createCommonLayer.js +1 -1
- package/dist/resources/helpers/createRoutes.js +1 -1
- package/dist/resources/index.js +1 -1
- package/dist/resources/lambda/create-basic-lambda.d.ts +3 -0
- package/dist/resources/lambda/create-basic-lambda.js +77 -0
- package/dist/resources/lambda/createLambda.js +1 -1
- package/dist/resources/lambda/lambdaHelper.js +1 -1
- package/dist/resources/lambda-authorizer/TsgJwtTokenAuthorizer.js +1 -1
- package/dist/resources/lambda-authorizer/TsgRequestAuthorizer.js +1 -1
- package/dist/resources/lambda-authorizer/createAuthorizerHelpers.js +1 -1
- package/dist/resources/lambda-layer/createLambdaLayer.js +1 -1
- package/dist/resources/layers/common/src/nodejs/api-response/responses.js +1 -1
- package/dist/resources/layers/common/src/nodejs/index.js +1 -1
- package/dist/resources/layers/common/src/nodejs/logger/IContext.js +1 -1
- package/dist/resources/layers/common/src/nodejs/logger/index.js +1 -1
- package/dist/resources/layers/common/src/nodejs/logger/logger.js +1 -1
- package/dist/resources/resources/iam/policyDocuments/cognitoPolicy.js +1 -1
- package/dist/resources/resources/iam/roles/createRole.js +1 -1
- package/dist/resources/route53/CreateZone.js +1 -1
- package/dist/resources/route53/create-zones.js +1 -1
- package/dist/resources/securityManager/index.js +1 -1
- package/jest.config.js +8 -8
- package/jest.config.ts.old +20 -20
- package/package.json +55 -56
- package/readme.md +147 -147
- package/src/config/AppConfig.ts +106 -106
- package/src/config/Constants.ts +26 -26
- package/src/config/Environments.ts +1 -1
- package/src/config/MicroserviceAppConfig.ts +8 -8
- package/src/config/ServiceBundleConfig.ts +17 -17
- package/src/config/bishopConfig.ts +82 -82
- package/src/config/customConfigs/ApiAppConfig.ts +5 -5
- package/src/config/customConfigs/GlobalAppConfig.ts +10 -10
- package/src/config/customConfigs/IAppConfig.ts +12 -12
- package/src/config/customConfigs/ResourceAppConfig.ts +15 -15
- package/src/config/microServiceConfig.ts +83 -83
- package/src/config/types/LogDuration.ts +7 -7
- package/src/config/types/TsgAuthorizerProp.ts +8 -8
- package/src/config/types/TsgAuthorizerType.ts +4 -4
- package/src/config/types/TsgCdkConfigType.ts +2 -2
- package/src/config/types/TsgCognitoConfig.ts +5 -5
- package/src/config/types/TsgDnsConfig.ts +10 -10
- package/src/config/types/TsgDynamoDbProp.ts +9 -9
- package/src/config/types/TsgDynamoIndex.ts +15 -15
- package/src/config/types/TsgDynamoProp.ts +16 -16
- package/src/config/types/TsgDynamoTable.ts +4 -4
- package/src/config/types/TsgDynamoTableRef.ts +3 -3
- package/src/config/types/TsgEc2Prop.ts +8 -8
- package/src/config/types/TsgKeyPair.ts +5 -5
- package/src/config/types/TsgLambda.ts +5 -5
- package/src/config/types/TsgLambdaLayerProp.ts +4 -4
- package/src/config/types/TsgLambdaMap.ts +3 -3
- package/src/config/types/TsgLambdaName.ts +3 -3
- package/src/config/types/TsgLambdaNode.ts +3 -3
- package/src/config/types/TsgLambdaProp.ts +19 -19
- package/src/config/types/TsgLambdaProps.ts +17 -17
- package/src/config/types/TsgLambdaRoutable.ts +8 -8
- package/src/config/types/index.ts +11 -11
- package/src/constants/aws-service-principal-constants.ts +3 -0
- package/src/constructs/DynamoDbSingleTable.ts +12 -12
- package/src/constructs/MicroService.ts +131 -132
- package/src/constructs/index.ts +4 -3
- package/src/constructs/timer-job.ts +17 -0
- package/src/helpers/logger.ts +12 -12
- package/src/helpers/util-helper.ts +19 -19
- package/src/index.ts +6 -5
- package/src/interfaces/ApiLambdaResult.ts +6 -6
- package/src/interfaces/BaseResourceProps.ts +6 -6
- package/src/interfaces/CreateLambdaFunctionInput.ts +10 -10
- package/src/interfaces/MicroserviceProps.ts +10 -10
- package/src/interfaces/timer-job/index.ts +1 -0
- package/src/interfaces/timer-job/timer-job-props.ts +17 -0
- package/src/resources/base/baseResource.ts +14 -14
- package/src/resources/base/tsgBaseResource.ts +21 -21
- package/src/resources/certificate/createCertificate.ts +57 -57
- package/src/resources/cognito/createCognito.ts +47 -47
- package/src/resources/cognito/createCognitoProps.ts +46 -46
- package/src/resources/customResource/createSeedDatabaseCustomResource.ts +36 -36
- package/src/resources/dynamodb/CreateDynamo.ts +110 -110
- package/src/resources/dynamodb/CreateDynamoSingleTableDesign.ts +111 -111
- package/src/resources/ec2/create-ec2-instance.ts +52 -52
- package/src/resources/ecr/CreateEcr.ts +57 -57
- package/src/resources/gateway/CreateApiAndAttachLambdas.ts +161 -161
- package/src/resources/gateway/createApi.ts +173 -173
- package/src/resources/gateway/createApiKey.ts +88 -88
- package/src/resources/helpers/createCommonLayer.ts +18 -18
- package/src/resources/helpers/createRoutes.ts +74 -74
- package/src/resources/index.ts +1 -1
- package/src/resources/lambda/create-basic-lambda.ts +104 -0
- package/src/resources/lambda/createLambda.ts +203 -200
- package/src/resources/lambda/lambdaHelper.ts +133 -133
- package/src/resources/lambda-authorizer/TsgJwtTokenAuthorizer.ts +107 -107
- package/src/resources/lambda-authorizer/TsgRequestAuthorizer.ts +44 -44
- package/src/resources/lambda-authorizer/createAuthorizerHelpers.ts +76 -76
- package/src/resources/lambda-layer/createLambdaLayer.ts +39 -39
- package/src/resources/layers/common/package.json +22 -22
- package/src/resources/layers/common/src/nodejs/api-response/responses.ts +41 -41
- package/src/resources/layers/common/src/nodejs/index.ts +3 -3
- package/src/resources/layers/common/src/nodejs/logger/IContext.ts +6 -6
- package/src/resources/layers/common/src/nodejs/logger/index.ts +1 -1
- package/src/resources/layers/common/src/nodejs/logger/logger.ts +10 -10
- package/src/resources/layers/common/tsconfig.json +7 -7
- package/src/resources/resources/iam/policyDocuments/cognitoPolicy.ts +54 -54
- package/src/resources/resources/iam/roles/createRole.ts +29 -29
- package/src/resources/route53/CreateZone.ts +61 -61
- package/src/resources/route53/create-zones.ts +40 -40
- package/src/resources/securityManager/index.ts +23 -23
- package/tsconfig.json +44 -42
|
@@ -1,89 +1,89 @@
|
|
|
1
|
-
import { Construct } from "constructs";
|
|
2
|
-
import { BaseResource } from "../base/baseResource";
|
|
3
|
-
import { ApiKeyProps, IApiKey, ApiKey as AWS_API_KEY, IRestApi, RestApi } from "aws-cdk-lib/aws-apigateway";
|
|
4
|
-
import { AppConfig } from "../../config/AppConfig";
|
|
5
|
-
import { CfnOutput, RemovalPolicy } from "aws-cdk-lib";
|
|
6
|
-
import { UsagePlanProps } from "aws-cdk-lib/aws-apigateway";
|
|
7
|
-
|
|
8
|
-
export class TsgApiKey extends BaseResource<AWS_API_KEY> {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
private api: RestApi;
|
|
12
|
-
private apiKey: IApiKey;
|
|
13
|
-
|
|
14
|
-
constructor(scope: Construct, config: AppConfig, api: RestApi) {
|
|
15
|
-
super(scope, config);
|
|
16
|
-
|
|
17
|
-
this.api = api;
|
|
18
|
-
|
|
19
|
-
this.createdResources = this.createResource(scope);
|
|
20
|
-
|
|
21
|
-
this.applyRemovalPolicy();
|
|
22
|
-
|
|
23
|
-
this.createOutput(scope, this.createdResources);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
get APIKey() {
|
|
27
|
-
return this.apiKey;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
public createResource(scope: Construct): AWS_API_KEY[] {
|
|
31
|
-
|
|
32
|
-
const keyProps: ApiKeyProps = {
|
|
33
|
-
apiKeyName: `${this.config.AppPrefix}-x-api-key`,
|
|
34
|
-
resources: [this.api],
|
|
35
|
-
value: `ddGTsLQrae2U6zTEjw4M07qO4pHGt60u73zImLHE`,
|
|
36
|
-
enabled: true,
|
|
37
|
-
description: `API App Key for ${this.config.AppName}`,
|
|
38
|
-
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
const apiKey = new AWS_API_KEY(scope, `${this.config.AppPrefix}-api-key`, keyProps);
|
|
42
|
-
|
|
43
|
-
this.createUsagePlan(scope, this.config, this.api, apiKey);
|
|
44
|
-
|
|
45
|
-
return [apiKey];
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
protected createOutput<T>(scope: Construct, createdAssets: T[]): void {
|
|
49
|
-
this.createdResources.forEach((key, idx) => {
|
|
50
|
-
|
|
51
|
-
new CfnOutput(scope, `api-key-${idx}`, {
|
|
52
|
-
value: `${key.keyId}\t${key.keyArn}`
|
|
53
|
-
});
|
|
54
|
-
});
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
private applyRemovalPolicy() {
|
|
58
|
-
|
|
59
|
-
const apiKey = this.createdResources[0];
|
|
60
|
-
|
|
61
|
-
if (apiKey) {
|
|
62
|
-
apiKey.applyRemovalPolicy(RemovalPolicy.DESTROY);
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
private createUsagePlan(scope: Construct, config: AppConfig, api: RestApi, apiKey: AWS_API_KEY) {
|
|
67
|
-
|
|
68
|
-
const planProps: UsagePlanProps = {
|
|
69
|
-
name: `${config.AppName}-easy-plan`,
|
|
70
|
-
throttle: {
|
|
71
|
-
rateLimit: 1000,
|
|
72
|
-
burstLimit: 10,
|
|
73
|
-
},
|
|
74
|
-
apiStages: [
|
|
75
|
-
{
|
|
76
|
-
api: api,
|
|
77
|
-
stage: api.deploymentStage,
|
|
78
|
-
},
|
|
79
|
-
],
|
|
80
|
-
};
|
|
81
|
-
|
|
82
|
-
const plan = api.addUsagePlan(
|
|
83
|
-
`${config.AppPrefix}-${config.AppName}-usage-plan`,
|
|
84
|
-
planProps
|
|
85
|
-
);
|
|
86
|
-
|
|
87
|
-
plan.addApiKey(apiKey);
|
|
88
|
-
}
|
|
1
|
+
import { Construct } from "constructs";
|
|
2
|
+
import { BaseResource } from "../base/baseResource";
|
|
3
|
+
import { ApiKeyProps, IApiKey, ApiKey as AWS_API_KEY, IRestApi, RestApi } from "aws-cdk-lib/aws-apigateway";
|
|
4
|
+
import { AppConfig } from "../../config/AppConfig";
|
|
5
|
+
import { CfnOutput, RemovalPolicy } from "aws-cdk-lib";
|
|
6
|
+
import { UsagePlanProps } from "aws-cdk-lib/aws-apigateway";
|
|
7
|
+
|
|
8
|
+
export class TsgApiKey extends BaseResource<AWS_API_KEY> {
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
private api: RestApi;
|
|
12
|
+
private apiKey: IApiKey;
|
|
13
|
+
|
|
14
|
+
constructor(scope: Construct, config: AppConfig, api: RestApi) {
|
|
15
|
+
super(scope, config);
|
|
16
|
+
|
|
17
|
+
this.api = api;
|
|
18
|
+
|
|
19
|
+
this.createdResources = this.createResource(scope);
|
|
20
|
+
|
|
21
|
+
this.applyRemovalPolicy();
|
|
22
|
+
|
|
23
|
+
this.createOutput(scope, this.createdResources);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
get APIKey() {
|
|
27
|
+
return this.apiKey;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
public createResource(scope: Construct): AWS_API_KEY[] {
|
|
31
|
+
|
|
32
|
+
const keyProps: ApiKeyProps = {
|
|
33
|
+
apiKeyName: `${this.config.AppPrefix}-x-api-key`,
|
|
34
|
+
resources: [this.api],
|
|
35
|
+
value: `ddGTsLQrae2U6zTEjw4M07qO4pHGt60u73zImLHE`,
|
|
36
|
+
enabled: true,
|
|
37
|
+
description: `API App Key for ${this.config.AppName}`,
|
|
38
|
+
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const apiKey = new AWS_API_KEY(scope, `${this.config.AppPrefix}-api-key`, keyProps);
|
|
42
|
+
|
|
43
|
+
this.createUsagePlan(scope, this.config, this.api, apiKey);
|
|
44
|
+
|
|
45
|
+
return [apiKey];
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
protected createOutput<T>(scope: Construct, createdAssets: T[]): void {
|
|
49
|
+
this.createdResources.forEach((key, idx) => {
|
|
50
|
+
|
|
51
|
+
new CfnOutput(scope, `api-key-${idx}`, {
|
|
52
|
+
value: `${key.keyId}\t${key.keyArn}`
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
private applyRemovalPolicy() {
|
|
58
|
+
|
|
59
|
+
const apiKey = this.createdResources[0];
|
|
60
|
+
|
|
61
|
+
if (apiKey) {
|
|
62
|
+
apiKey.applyRemovalPolicy(RemovalPolicy.DESTROY);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
private createUsagePlan(scope: Construct, config: AppConfig, api: RestApi, apiKey: AWS_API_KEY) {
|
|
67
|
+
|
|
68
|
+
const planProps: UsagePlanProps = {
|
|
69
|
+
name: `${config.AppName}-easy-plan`,
|
|
70
|
+
throttle: {
|
|
71
|
+
rateLimit: 1000,
|
|
72
|
+
burstLimit: 10,
|
|
73
|
+
},
|
|
74
|
+
apiStages: [
|
|
75
|
+
{
|
|
76
|
+
api: api,
|
|
77
|
+
stage: api.deploymentStage,
|
|
78
|
+
},
|
|
79
|
+
],
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
const plan = api.addUsagePlan(
|
|
83
|
+
`${config.AppPrefix}-${config.AppName}-usage-plan`,
|
|
84
|
+
planProps
|
|
85
|
+
);
|
|
86
|
+
|
|
87
|
+
plan.addApiKey(apiKey);
|
|
88
|
+
}
|
|
89
89
|
}
|
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
import { Construct } from "constructs";
|
|
2
|
-
import { createLambdaLayer } from "../lambda-layer/createLambdaLayer";
|
|
3
|
-
import { TsgLambdaLayerProp } from "../../config/types/TsgLambdaLayerProp";
|
|
4
|
-
import { AppConfig } from "../../config/AppConfig";
|
|
5
|
-
|
|
6
|
-
export const createCommonLayer = (scope: Construct, appProps: AppConfig) => {
|
|
7
|
-
|
|
8
|
-
const prop: TsgLambdaLayerProp = {
|
|
9
|
-
description: appProps.RESOURCES.LAMBDA_LAYERS![0].description,
|
|
10
|
-
codePath: appProps.RESOURCES.LAMBDA_LAYERS![0].codePath,
|
|
11
|
-
name: appProps.RESOURCES.LAMBDA_LAYERS![0].name,
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
const layer = createLambdaLayer(scope, appProps, prop)
|
|
15
|
-
|
|
16
|
-
return [layer];
|
|
17
|
-
};
|
|
18
|
-
|
|
1
|
+
import { Construct } from "constructs";
|
|
2
|
+
import { createLambdaLayer } from "../lambda-layer/createLambdaLayer";
|
|
3
|
+
import { TsgLambdaLayerProp } from "../../config/types/TsgLambdaLayerProp";
|
|
4
|
+
import { AppConfig } from "../../config/AppConfig";
|
|
5
|
+
|
|
6
|
+
export const createCommonLayer = (scope: Construct, appProps: AppConfig) => {
|
|
7
|
+
|
|
8
|
+
const prop: TsgLambdaLayerProp = {
|
|
9
|
+
description: appProps.RESOURCES.LAMBDA_LAYERS![0].description,
|
|
10
|
+
codePath: appProps.RESOURCES.LAMBDA_LAYERS![0].codePath,
|
|
11
|
+
name: appProps.RESOURCES.LAMBDA_LAYERS![0].name,
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
const layer = createLambdaLayer(scope, appProps, prop)
|
|
15
|
+
|
|
16
|
+
return [layer];
|
|
17
|
+
};
|
|
18
|
+
|
|
@@ -1,75 +1,75 @@
|
|
|
1
|
-
import { AuthorizationType, IRestApi, LambdaIntegration, RequestAuthorizer, Resource, TokenAuthorizer } from "aws-cdk-lib/aws-apigateway";
|
|
2
|
-
import { NodejsFunction } from "aws-cdk-lib/aws-lambda-nodejs";
|
|
3
|
-
import { TsgLambdaProp } from "../../config/types";
|
|
4
|
-
import { Logger } from "../../helpers/logger";
|
|
5
|
-
import { logger } from "../layers/common/src/nodejs";
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
export class Routes {
|
|
9
|
-
|
|
10
|
-
public static Resources: Resource[] = [];
|
|
11
|
-
private static routeMap = new Map();
|
|
12
|
-
|
|
13
|
-
public static createResource(
|
|
14
|
-
prop: TsgLambdaProp,
|
|
15
|
-
api: IRestApi,
|
|
16
|
-
lambdaNode: NodejsFunction,
|
|
17
|
-
authorizer?: TokenAuthorizer | RequestAuthorizer,
|
|
18
|
-
): void {
|
|
19
|
-
|
|
20
|
-
const routeMap: Map<string, Resource> = new Map();
|
|
21
|
-
|
|
22
|
-
let activeRoutePath = "";
|
|
23
|
-
let activeResource: Resource | undefined = undefined;
|
|
24
|
-
|
|
25
|
-
// Only attach lambda to an Api Gateway if a route exist
|
|
26
|
-
if (prop.apiGateway?.route) {
|
|
27
|
-
|
|
28
|
-
if (!prop.apiGateway.useRouteOverride) {
|
|
29
|
-
// First we create the root resource if it doesn't exist.
|
|
30
|
-
// Note: this now uses the bundle version as the first segment in the path.
|
|
31
|
-
activeRoutePath = `v${(prop.apiGateway?.version) ? prop.apiGateway.version : 1}`;
|
|
32
|
-
activeResource = Routes.routeMap.get(activeRoutePath) || api.root.addResource(activeRoutePath);
|
|
33
|
-
Routes.routeMap.set(activeRoutePath, activeResource);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
// Now we go through our route segments creating the rest of the path.
|
|
38
|
-
const pathSegments = prop.apiGateway?.route.split("/").filter(x => (x));
|
|
39
|
-
|
|
40
|
-
for (let i = 0; i < pathSegments.length; i++) {
|
|
41
|
-
activeRoutePath = `${activeRoutePath}/${pathSegments[i]}`;
|
|
42
|
-
let secondaryResource = Routes.routeMap.get(activeRoutePath) || activeResource?.addResource(pathSegments[i]) || api.root.addResource(pathSegments[i]);
|
|
43
|
-
Routes.routeMap.set(activeRoutePath, secondaryResource);
|
|
44
|
-
activeResource = secondaryResource;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
// console.log(`Route: ${prop.apiGateway.method}\t Lambda Name: ${lambdaNode.node.id}\t Path: ${activeRoutePath}`);
|
|
48
|
-
// Finally, we attach our function to the last resource
|
|
49
|
-
activeResource?.addMethod(prop.apiGateway.method || 'GET',
|
|
50
|
-
new LambdaIntegration(lambdaNode, { proxy: true, }),
|
|
51
|
-
{
|
|
52
|
-
requestParameters: Routes.createQueryStringObject(prop.apiGateway.queryStrings),
|
|
53
|
-
apiKeyRequired: (prop.apiGateway.requireApiKey) ? true : false,
|
|
54
|
-
authorizer: prop.apiGateway.secure ? authorizer : undefined,
|
|
55
|
-
authorizationType: prop.apiGateway.secure ? AuthorizationType.CUSTOM : undefined,
|
|
56
|
-
});
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
public static createQueryStringObject(queryStrings: string[] | undefined): { [key: string]: boolean } {
|
|
61
|
-
|
|
62
|
-
if (!queryStrings || queryStrings.length === 0) {
|
|
63
|
-
return {};
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
const obj: { [key: string]: boolean } = {};
|
|
67
|
-
|
|
68
|
-
queryStrings.forEach((qs: string) => {
|
|
69
|
-
obj[`method.request.querystring.${qs}`] = true;
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
return obj;
|
|
73
|
-
}
|
|
74
|
-
|
|
1
|
+
import { AuthorizationType, IRestApi, LambdaIntegration, RequestAuthorizer, Resource, TokenAuthorizer } from "aws-cdk-lib/aws-apigateway";
|
|
2
|
+
import { NodejsFunction } from "aws-cdk-lib/aws-lambda-nodejs";
|
|
3
|
+
import { TsgLambdaProp } from "../../config/types";
|
|
4
|
+
import { Logger } from "../../helpers/logger";
|
|
5
|
+
import { logger } from "../layers/common/src/nodejs";
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
export class Routes {
|
|
9
|
+
|
|
10
|
+
public static Resources: Resource[] = [];
|
|
11
|
+
private static routeMap = new Map();
|
|
12
|
+
|
|
13
|
+
public static createResource(
|
|
14
|
+
prop: TsgLambdaProp,
|
|
15
|
+
api: IRestApi,
|
|
16
|
+
lambdaNode: NodejsFunction,
|
|
17
|
+
authorizer?: TokenAuthorizer | RequestAuthorizer,
|
|
18
|
+
): void {
|
|
19
|
+
|
|
20
|
+
const routeMap: Map<string, Resource> = new Map();
|
|
21
|
+
|
|
22
|
+
let activeRoutePath = "";
|
|
23
|
+
let activeResource: Resource | undefined = undefined;
|
|
24
|
+
|
|
25
|
+
// Only attach lambda to an Api Gateway if a route exist
|
|
26
|
+
if (prop.apiGateway?.route) {
|
|
27
|
+
|
|
28
|
+
if (!prop.apiGateway.useRouteOverride) {
|
|
29
|
+
// First we create the root resource if it doesn't exist.
|
|
30
|
+
// Note: this now uses the bundle version as the first segment in the path.
|
|
31
|
+
activeRoutePath = `v${(prop.apiGateway?.version) ? prop.apiGateway.version : 1}`;
|
|
32
|
+
activeResource = Routes.routeMap.get(activeRoutePath) || api.root.addResource(activeRoutePath);
|
|
33
|
+
Routes.routeMap.set(activeRoutePath, activeResource);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
// Now we go through our route segments creating the rest of the path.
|
|
38
|
+
const pathSegments = prop.apiGateway?.route.split("/").filter(x => (x));
|
|
39
|
+
|
|
40
|
+
for (let i = 0; i < pathSegments.length; i++) {
|
|
41
|
+
activeRoutePath = `${activeRoutePath}/${pathSegments[i]}`;
|
|
42
|
+
let secondaryResource = Routes.routeMap.get(activeRoutePath) || activeResource?.addResource(pathSegments[i]) || api.root.addResource(pathSegments[i]);
|
|
43
|
+
Routes.routeMap.set(activeRoutePath, secondaryResource);
|
|
44
|
+
activeResource = secondaryResource;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// console.log(`Route: ${prop.apiGateway.method}\t Lambda Name: ${lambdaNode.node.id}\t Path: ${activeRoutePath}`);
|
|
48
|
+
// Finally, we attach our function to the last resource
|
|
49
|
+
activeResource?.addMethod(prop.apiGateway.method || 'GET',
|
|
50
|
+
new LambdaIntegration(lambdaNode, { proxy: true, }),
|
|
51
|
+
{
|
|
52
|
+
requestParameters: Routes.createQueryStringObject(prop.apiGateway.queryStrings),
|
|
53
|
+
apiKeyRequired: (prop.apiGateway.requireApiKey) ? true : false,
|
|
54
|
+
authorizer: prop.apiGateway.secure ? authorizer : undefined,
|
|
55
|
+
authorizationType: prop.apiGateway.secure ? AuthorizationType.CUSTOM : undefined,
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
public static createQueryStringObject(queryStrings: string[] | undefined): { [key: string]: boolean } {
|
|
61
|
+
|
|
62
|
+
if (!queryStrings || queryStrings.length === 0) {
|
|
63
|
+
return {};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const obj: { [key: string]: boolean } = {};
|
|
67
|
+
|
|
68
|
+
queryStrings.forEach((qs: string) => {
|
|
69
|
+
obj[`method.request.querystring.${qs}`] = true;
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
return obj;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
75
|
}
|
package/src/resources/index.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { CreateCertificate } from "./certificate/createCertificate";
|
|
1
|
+
export { CreateCertificate } from "./certificate/createCertificate";
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { Duration } from "aws-cdk-lib";
|
|
2
|
+
import { ServicePrincipal } from "aws-cdk-lib/aws-iam";
|
|
3
|
+
import { LayerVersion, Runtime } from "aws-cdk-lib/aws-lambda";
|
|
4
|
+
import { NodejsFunction, NodejsFunctionProps, SourceMapMode } from "aws-cdk-lib/aws-lambda-nodejs";
|
|
5
|
+
import { RetentionDays } from "aws-cdk-lib/aws-logs";
|
|
6
|
+
import path = require("path");
|
|
7
|
+
import { TimerJobProps } from "../../interfaces/timer-job";
|
|
8
|
+
import { Construct } from "constructs";
|
|
9
|
+
import { SERVICE_PRINCIPAL } from "../../constants/aws-service-principal-constants";
|
|
10
|
+
import { CronOptions, Rule, Schedule } from "aws-cdk-lib/aws-events";
|
|
11
|
+
import { LambdaFunction } from "aws-cdk-lib/aws-events-targets";
|
|
12
|
+
import { Table } from "aws-cdk-lib/aws-dynamodb";
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
export const createBasicLambdaTimerJob = (scope: Construct, props: TimerJobProps) => {
|
|
17
|
+
|
|
18
|
+
const lambdaProps = createBasicLambdaProps(props);
|
|
19
|
+
|
|
20
|
+
let lambdaFunction = new NodejsFunction(scope, `${props.appPrefix}${props.functionName}`, lambdaProps);
|
|
21
|
+
|
|
22
|
+
addInvokePermissionToLambdaForEvents(lambdaFunction);
|
|
23
|
+
|
|
24
|
+
const eventRule = createEventRuleForLambda(scope, lambdaFunction, props.cronOptions);
|
|
25
|
+
|
|
26
|
+
eventRule.addTarget(new LambdaFunction(lambdaFunction));
|
|
27
|
+
|
|
28
|
+
grantAccessToDynamoTables(scope, lambdaFunction, props.dynamoTableNames);
|
|
29
|
+
|
|
30
|
+
addLambdaLayers(scope, lambdaFunction, props.lambdaLayerArn);
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
const createBasicLambdaProps = (props: TimerJobProps) => {
|
|
34
|
+
|
|
35
|
+
const lambdaProp: NodejsFunctionProps = {
|
|
36
|
+
entry: (props.codePath) ? path.join(props.codePath) : path.join(`./resources/lambdas/timer-jobs/${props.functionName}/main.mts`),
|
|
37
|
+
functionName: `${(props.appPrefix) ? `${props.appPrefix}-` : ''}${props.functionName}`,
|
|
38
|
+
handler: 'main.ts',
|
|
39
|
+
logRetention: RetentionDays.TWO_WEEKS,
|
|
40
|
+
runtime: Runtime.NODEJS_LATEST,
|
|
41
|
+
timeout: Duration.minutes((props.timeoutInMinutes) ? props.timeoutInMinutes : 1),
|
|
42
|
+
memorySize: props.memory,
|
|
43
|
+
environment: {
|
|
44
|
+
...props.envs
|
|
45
|
+
},
|
|
46
|
+
bundling: {
|
|
47
|
+
minify: true,
|
|
48
|
+
target: `esnext`,
|
|
49
|
+
sourceMap: true,
|
|
50
|
+
sourceMapMode: SourceMapMode.EXTERNAL,
|
|
51
|
+
environment: {
|
|
52
|
+
...props.envs
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
role: props.role,
|
|
56
|
+
layers: undefined
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return lambdaProp;
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
const addInvokePermissionToLambdaForEvents = (lambda: NodejsFunction) => {
|
|
63
|
+
|
|
64
|
+
lambda.addPermission(`InvokePermission-${lambda.functionName}`, {
|
|
65
|
+
principal: new ServicePrincipal(SERVICE_PRINCIPAL.EVENTS),
|
|
66
|
+
});
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
const createEventRuleForLambda = (scope: Construct, lambda: NodejsFunction, options: CronOptions) => {
|
|
70
|
+
|
|
71
|
+
const eventRule = new Rule(scope, `scheduleRule-${lambda?.node.id || '010'}`, {
|
|
72
|
+
schedule: Schedule.cron(options),
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
return eventRule;
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
const grantAccessToDynamoTables = (scope: Construct, lambda: NodejsFunction, tableNames?: string[]) => {
|
|
79
|
+
|
|
80
|
+
if (tableNames && tableNames.length > 0) {
|
|
81
|
+
|
|
82
|
+
tableNames.forEach(tableName => {
|
|
83
|
+
|
|
84
|
+
const table = Table.fromTableName(scope, `${tableName}-table`, tableName);
|
|
85
|
+
|
|
86
|
+
table.grantReadWriteData(lambda);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
const addLambdaLayers = (scope: Construct, lambda: NodejsFunction, layerArns?: string[]) => {
|
|
95
|
+
|
|
96
|
+
if (layerArns && layerArns.length > 0) {
|
|
97
|
+
layerArns.forEach((arn: string, idx: number) => {
|
|
98
|
+
const layer = LayerVersion.fromLayerVersionArn(scope, `common-layer-${idx}`, arn);
|
|
99
|
+
|
|
100
|
+
lambda.addLayers(layer);
|
|
101
|
+
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
};
|