@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,162 +1,162 @@
|
|
|
1
|
-
import { Construct } from "constructs";
|
|
2
|
-
import { BaseResource } from "../base/baseResource";
|
|
3
|
-
import { ITable } from "aws-cdk-lib/aws-dynamodb";
|
|
4
|
-
import { AppConfig } from "../../config/AppConfig";
|
|
5
|
-
import { ApiLambdaResult } from "../../interfaces/ApiLambdaResult";
|
|
6
|
-
import { TsgAuthorizerType } from "../../config/types/TsgAuthorizerType";
|
|
7
|
-
import { IRestApi, RequestAuthorizer, TokenAuthorizer } from "aws-cdk-lib/aws-apigateway";
|
|
8
|
-
import { TsgJwtTokenAuthorizer } from "../lambda-authorizer/TsgJwtTokenAuthorizer";
|
|
9
|
-
import { CfnOutput, RemovalPolicy } from "aws-cdk-lib";
|
|
10
|
-
import { TsgRequestAuthorizer } from "../lambda-authorizer/TsgRequestAuthorizer";
|
|
11
|
-
import { CreateLambda } from "../lambda/createLambda";
|
|
12
|
-
import { LayerVersion } from "aws-cdk-lib/aws-lambda";
|
|
13
|
-
import { NodejsFunction } from "aws-cdk-lib/aws-lambda-nodejs";
|
|
14
|
-
import { TsgLambdaProp } from "../../config/types";
|
|
15
|
-
import { Routes } from "../helpers/createRoutes";
|
|
16
|
-
|
|
17
|
-
export class CreateApiAndAttachLambdas extends BaseResource<ApiLambdaResult> {
|
|
18
|
-
|
|
19
|
-
protected requireAuthorizer: boolean;
|
|
20
|
-
protected authorizerType?: TsgAuthorizerType;
|
|
21
|
-
|
|
22
|
-
private lambdas: NodejsFunction[] = [];
|
|
23
|
-
private lambdaRecords: Record<string, NodejsFunction> = {};
|
|
24
|
-
|
|
25
|
-
public get Lambdas() {
|
|
26
|
-
return this.lambdas;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
public get LambdaRecords() {
|
|
30
|
-
return this.lambdaRecords;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
constructor(scope: Construct,
|
|
34
|
-
config: AppConfig,
|
|
35
|
-
private gatewayApi: IRestApi,
|
|
36
|
-
private layers?: LayerVersion[],
|
|
37
|
-
private tables?: ITable[]) {
|
|
38
|
-
super(scope, config);
|
|
39
|
-
|
|
40
|
-
this.requireAuthorizer = (this.config.RESOURCES.AUTHORIZER &&
|
|
41
|
-
this.config.RESOURCES.AUTHORIZER.type) ? true : false;
|
|
42
|
-
|
|
43
|
-
if (this.requireAuthorizer) {
|
|
44
|
-
this.authorizerType = this.config.RESOURCES.AUTHORIZER?.type;
|
|
45
|
-
} else if (this.config.RESOURCES.AUTHORIZER && !this.config.RESOURCES.AUTHORIZER.type) {
|
|
46
|
-
throw new Error(`You must provide an authorizer type if a Authorizer is required`);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
this.createdResources = this.createResource(scope)!;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
protected createResource(scope: Construct): ApiLambdaResult[] | null {
|
|
53
|
-
|
|
54
|
-
let authorizer: TokenAuthorizer | RequestAuthorizer | undefined | null = undefined;
|
|
55
|
-
|
|
56
|
-
// Create Authorizer
|
|
57
|
-
if (this.requireAuthorizer) {
|
|
58
|
-
authorizer = this.createAuthorizer();
|
|
59
|
-
|
|
60
|
-
if (!authorizer) {
|
|
61
|
-
// console.log('Authorizer Not Created');
|
|
62
|
-
}
|
|
63
|
-
else {
|
|
64
|
-
// console.log('Authorizer Created', authorizer);
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
// Create Lambdas
|
|
69
|
-
const lambdas = new CreateLambda(scope, this.config, this.layers);
|
|
70
|
-
this.lambdas = lambdas.Lambdas;
|
|
71
|
-
this.lambdaRecords = this.lambdaRecords;
|
|
72
|
-
|
|
73
|
-
// Give Access to Lambdds to All DynamoDb Tables
|
|
74
|
-
if (this.tables) {
|
|
75
|
-
this.assignAccessToTables(this.tables, lambdas.Lambdas);
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
// Create Routes on API Gateway for Lambdas from config
|
|
79
|
-
this.AddRoutes(this.config, this.gatewayApi, lambdas.Lambdas, authorizer);
|
|
80
|
-
|
|
81
|
-
const result: ApiLambdaResult = {
|
|
82
|
-
api: this.gatewayApi,
|
|
83
|
-
authorizer: authorizer
|
|
84
|
-
};
|
|
85
|
-
return [result];
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
protected createOutput<T>(scope: Construct, createdAssets: T[]): void {
|
|
89
|
-
|
|
90
|
-
this.createdResources!.forEach((x, idx) => {
|
|
91
|
-
new CfnOutput(scope, `Authorizerr-${idx}`, {
|
|
92
|
-
value: x.authorizer?.authorizerArn!
|
|
93
|
-
});
|
|
94
|
-
});
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
private createAuthorizer() {
|
|
98
|
-
|
|
99
|
-
let authorizer: TokenAuthorizer | RequestAuthorizer | undefined = undefined;
|
|
100
|
-
|
|
101
|
-
if (this.requireAuthorizer && this.authorizerType === TsgAuthorizerType.TOKEN_AUTHORIZER) {
|
|
102
|
-
|
|
103
|
-
authorizer = new TsgJwtTokenAuthorizer(this.scope,
|
|
104
|
-
this.config).JwtAuthorizer;
|
|
105
|
-
|
|
106
|
-
(authorizer as TokenAuthorizer)?._attachToApi(this.gatewayApi);
|
|
107
|
-
(authorizer as TokenAuthorizer)?.applyRemovalPolicy(RemovalPolicy.DESTROY);
|
|
108
|
-
|
|
109
|
-
return authorizer;
|
|
110
|
-
|
|
111
|
-
} else if (this.requireAuthorizer && this.authorizerType === TsgAuthorizerType.REQUEST_AUTHORIZER) {
|
|
112
|
-
|
|
113
|
-
authorizer = new TsgRequestAuthorizer(this.scope,
|
|
114
|
-
this.config, this.layers, this.tables).TsgRequestAuthorizer as RequestAuthorizer;
|
|
115
|
-
|
|
116
|
-
(authorizer as RequestAuthorizer)._attachToApi(this.gatewayApi);
|
|
117
|
-
(authorizer as RequestAuthorizer).applyRemovalPolicy(RemovalPolicy.DESTROY);
|
|
118
|
-
|
|
119
|
-
return authorizer;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
return authorizer;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
private assignAccessToTables(tables: ITable[], lambdas: NodejsFunction[]) {
|
|
127
|
-
|
|
128
|
-
if (tables) {
|
|
129
|
-
lambdas.forEach((lambda: NodejsFunction) => {
|
|
130
|
-
|
|
131
|
-
tables.forEach((table: ITable) => {
|
|
132
|
-
|
|
133
|
-
table.grantReadWriteData(lambda);
|
|
134
|
-
|
|
135
|
-
});
|
|
136
|
-
});
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
private AddRoutes(config: AppConfig,
|
|
141
|
-
gateway: IRestApi,
|
|
142
|
-
lambdas: NodejsFunction[],
|
|
143
|
-
authorizer?: TokenAuthorizer | RequestAuthorizer) {
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
config.RESOURCES.LAMBDA?.forEach((prop: TsgLambdaProp) => {
|
|
147
|
-
|
|
148
|
-
const lambdaId = CreateLambda.getIdForLambda(prop, this.config);
|
|
149
|
-
|
|
150
|
-
if (!lambdaId) {
|
|
151
|
-
throw new Error(`Can't find lambda`);
|
|
152
|
-
}
|
|
153
|
-
const lambdaNode = lambdas.find(x => x.node.id === lambdaId);
|
|
154
|
-
|
|
155
|
-
if (!lambdaNode) {
|
|
156
|
-
throw new Error("Can't find the Lambda Integration");
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
Routes.createResource(prop, gateway, lambdaNode, authorizer);
|
|
160
|
-
});
|
|
161
|
-
}
|
|
1
|
+
import { Construct } from "constructs";
|
|
2
|
+
import { BaseResource } from "../base/baseResource";
|
|
3
|
+
import { ITable } from "aws-cdk-lib/aws-dynamodb";
|
|
4
|
+
import { AppConfig } from "../../config/AppConfig";
|
|
5
|
+
import { ApiLambdaResult } from "../../interfaces/ApiLambdaResult";
|
|
6
|
+
import { TsgAuthorizerType } from "../../config/types/TsgAuthorizerType";
|
|
7
|
+
import { IRestApi, RequestAuthorizer, TokenAuthorizer } from "aws-cdk-lib/aws-apigateway";
|
|
8
|
+
import { TsgJwtTokenAuthorizer } from "../lambda-authorizer/TsgJwtTokenAuthorizer";
|
|
9
|
+
import { CfnOutput, RemovalPolicy } from "aws-cdk-lib";
|
|
10
|
+
import { TsgRequestAuthorizer } from "../lambda-authorizer/TsgRequestAuthorizer";
|
|
11
|
+
import { CreateLambda } from "../lambda/createLambda";
|
|
12
|
+
import { LayerVersion } from "aws-cdk-lib/aws-lambda";
|
|
13
|
+
import { NodejsFunction } from "aws-cdk-lib/aws-lambda-nodejs";
|
|
14
|
+
import { TsgLambdaProp } from "../../config/types";
|
|
15
|
+
import { Routes } from "../helpers/createRoutes";
|
|
16
|
+
|
|
17
|
+
export class CreateApiAndAttachLambdas extends BaseResource<ApiLambdaResult> {
|
|
18
|
+
|
|
19
|
+
protected requireAuthorizer: boolean;
|
|
20
|
+
protected authorizerType?: TsgAuthorizerType;
|
|
21
|
+
|
|
22
|
+
private lambdas: NodejsFunction[] = [];
|
|
23
|
+
private lambdaRecords: Record<string, NodejsFunction> = {};
|
|
24
|
+
|
|
25
|
+
public get Lambdas() {
|
|
26
|
+
return this.lambdas;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
public get LambdaRecords() {
|
|
30
|
+
return this.lambdaRecords;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
constructor(scope: Construct,
|
|
34
|
+
config: AppConfig,
|
|
35
|
+
private gatewayApi: IRestApi,
|
|
36
|
+
private layers?: LayerVersion[],
|
|
37
|
+
private tables?: ITable[]) {
|
|
38
|
+
super(scope, config);
|
|
39
|
+
|
|
40
|
+
this.requireAuthorizer = (this.config.RESOURCES.AUTHORIZER &&
|
|
41
|
+
this.config.RESOURCES.AUTHORIZER.type) ? true : false;
|
|
42
|
+
|
|
43
|
+
if (this.requireAuthorizer) {
|
|
44
|
+
this.authorizerType = this.config.RESOURCES.AUTHORIZER?.type;
|
|
45
|
+
} else if (this.config.RESOURCES.AUTHORIZER && !this.config.RESOURCES.AUTHORIZER.type) {
|
|
46
|
+
throw new Error(`You must provide an authorizer type if a Authorizer is required`);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
this.createdResources = this.createResource(scope)!;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
protected createResource(scope: Construct): ApiLambdaResult[] | null {
|
|
53
|
+
|
|
54
|
+
let authorizer: TokenAuthorizer | RequestAuthorizer | undefined | null = undefined;
|
|
55
|
+
|
|
56
|
+
// Create Authorizer
|
|
57
|
+
if (this.requireAuthorizer) {
|
|
58
|
+
authorizer = this.createAuthorizer();
|
|
59
|
+
|
|
60
|
+
if (!authorizer) {
|
|
61
|
+
// console.log('Authorizer Not Created');
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
// console.log('Authorizer Created', authorizer);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// Create Lambdas
|
|
69
|
+
const lambdas = new CreateLambda(scope, this.config, this.layers);
|
|
70
|
+
this.lambdas = lambdas.Lambdas;
|
|
71
|
+
this.lambdaRecords = this.lambdaRecords;
|
|
72
|
+
|
|
73
|
+
// Give Access to Lambdds to All DynamoDb Tables
|
|
74
|
+
if (this.tables) {
|
|
75
|
+
this.assignAccessToTables(this.tables, lambdas.Lambdas);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Create Routes on API Gateway for Lambdas from config
|
|
79
|
+
this.AddRoutes(this.config, this.gatewayApi, lambdas.Lambdas, authorizer);
|
|
80
|
+
|
|
81
|
+
const result: ApiLambdaResult = {
|
|
82
|
+
api: this.gatewayApi,
|
|
83
|
+
authorizer: authorizer
|
|
84
|
+
};
|
|
85
|
+
return [result];
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
protected createOutput<T>(scope: Construct, createdAssets: T[]): void {
|
|
89
|
+
|
|
90
|
+
this.createdResources!.forEach((x, idx) => {
|
|
91
|
+
new CfnOutput(scope, `Authorizerr-${idx}`, {
|
|
92
|
+
value: x.authorizer?.authorizerArn!
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
private createAuthorizer() {
|
|
98
|
+
|
|
99
|
+
let authorizer: TokenAuthorizer | RequestAuthorizer | undefined = undefined;
|
|
100
|
+
|
|
101
|
+
if (this.requireAuthorizer && this.authorizerType === TsgAuthorizerType.TOKEN_AUTHORIZER) {
|
|
102
|
+
|
|
103
|
+
authorizer = new TsgJwtTokenAuthorizer(this.scope,
|
|
104
|
+
this.config).JwtAuthorizer;
|
|
105
|
+
|
|
106
|
+
(authorizer as TokenAuthorizer)?._attachToApi(this.gatewayApi);
|
|
107
|
+
(authorizer as TokenAuthorizer)?.applyRemovalPolicy(RemovalPolicy.DESTROY);
|
|
108
|
+
|
|
109
|
+
return authorizer;
|
|
110
|
+
|
|
111
|
+
} else if (this.requireAuthorizer && this.authorizerType === TsgAuthorizerType.REQUEST_AUTHORIZER) {
|
|
112
|
+
|
|
113
|
+
authorizer = new TsgRequestAuthorizer(this.scope,
|
|
114
|
+
this.config, this.layers, this.tables).TsgRequestAuthorizer as RequestAuthorizer;
|
|
115
|
+
|
|
116
|
+
(authorizer as RequestAuthorizer)._attachToApi(this.gatewayApi);
|
|
117
|
+
(authorizer as RequestAuthorizer).applyRemovalPolicy(RemovalPolicy.DESTROY);
|
|
118
|
+
|
|
119
|
+
return authorizer;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
return authorizer;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
private assignAccessToTables(tables: ITable[], lambdas: NodejsFunction[]) {
|
|
127
|
+
|
|
128
|
+
if (tables) {
|
|
129
|
+
lambdas.forEach((lambda: NodejsFunction) => {
|
|
130
|
+
|
|
131
|
+
tables.forEach((table: ITable) => {
|
|
132
|
+
|
|
133
|
+
table.grantReadWriteData(lambda);
|
|
134
|
+
|
|
135
|
+
});
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
private AddRoutes(config: AppConfig,
|
|
141
|
+
gateway: IRestApi,
|
|
142
|
+
lambdas: NodejsFunction[],
|
|
143
|
+
authorizer?: TokenAuthorizer | RequestAuthorizer) {
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
config.RESOURCES.LAMBDA?.forEach((prop: TsgLambdaProp) => {
|
|
147
|
+
|
|
148
|
+
const lambdaId = CreateLambda.getIdForLambda(prop, this.config);
|
|
149
|
+
|
|
150
|
+
if (!lambdaId) {
|
|
151
|
+
throw new Error(`Can't find lambda`);
|
|
152
|
+
}
|
|
153
|
+
const lambdaNode = lambdas.find(x => x.node.id === lambdaId);
|
|
154
|
+
|
|
155
|
+
if (!lambdaNode) {
|
|
156
|
+
throw new Error("Can't find the Lambda Integration");
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
Routes.createResource(prop, gateway, lambdaNode, authorizer);
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
162
|
}
|
|
@@ -1,174 +1,174 @@
|
|
|
1
|
-
import { CfnOutput, RemovalPolicy, Tag } from "aws-cdk-lib";
|
|
2
|
-
import { BasePathMapping, Cors, CorsOptions, DomainName, EndpointType, IDomainName, IRestApi, MethodOptions, RestApi, RestApiProps, SecurityPolicy } from "aws-cdk-lib/aws-apigateway";
|
|
3
|
-
import { ARecord, CnameRecord, HostedZone, IHostedZone, RecordTarget } from "aws-cdk-lib/aws-route53";
|
|
4
|
-
import { ApiGateway, ApiGatewayDomain } from "aws-cdk-lib/aws-route53-targets";
|
|
5
|
-
import { Construct } from "constructs";
|
|
6
|
-
import { AppConfig } from "../../config/AppConfig";
|
|
7
|
-
import { MicroserviceProps } from "../../interfaces/MicroserviceProps";
|
|
8
|
-
|
|
9
|
-
import { BaseResource } from "../base/baseResource";
|
|
10
|
-
import { CreateCertificate } from "../certificate/createCertificate";
|
|
11
|
-
import { TsgApiKey } from "./createApiKey";
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
export class Api extends BaseResource<IRestApi> {
|
|
15
|
-
|
|
16
|
-
private corsOptions: CorsOptions;
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
get APIs() {
|
|
20
|
-
return this.createdResources;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
constructor(scope: Construct, config: AppConfig, private env: string) {
|
|
24
|
-
super(scope, config);
|
|
25
|
-
|
|
26
|
-
this.corsOptions = this.createDefaultCorsOptions();
|
|
27
|
-
|
|
28
|
-
this.createdResources = this.createResource(scope);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
private createApi(scope: Construct, env: string) {
|
|
32
|
-
if (this.config.DNS) {
|
|
33
|
-
|
|
34
|
-
// console.log('### DNS is true ###');
|
|
35
|
-
|
|
36
|
-
const zone = this.getZone(this.scope, this.config);
|
|
37
|
-
|
|
38
|
-
const api = new RestApi(this.scope, `${this.config.AppPrefix}-rest-api`, this.createApiProps(env, zone));
|
|
39
|
-
|
|
40
|
-
this.createARecord(scope, zone, api);
|
|
41
|
-
|
|
42
|
-
// this.createCnameRecord(scope, zone, api, this.config);
|
|
43
|
-
|
|
44
|
-
this.createApiKey(this.config, api);
|
|
45
|
-
|
|
46
|
-
return api;
|
|
47
|
-
|
|
48
|
-
} else {
|
|
49
|
-
|
|
50
|
-
const api = new RestApi(this.scope, `${this.config.AppPrefix}-rest-api-without-DNS`, this.createApiProps(env));
|
|
51
|
-
|
|
52
|
-
this.createApiKey(this.config, api);
|
|
53
|
-
|
|
54
|
-
return api;
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
private createApiProps(env: string, zone?: IHostedZone): RestApiProps {
|
|
59
|
-
|
|
60
|
-
if (this.config.DNS) {
|
|
61
|
-
|
|
62
|
-
const cert = this.createCertificate(this.scope, zone!, this.config, env);
|
|
63
|
-
|
|
64
|
-
const props: RestApiProps = {
|
|
65
|
-
restApiName: `${this.config.AppPrefix}-${this.config.API.Name}`,
|
|
66
|
-
description: this.config.API.Description,
|
|
67
|
-
domainName: {
|
|
68
|
-
domainName: `${this.config.API.DomainPrefix}.${env}.${this.config.DNS.ZoneName}`,
|
|
69
|
-
certificate: cert.certificate,
|
|
70
|
-
endpointType: EndpointType.REGIONAL,
|
|
71
|
-
securityPolicy: SecurityPolicy.TLS_1_2
|
|
72
|
-
},
|
|
73
|
-
defaultCorsPreflightOptions: this.corsOptions
|
|
74
|
-
};
|
|
75
|
-
|
|
76
|
-
return props;
|
|
77
|
-
} else {
|
|
78
|
-
const props: RestApiProps = {
|
|
79
|
-
restApiName: `${this.config.AppPrefix}-${this.config.API.Name}`,
|
|
80
|
-
description: this.config.API.Description,
|
|
81
|
-
defaultCorsPreflightOptions: this.corsOptions
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
return props;
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
private createDefaultCorsOptions() {
|
|
89
|
-
|
|
90
|
-
const corsOptions: CorsOptions = {
|
|
91
|
-
allowHeaders: [
|
|
92
|
-
'*',
|
|
93
|
-
],
|
|
94
|
-
allowMethods: Cors.ALL_METHODS,
|
|
95
|
-
allowCredentials: true,
|
|
96
|
-
allowOrigins: Cors.ALL_ORIGINS,
|
|
97
|
-
};
|
|
98
|
-
|
|
99
|
-
return corsOptions;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
private requiresApiKey(config: AppConfig) {
|
|
103
|
-
// Determine if any of the lambdas require an API Key
|
|
104
|
-
return config.RESOURCES.LAMBDA.some((lambda) => lambda.apiGateway?.requireApiKey === true);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
private createApiKey(config: AppConfig, api: RestApi) {
|
|
108
|
-
|
|
109
|
-
if (this.requiresApiKey(this.config)) {
|
|
110
|
-
const apiKey = new TsgApiKey(this.scope, this.config, api)
|
|
111
|
-
return apiKey;
|
|
112
|
-
}
|
|
113
|
-
return null;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
private getZone(scope: Construct, config: MicroserviceProps) {
|
|
117
|
-
return HostedZone.fromHostedZoneAttributes(scope, `${config.DNS?.ZoneName}-zone`, {
|
|
118
|
-
zoneName: config.DNS?.ZoneName!,
|
|
119
|
-
hostedZoneId: config.DNS?.ZoneId!
|
|
120
|
-
});
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
private createCertificate(scope: Construct, zone: IHostedZone, config: MicroserviceProps,
|
|
124
|
-
env: string) {
|
|
125
|
-
const cert = new CreateCertificate(scope, config, zone, env);
|
|
126
|
-
|
|
127
|
-
cert.certificate.applyRemovalPolicy(RemovalPolicy.DESTROY);
|
|
128
|
-
return cert;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
private createARecord(scope: Construct, zone: IHostedZone, api: RestApi) {
|
|
132
|
-
const aRecord = new ARecord(scope, `api-dns-a-record-${this.config.API.DomainPrefix}`, {
|
|
133
|
-
zone,
|
|
134
|
-
target: RecordTarget.fromAlias(new ApiGateway(api)),
|
|
135
|
-
recordName: `${this.config.API.DomainPrefix}.${this.env}.${this.config.DNS?.ZoneName}`
|
|
136
|
-
});
|
|
137
|
-
|
|
138
|
-
aRecord.applyRemovalPolicy(RemovalPolicy.DESTROY);
|
|
139
|
-
return aRecord;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
private createCnameRecord(scope: Construct, zone: IHostedZone, api: RestApi, config: AppConfig) {
|
|
143
|
-
const record = new CnameRecord(scope, "api-cname-record", {
|
|
144
|
-
zone,
|
|
145
|
-
domainName: api.domainName?.domainNameAliasDomainName!,
|
|
146
|
-
recordName: `${config?.DNS?.SubDomainNameForApi}.${config?.DNS?.SubDomainName}`
|
|
147
|
-
});
|
|
148
|
-
return record;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
protected createResource(scope: Construct) {
|
|
152
|
-
|
|
153
|
-
const api = this.createApi(scope, this.env);
|
|
154
|
-
// TODO: ONLY IF CUSTOM MAPPING IS REQUIRED
|
|
155
|
-
// const domain = this.createCustomDomain(scope, this.config);
|
|
156
|
-
|
|
157
|
-
// const mapping = this.attachDomainToApi(scope, api, domain, this.config);
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
return [api];
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
protected createOutput<T>(scope: Construct, createdAssets: T[]): void {
|
|
165
|
-
createdAssets.forEach((api, idx) => {
|
|
166
|
-
|
|
167
|
-
new CfnOutput(scope, `api${idx}`, {
|
|
168
|
-
// @ts-ignore
|
|
169
|
-
value: api.url
|
|
170
|
-
});
|
|
171
|
-
});
|
|
172
|
-
}
|
|
173
|
-
|
|
1
|
+
import { CfnOutput, RemovalPolicy, Tag } from "aws-cdk-lib";
|
|
2
|
+
import { BasePathMapping, Cors, CorsOptions, DomainName, EndpointType, IDomainName, IRestApi, MethodOptions, RestApi, RestApiProps, SecurityPolicy } from "aws-cdk-lib/aws-apigateway";
|
|
3
|
+
import { ARecord, CnameRecord, HostedZone, IHostedZone, RecordTarget } from "aws-cdk-lib/aws-route53";
|
|
4
|
+
import { ApiGateway, ApiGatewayDomain } from "aws-cdk-lib/aws-route53-targets";
|
|
5
|
+
import { Construct } from "constructs";
|
|
6
|
+
import { AppConfig } from "../../config/AppConfig";
|
|
7
|
+
import { MicroserviceProps } from "../../interfaces/MicroserviceProps";
|
|
8
|
+
|
|
9
|
+
import { BaseResource } from "../base/baseResource";
|
|
10
|
+
import { CreateCertificate } from "../certificate/createCertificate";
|
|
11
|
+
import { TsgApiKey } from "./createApiKey";
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
export class Api extends BaseResource<IRestApi> {
|
|
15
|
+
|
|
16
|
+
private corsOptions: CorsOptions;
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
get APIs() {
|
|
20
|
+
return this.createdResources;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
constructor(scope: Construct, config: AppConfig, private env: string) {
|
|
24
|
+
super(scope, config);
|
|
25
|
+
|
|
26
|
+
this.corsOptions = this.createDefaultCorsOptions();
|
|
27
|
+
|
|
28
|
+
this.createdResources = this.createResource(scope);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
private createApi(scope: Construct, env: string) {
|
|
32
|
+
if (this.config.DNS) {
|
|
33
|
+
|
|
34
|
+
// console.log('### DNS is true ###');
|
|
35
|
+
|
|
36
|
+
const zone = this.getZone(this.scope, this.config);
|
|
37
|
+
|
|
38
|
+
const api = new RestApi(this.scope, `${this.config.AppPrefix}-rest-api`, this.createApiProps(env, zone));
|
|
39
|
+
|
|
40
|
+
this.createARecord(scope, zone, api);
|
|
41
|
+
|
|
42
|
+
// this.createCnameRecord(scope, zone, api, this.config);
|
|
43
|
+
|
|
44
|
+
this.createApiKey(this.config, api);
|
|
45
|
+
|
|
46
|
+
return api;
|
|
47
|
+
|
|
48
|
+
} else {
|
|
49
|
+
|
|
50
|
+
const api = new RestApi(this.scope, `${this.config.AppPrefix}-rest-api-without-DNS`, this.createApiProps(env));
|
|
51
|
+
|
|
52
|
+
this.createApiKey(this.config, api);
|
|
53
|
+
|
|
54
|
+
return api;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
private createApiProps(env: string, zone?: IHostedZone): RestApiProps {
|
|
59
|
+
|
|
60
|
+
if (this.config.DNS) {
|
|
61
|
+
|
|
62
|
+
const cert = this.createCertificate(this.scope, zone!, this.config, env);
|
|
63
|
+
|
|
64
|
+
const props: RestApiProps = {
|
|
65
|
+
restApiName: `${this.config.AppPrefix}-${this.config.API.Name}`,
|
|
66
|
+
description: this.config.API.Description,
|
|
67
|
+
domainName: {
|
|
68
|
+
domainName: `${this.config.API.DomainPrefix}.${env}.${this.config.DNS.ZoneName}`,
|
|
69
|
+
certificate: cert.certificate,
|
|
70
|
+
endpointType: EndpointType.REGIONAL,
|
|
71
|
+
securityPolicy: SecurityPolicy.TLS_1_2
|
|
72
|
+
},
|
|
73
|
+
defaultCorsPreflightOptions: this.corsOptions
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
return props;
|
|
77
|
+
} else {
|
|
78
|
+
const props: RestApiProps = {
|
|
79
|
+
restApiName: `${this.config.AppPrefix}-${this.config.API.Name}`,
|
|
80
|
+
description: this.config.API.Description,
|
|
81
|
+
defaultCorsPreflightOptions: this.corsOptions
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
return props;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
private createDefaultCorsOptions() {
|
|
89
|
+
|
|
90
|
+
const corsOptions: CorsOptions = {
|
|
91
|
+
allowHeaders: [
|
|
92
|
+
'*',
|
|
93
|
+
],
|
|
94
|
+
allowMethods: Cors.ALL_METHODS,
|
|
95
|
+
allowCredentials: true,
|
|
96
|
+
allowOrigins: Cors.ALL_ORIGINS,
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
return corsOptions;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
private requiresApiKey(config: AppConfig) {
|
|
103
|
+
// Determine if any of the lambdas require an API Key
|
|
104
|
+
return config.RESOURCES.LAMBDA.some((lambda) => lambda.apiGateway?.requireApiKey === true);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
private createApiKey(config: AppConfig, api: RestApi) {
|
|
108
|
+
|
|
109
|
+
if (this.requiresApiKey(this.config)) {
|
|
110
|
+
const apiKey = new TsgApiKey(this.scope, this.config, api)
|
|
111
|
+
return apiKey;
|
|
112
|
+
}
|
|
113
|
+
return null;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
private getZone(scope: Construct, config: MicroserviceProps) {
|
|
117
|
+
return HostedZone.fromHostedZoneAttributes(scope, `${config.DNS?.ZoneName}-zone`, {
|
|
118
|
+
zoneName: config.DNS?.ZoneName!,
|
|
119
|
+
hostedZoneId: config.DNS?.ZoneId!
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
private createCertificate(scope: Construct, zone: IHostedZone, config: MicroserviceProps,
|
|
124
|
+
env: string) {
|
|
125
|
+
const cert = new CreateCertificate(scope, config, zone, env);
|
|
126
|
+
|
|
127
|
+
cert.certificate.applyRemovalPolicy(RemovalPolicy.DESTROY);
|
|
128
|
+
return cert;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
private createARecord(scope: Construct, zone: IHostedZone, api: RestApi) {
|
|
132
|
+
const aRecord = new ARecord(scope, `api-dns-a-record-${this.config.API.DomainPrefix}`, {
|
|
133
|
+
zone,
|
|
134
|
+
target: RecordTarget.fromAlias(new ApiGateway(api)),
|
|
135
|
+
recordName: `${this.config.API.DomainPrefix}.${this.env}.${this.config.DNS?.ZoneName}`
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
aRecord.applyRemovalPolicy(RemovalPolicy.DESTROY);
|
|
139
|
+
return aRecord;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
private createCnameRecord(scope: Construct, zone: IHostedZone, api: RestApi, config: AppConfig) {
|
|
143
|
+
const record = new CnameRecord(scope, "api-cname-record", {
|
|
144
|
+
zone,
|
|
145
|
+
domainName: api.domainName?.domainNameAliasDomainName!,
|
|
146
|
+
recordName: `${config?.DNS?.SubDomainNameForApi}.${config?.DNS?.SubDomainName}`
|
|
147
|
+
});
|
|
148
|
+
return record;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
protected createResource(scope: Construct) {
|
|
152
|
+
|
|
153
|
+
const api = this.createApi(scope, this.env);
|
|
154
|
+
// TODO: ONLY IF CUSTOM MAPPING IS REQUIRED
|
|
155
|
+
// const domain = this.createCustomDomain(scope, this.config);
|
|
156
|
+
|
|
157
|
+
// const mapping = this.attachDomainToApi(scope, api, domain, this.config);
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
return [api];
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
protected createOutput<T>(scope: Construct, createdAssets: T[]): void {
|
|
165
|
+
createdAssets.forEach((api, idx) => {
|
|
166
|
+
|
|
167
|
+
new CfnOutput(scope, `api${idx}`, {
|
|
168
|
+
// @ts-ignore
|
|
169
|
+
value: api.url
|
|
170
|
+
});
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
|
|
174
174
|
}
|