@sylvesterllc/aws-constructs 1.0.31 → 1.0.34
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/__tests__/data/testConfig.tsx +136 -0
- package/__tests__/mocks/ResourceMock.ts +10 -0
- package/__tests__/router.test.ts +195 -0
- package/dist/config/AppConfig.js +11 -1
- package/dist/config/customConfigs/ResourceAppConfig.d.ts +4 -1
- package/dist/config/customConfigs/ResourceAppConfig.js +1 -1
- package/dist/config/types/LogDuration.d.ts +8 -0
- package/dist/config/types/LogDuration.js +13 -0
- package/dist/config/types/TsgAuthorizerProp.d.ts +6 -0
- package/dist/config/types/TsgAuthorizerProp.js +3 -0
- package/dist/config/types/TsgAuthorizerType.d.ts +4 -0
- package/dist/config/types/TsgAuthorizerType.js +9 -0
- package/dist/config/types/TsgEc2Prop.d.ts +7 -0
- package/dist/config/types/TsgEc2Prop.js +3 -0
- package/dist/config/types/TsgKeyPair.d.ts +4 -0
- package/dist/config/types/TsgKeyPair.js +3 -0
- package/dist/config/types/TsgLambdaProp.d.ts +2 -0
- package/dist/config/types/TsgLambdaProp.js +1 -1
- package/dist/config/types/TsgLambdaProps.d.ts +0 -2
- package/dist/config/types/TsgLambdaProps.js +1 -1
- package/dist/config/types/index.d.ts +3 -0
- package/dist/config/types/index.js +4 -1
- package/dist/constructs/MicroService.js +4 -13
- package/dist/interfaces/ApiLambdaResult.d.ts +4 -0
- package/dist/interfaces/ApiLambdaResult.js +3 -0
- package/dist/resources/dynamodb/CreateDynamo.js +3 -3
- package/dist/resources/ec2/create-ec2-instance.d.ts +9 -0
- package/dist/resources/ec2/create-ec2-instance.js +42 -0
- package/dist/resources/gateway/CreateApiAndAttachLambdas.d.ts +24 -0
- package/dist/resources/gateway/CreateApiAndAttachLambdas.js +89 -0
- package/dist/resources/helpers/createRoutes.d.ts +2 -2
- package/dist/resources/helpers/createRoutes.js +3 -3
- package/dist/resources/lambda/createLambda.d.ts +5 -4
- package/dist/resources/lambda/createLambda.js +49 -28
- package/dist/resources/{helpers/createAuthorizer.d.ts → lambda-authorizer/TsgJwtTokenAuthorizer.d.ts} +2 -4
- package/dist/resources/lambda-authorizer/TsgJwtTokenAuthorizer.js +80 -0
- package/dist/resources/lambda-authorizer/TsgRequestAuthorizer.d.ts +12 -0
- package/dist/resources/lambda-authorizer/TsgRequestAuthorizer.js +32 -0
- package/dist/resources/lambda-authorizer/createAuthorizerHelpers.d.ts +5 -0
- package/dist/resources/lambda-authorizer/createAuthorizerHelpers.js +50 -0
- package/jest.config.js +9 -0
- package/jest.config.ts.old +20 -0
- package/package.json +13 -11
- package/src/config/AppConfig.ts +13 -0
- package/src/config/customConfigs/ResourceAppConfig.ts +5 -1
- package/src/config/types/LogDuration.ts +8 -0
- package/src/config/types/TsgAuthorizerProp.ts +9 -0
- package/src/config/types/TsgAuthorizerType.ts +5 -0
- package/src/config/types/TsgEc2Prop.ts +9 -0
- package/src/config/types/TsgKeyPair.ts +6 -0
- package/src/config/types/TsgLambdaProp.ts +4 -0
- package/src/config/types/TsgLambdaProps.ts +0 -1
- package/src/config/types/index.ts +3 -1
- package/src/constructs/MicroService.ts +4 -14
- package/src/interfaces/ApiLambdaResult.ts +6 -0
- package/src/resources/dynamodb/CreateDynamo.ts +1 -1
- package/src/resources/ec2/create-ec2-instance.ts +53 -0
- package/src/resources/gateway/CreateApiAndAttachLambdas.ts +137 -0
- package/src/resources/helpers/createRoutes.ts +3 -3
- package/src/resources/lambda/createLambda.ts +83 -55
- package/src/resources/{helpers/createAuthorizer.ts → lambda-authorizer/TsgJwtTokenAuthorizer.ts} +3 -3
- package/src/resources/lambda-authorizer/TsgRequestAuthorizer.ts +36 -0
- package/src/resources/lambda-authorizer/createAuthorizerHelpers.ts +69 -0
- package/dist/resources/gateway/createMicroServiceBundle.d.ts +0 -14
- package/dist/resources/gateway/createMicroServiceBundle.js +0 -127
- package/dist/resources/helpers/createAuthorizer.js +0 -81
- package/src/resources/gateway/createMicroServiceBundle.ts +0 -184
|
@@ -0,0 +1,137 @@
|
|
|
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 { RemovalPolicy } from "aws-cdk-lib";
|
|
10
|
+
import { TsgRequestAuthorizer } from "../lambda-authorizer/TsgRequestAuthorizer";
|
|
11
|
+
import { CreateLambda } from "../lambda/createLambda";
|
|
12
|
+
import { TsgLambdaProps } from "../../config/types/TsgLambdaProps";
|
|
13
|
+
import { LayerVersion } from "aws-cdk-lib/aws-lambda";
|
|
14
|
+
import { NodejsFunction } from "aws-cdk-lib/aws-lambda-nodejs";
|
|
15
|
+
import { TsgLambdaProp } from "../../config/types";
|
|
16
|
+
import { Routes } from "../helpers/createRoutes";
|
|
17
|
+
|
|
18
|
+
export class CreateApiAndAttachLambdas extends BaseResource<ApiLambdaResult>{
|
|
19
|
+
|
|
20
|
+
protected readonly requireDynamoTableRefs: boolean;
|
|
21
|
+
protected readonly requireAuthorizer: boolean;
|
|
22
|
+
protected readonly authorizer?: TsgAuthorizerType;
|
|
23
|
+
|
|
24
|
+
constructor(scope: Construct,
|
|
25
|
+
protected readonly config: AppConfig,
|
|
26
|
+
private readonly gatewayApi: IRestApi,
|
|
27
|
+
private readonly layers?: LayerVersion[],
|
|
28
|
+
private readonly tables?: ITable[]) {
|
|
29
|
+
super(scope, config);
|
|
30
|
+
|
|
31
|
+
this.requireDynamoTableRefs = (this.config.RESOURCES.DYNAMO?.TABLE_REFS?.length ?? 0 > 0) ? true : false;
|
|
32
|
+
this.requireAuthorizer = (this.config.RESOURCES.AUTHORIZER && this.config.RESOURCES.AUTHORIZER.type) ? true : false;
|
|
33
|
+
|
|
34
|
+
if (this.requireAuthorizer) {
|
|
35
|
+
this.authorizer = this.config.RESOURCES.AUTHORIZER?.type;
|
|
36
|
+
} else {
|
|
37
|
+
throw new Error(`You must provide an authorizer type if a Authorizer is required`);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
this.onInit();
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
protected createResource(scope: Construct): any[] | null {
|
|
45
|
+
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
protected createOutput<T>(scope: Construct, createdAssets: T[]): void {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
private onInit() {
|
|
54
|
+
|
|
55
|
+
let authorizer: TokenAuthorizer | RequestAuthorizer | undefined = undefined;
|
|
56
|
+
|
|
57
|
+
// Create Authorizer
|
|
58
|
+
if (this.requireAuthorizer) {
|
|
59
|
+
authorizer = this.createAuthorizer()
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// Create Lambdas
|
|
63
|
+
const lambdas = new CreateLambda(this.scope, this.config, this.layers);
|
|
64
|
+
|
|
65
|
+
// Give Access to Lambdds to All DynamoDb Tables
|
|
66
|
+
if (this.tables) {
|
|
67
|
+
this.assignAccessToTables(this.tables, lambdas.Lambdas);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Create Routes on API Gateway for Lambdas from config
|
|
71
|
+
this.AddRoutes(this.config, this.gatewayApi, lambdas.Lambdas, authorizer);
|
|
72
|
+
|
|
73
|
+
return lambdas.Lambdas;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
private createAuthorizer() {
|
|
77
|
+
|
|
78
|
+
let authorizer: TokenAuthorizer | RequestAuthorizer | undefined = undefined;
|
|
79
|
+
|
|
80
|
+
if (this.requireAuthorizer && this.authorizer === TsgAuthorizerType.TOKEN_AUTHORIZER) {
|
|
81
|
+
|
|
82
|
+
authorizer = new TsgJwtTokenAuthorizer(this.scope,
|
|
83
|
+
this.config).JwtAuthorizer;
|
|
84
|
+
|
|
85
|
+
authorizer._attachToApi(this.gatewayApi);
|
|
86
|
+
authorizer.applyRemovalPolicy(RemovalPolicy.DESTROY);
|
|
87
|
+
|
|
88
|
+
return authorizer;
|
|
89
|
+
|
|
90
|
+
} else {
|
|
91
|
+
authorizer = new TsgRequestAuthorizer(this.scope,
|
|
92
|
+
this.config).RequestAuthorizer as RequestAuthorizer;
|
|
93
|
+
|
|
94
|
+
authorizer._attachToApi(this.gatewayApi);
|
|
95
|
+
authorizer.applyRemovalPolicy(RemovalPolicy.DESTROY);
|
|
96
|
+
|
|
97
|
+
return authorizer;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
private assignAccessToTables(tables: ITable[], lambdas: NodejsFunction[]) {
|
|
102
|
+
|
|
103
|
+
if (tables) {
|
|
104
|
+
lambdas.forEach((lambda: NodejsFunction) => {
|
|
105
|
+
|
|
106
|
+
tables.forEach((table: ITable) => {
|
|
107
|
+
|
|
108
|
+
table.grantReadWriteData(lambda);
|
|
109
|
+
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
private AddRoutes(config: AppConfig,
|
|
116
|
+
gateway: IRestApi,
|
|
117
|
+
lambdas: NodejsFunction[],
|
|
118
|
+
authorizer?: TokenAuthorizer|RequestAuthorizer) {
|
|
119
|
+
|
|
120
|
+
config.RESOURCES.LAMBDA?.forEach((prop: TsgLambdaProp) => {
|
|
121
|
+
|
|
122
|
+
const lambdaId = CreateLambda.getIdForLambda(prop, this.config);
|
|
123
|
+
|
|
124
|
+
if (!lambdaId) {
|
|
125
|
+
throw new Error(`Can't find lambda`);
|
|
126
|
+
}
|
|
127
|
+
const lambdaNode = lambdas.find(x => x.node.id === lambdaId);
|
|
128
|
+
|
|
129
|
+
if (!lambdaNode) {
|
|
130
|
+
throw new Error("Can't find the Lambda Integration");
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
Routes.createResource(prop, gateway, lambdaNode, authorizer);
|
|
134
|
+
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AuthorizationType, IRestApi, LambdaIntegration, Resource, TokenAuthorizer } from "aws-cdk-lib/aws-apigateway";
|
|
1
|
+
import { AuthorizationType, IRestApi, LambdaIntegration, RequestAuthorizer, Resource, TokenAuthorizer } from "aws-cdk-lib/aws-apigateway";
|
|
2
2
|
import { NodejsFunction } from "aws-cdk-lib/aws-lambda-nodejs";
|
|
3
3
|
import { TsgLambdaProp } from "../../config/types";
|
|
4
4
|
|
|
@@ -12,7 +12,7 @@ export class Routes {
|
|
|
12
12
|
prop: TsgLambdaProp,
|
|
13
13
|
api: IRestApi,
|
|
14
14
|
lambdaNode: NodejsFunction,
|
|
15
|
-
authorizer?: TokenAuthorizer) {
|
|
15
|
+
authorizer?: TokenAuthorizer|RequestAuthorizer): void {
|
|
16
16
|
|
|
17
17
|
const routeMap: Map<string, Resource> = new Map();
|
|
18
18
|
|
|
@@ -27,7 +27,7 @@ export class Routes {
|
|
|
27
27
|
if (!prop.apiGateway.useRouteOverride) {
|
|
28
28
|
// First we create the root resource if it doesn't exist.
|
|
29
29
|
// Note: this now uses the bundle version as the first segment in the path.
|
|
30
|
-
activeRoutePath = `v${(prop.apiGateway
|
|
30
|
+
activeRoutePath = `v${(prop.apiGateway?.version) ? prop.apiGateway.version : 1}`;
|
|
31
31
|
activeResource = Routes.routeMap.get(activeRoutePath) || api.root.addResource(activeRoutePath);
|
|
32
32
|
Routes.routeMap.set(activeRoutePath, activeResource);
|
|
33
33
|
}
|
|
@@ -7,34 +7,37 @@ import { NodejsFunction, NodejsFunctionProps, SourceMapMode } from "aws-cdk-lib/
|
|
|
7
7
|
import { Construct } from "constructs";
|
|
8
8
|
import * as path from 'path';
|
|
9
9
|
import { AppConfig } from "../../config/AppConfig";
|
|
10
|
-
import { TsgLambdaProp } from "../../config/types";
|
|
10
|
+
import { LogDuration, TsgLambdaProp } from "../../config/types";
|
|
11
11
|
|
|
12
12
|
import { TsgLambdaProps } from "../../config/types/TsgLambdaProps";
|
|
13
13
|
import { CreateLambdaFunctionInput } from "../../interfaces/CreateLambdaFunctionInput";
|
|
14
14
|
import { BaseResource } from "../base/baseResource";
|
|
15
|
+
import { RetentionDays } from "aws-cdk-lib/aws-logs";
|
|
15
16
|
|
|
16
17
|
|
|
17
18
|
export class CreateLambda extends BaseResource<NodejsFunction> {
|
|
18
19
|
|
|
19
20
|
public Lambdas: NodejsFunction[] = [];
|
|
20
|
-
|
|
21
|
+
public LambdaRecords: Record<string, NodejsFunction> = {};
|
|
21
22
|
|
|
22
|
-
constructor(
|
|
23
|
-
super(
|
|
23
|
+
constructor(scope: Construct, config: AppConfig, private layers?: LayerVersion[]) {
|
|
24
|
+
super(scope, config);
|
|
24
25
|
|
|
25
|
-
const resources = this.createResource(
|
|
26
|
+
const resources = this.createResource(scope);
|
|
26
27
|
|
|
27
28
|
this.Lambdas = [...resources];
|
|
28
29
|
|
|
29
30
|
this.createAlarmsForLambdas(this.Lambdas);
|
|
30
|
-
|
|
31
|
-
this.
|
|
31
|
+
|
|
32
|
+
this.LambdaRecords = this.createRecordForLambda(this.Lambdas);
|
|
33
|
+
|
|
34
|
+
this.createOutput(scope, resources);
|
|
32
35
|
}
|
|
33
36
|
|
|
34
37
|
protected createResource(scope: Construct): NodejsFunction[] {
|
|
35
38
|
|
|
36
|
-
const result = this.createLambdas(this.
|
|
37
|
-
|
|
39
|
+
const result = this.createLambdas(this.config);
|
|
40
|
+
|
|
38
41
|
return result;
|
|
39
42
|
}
|
|
40
43
|
|
|
@@ -48,17 +51,17 @@ export class CreateLambda extends BaseResource<NodejsFunction> {
|
|
|
48
51
|
});
|
|
49
52
|
}
|
|
50
53
|
|
|
51
|
-
private createLambdas(
|
|
52
|
-
|
|
53
|
-
const createdLambdas: NodejsFunction[] = this.createLambdaFunctions(this.scope,
|
|
54
|
+
private createLambdas(config: AppConfig): NodejsFunction[] {
|
|
55
|
+
|
|
56
|
+
const createdLambdas: NodejsFunction[] = this.createLambdaFunctions(this.scope, undefined, this.layers);
|
|
54
57
|
|
|
55
58
|
return createdLambdas;
|
|
56
59
|
}
|
|
57
60
|
|
|
58
|
-
private createLambdaFunctions(scope: Construct, role?: IRole, layers?: LayerVersion[]) {
|
|
59
|
-
|
|
60
|
-
const createdLambdas = this.
|
|
61
|
-
|
|
61
|
+
private createLambdaFunctions(scope: Construct, role?: IRole, layers?: LayerVersion[]) {
|
|
62
|
+
|
|
63
|
+
const createdLambdas = this.config.RESOURCES.LAMBDA.map((config: TsgLambdaProp) => {
|
|
64
|
+
|
|
62
65
|
let lambdaProps = this.createLambdaProps(config, role, layers);
|
|
63
66
|
|
|
64
67
|
const lambdaId = CreateLambda.getIdForLambda(config, this.config);
|
|
@@ -68,13 +71,6 @@ export class CreateLambda extends BaseResource<NodejsFunction> {
|
|
|
68
71
|
console.log(`found Lambda for : ${fctn.node.id}`);
|
|
69
72
|
}
|
|
70
73
|
|
|
71
|
-
|
|
72
|
-
// If we have managed policies, we add them.
|
|
73
|
-
if (config.managedPolicies && config.managedPolicies?.length > 0) {
|
|
74
|
-
|
|
75
|
-
this.assignManagedPolicies(fctn, config.managedPolicies);
|
|
76
|
-
}
|
|
77
|
-
|
|
78
74
|
return fctn;
|
|
79
75
|
});
|
|
80
76
|
|
|
@@ -84,9 +80,9 @@ export class CreateLambda extends BaseResource<NodejsFunction> {
|
|
|
84
80
|
private createLambdaProps(prop: TsgLambdaProp, role?: IRole, layers?: LayerVersion[], props?: TsgLambdaProps) {
|
|
85
81
|
|
|
86
82
|
return this.createLambdaFunctionProps({
|
|
87
|
-
prop,
|
|
83
|
+
prop,
|
|
88
84
|
role,
|
|
89
|
-
layers,
|
|
85
|
+
layers,
|
|
90
86
|
props
|
|
91
87
|
});
|
|
92
88
|
}
|
|
@@ -94,17 +90,18 @@ export class CreateLambda extends BaseResource<NodejsFunction> {
|
|
|
94
90
|
private createLambdaFunctionProps(props: CreateLambdaFunctionInput) {
|
|
95
91
|
const { prop, role, layers } = props;
|
|
96
92
|
|
|
97
|
-
console.log(`function Name: ${this.
|
|
93
|
+
console.log(`function Name: ${this.config.AppPrefix}-${prop.name}`);
|
|
98
94
|
|
|
99
95
|
const lambdaProp: NodejsFunctionProps = {
|
|
100
96
|
entry: path.join(prop.codePath),
|
|
101
|
-
functionName: `${this.
|
|
97
|
+
functionName: `${this.config.AppPrefix}-${prop.name}`,
|
|
102
98
|
handler: prop.handler,
|
|
99
|
+
logRetention: (!prop.logDuration) ? RetentionDays.FIVE_DAYS : getDayToSaveLogs(prop.logDuration),
|
|
103
100
|
runtime: prop.runtime || this.config.GLOBALS.stackRuntime,
|
|
104
101
|
timeout: prop.duration || Duration.minutes(2),
|
|
105
102
|
memorySize: prop.memory || 512,
|
|
106
103
|
environment: {
|
|
107
|
-
"VERBOSE_LOGGING": "true",
|
|
104
|
+
"VERBOSE_LOGGING": "true",
|
|
108
105
|
...prop.environment
|
|
109
106
|
},
|
|
110
107
|
bundling: {
|
|
@@ -119,40 +116,29 @@ export class CreateLambda extends BaseResource<NodejsFunction> {
|
|
|
119
116
|
|
|
120
117
|
}
|
|
121
118
|
|
|
122
|
-
|
|
119
|
+
|
|
123
120
|
return lambdaProp;
|
|
124
121
|
};
|
|
125
122
|
|
|
126
|
-
private
|
|
127
|
-
|
|
128
|
-
managedPolicyNames.forEach((managedPolicyName: string) => {
|
|
129
|
-
|
|
130
|
-
let policy = ManagedPolicy.fromAwsManagedPolicyName(managedPolicyName);
|
|
131
|
-
|
|
132
|
-
lambda.role?.addManagedPolicy(policy);
|
|
133
|
-
});
|
|
134
|
-
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
private createAlarmsForLambdas(lambdas: NodejsFunction[]) {
|
|
123
|
+
private createAlarmsForLambdas(lambdas: NodejsFunction[]) {
|
|
138
124
|
|
|
139
125
|
lambdas.forEach((lambda, idx) => {
|
|
140
126
|
|
|
141
127
|
const errorMetric = lambda.metricErrors({
|
|
142
128
|
period: Duration.minutes(3),
|
|
143
|
-
|
|
129
|
+
|
|
144
130
|
});
|
|
145
131
|
|
|
146
132
|
const durationMetric = lambda.metricDuration({
|
|
147
|
-
period: Duration.minutes(3),
|
|
133
|
+
period: Duration.minutes(3),
|
|
148
134
|
});
|
|
149
135
|
|
|
150
136
|
const invocationMetric = lambda.metricInvocations({
|
|
151
|
-
period: Duration.minutes(3),
|
|
137
|
+
period: Duration.minutes(3),
|
|
152
138
|
});
|
|
153
139
|
|
|
154
|
-
new Alarm(this.
|
|
155
|
-
metric: errorMetric,
|
|
140
|
+
new Alarm(this.scope, `${this.config.AppPrefix}-${idx}-error-alarm`, {
|
|
141
|
+
metric: errorMetric,
|
|
156
142
|
threshold: 5,
|
|
157
143
|
comparisonOperator: ComparisonOperator.GREATER_THAN_OR_EQUAL_TO_THRESHOLD,
|
|
158
144
|
evaluationPeriods: 3,
|
|
@@ -160,8 +146,8 @@ export class CreateLambda extends BaseResource<NodejsFunction> {
|
|
|
160
146
|
alarmName: `${this.config.AppPrefix}-${idx}-error-alarm`
|
|
161
147
|
});
|
|
162
148
|
|
|
163
|
-
new Alarm(this.
|
|
164
|
-
metric: durationMetric,
|
|
149
|
+
new Alarm(this.scope, `${this.config.AppPrefix}-${idx}-duration-alarm`, {
|
|
150
|
+
metric: durationMetric,
|
|
165
151
|
threshold: 1,
|
|
166
152
|
comparisonOperator: ComparisonOperator.GREATER_THAN_OR_EQUAL_TO_THRESHOLD,
|
|
167
153
|
evaluationPeriods: 3,
|
|
@@ -169,21 +155,63 @@ export class CreateLambda extends BaseResource<NodejsFunction> {
|
|
|
169
155
|
alarmName: `${this.config.AppPrefix}-${idx}-duration-alarm`
|
|
170
156
|
});
|
|
171
157
|
|
|
172
|
-
const invocationAlarm = new Alarm(this.
|
|
173
|
-
metric: errorMetric,
|
|
158
|
+
const invocationAlarm = new Alarm(this.scope, `${this.config.AppPrefix}-${idx}-invocation-alarm`, {
|
|
159
|
+
metric: errorMetric,
|
|
174
160
|
threshold: 1000,
|
|
175
161
|
comparisonOperator: ComparisonOperator.GREATER_THAN_OR_EQUAL_TO_THRESHOLD,
|
|
176
162
|
evaluationPeriods: 3,
|
|
177
163
|
alarmDescription: `${this.config.AppPrefix}-${idx} errors over 3 min period`,
|
|
178
164
|
alarmName: `${this.config.AppPrefix}-${idx}-invocation-Metric-alarm`
|
|
179
165
|
});
|
|
180
|
-
|
|
181
|
-
// const alarmAction: IAlarmAction = {};
|
|
182
|
-
// invocationAlarm.addAlarmAction(alarmAction);
|
|
183
166
|
});
|
|
184
167
|
}
|
|
185
168
|
|
|
186
|
-
public static getIdForLambda(lambdaProp: TsgLambdaProp, appConfig: AppConfig) {
|
|
169
|
+
public static getIdForLambda(lambdaProp: TsgLambdaProp, appConfig: AppConfig) {
|
|
187
170
|
return `${appConfig.AppPrefix}-${lambdaProp.name}`.toLowerCase();
|
|
188
171
|
}
|
|
189
|
-
|
|
172
|
+
|
|
173
|
+
private createRecordForLambda(lambdas: NodejsFunction[]) {
|
|
174
|
+
|
|
175
|
+
const names = this.config.RESOURCES.LAMBDA.map((lambda) => {
|
|
176
|
+
return lambda.name;
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
const lambdaNames = [...names] as const;
|
|
180
|
+
|
|
181
|
+
type LambdaName = typeof lambdaNames[number];
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
const lambdaRecord: Record<LambdaName, NodejsFunction> = {} as Record<LambdaName, NodejsFunction>;
|
|
185
|
+
|
|
186
|
+
lambdas.forEach((lambda, idx) => {
|
|
187
|
+
lambdaRecord[lambdaNames[idx] as LambdaName] = lambdas[idx];
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
return lambdaRecord;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function getDayToSaveLogs(saveLogDuration: LogDuration): RetentionDays {
|
|
195
|
+
|
|
196
|
+
switch (saveLogDuration) {
|
|
197
|
+
case LogDuration.ONE_DAY:
|
|
198
|
+
return RetentionDays.ONE_DAY;
|
|
199
|
+
|
|
200
|
+
case LogDuration.ONE_WEEK:
|
|
201
|
+
return RetentionDays.ONE_WEEK;
|
|
202
|
+
|
|
203
|
+
case LogDuration.ONE_MONTH:
|
|
204
|
+
return RetentionDays.ONE_MONTH;
|
|
205
|
+
|
|
206
|
+
case LogDuration.ONE_YEAR:
|
|
207
|
+
return RetentionDays.ONE_YEAR;
|
|
208
|
+
|
|
209
|
+
case LogDuration.FIVE_YEARS:
|
|
210
|
+
return RetentionDays.FIVE_YEARS;
|
|
211
|
+
|
|
212
|
+
case LogDuration.FOREVER:
|
|
213
|
+
return RetentionDays.INFINITE;
|
|
214
|
+
default:
|
|
215
|
+
return RetentionDays.FIVE_DAYS;
|
|
216
|
+
}
|
|
217
|
+
}
|
package/src/resources/{helpers/createAuthorizer.ts → lambda-authorizer/TsgJwtTokenAuthorizer.ts}
RENAMED
|
@@ -11,13 +11,13 @@ import { MicroserviceProps } from "../../interfaces/MicroserviceProps";
|
|
|
11
11
|
import { BaseResource } from "../base/baseResource";
|
|
12
12
|
import { CreateLambda } from "../lambda/createLambda";
|
|
13
13
|
|
|
14
|
-
export class
|
|
14
|
+
export class TsgJwtTokenAuthorizer extends BaseResource<TokenAuthorizer> {
|
|
15
15
|
|
|
16
16
|
get JwtAuthorizer() {
|
|
17
17
|
return this.createdResources[0];
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
constructor(scope: Construct, props: AppConfig
|
|
20
|
+
constructor(scope: Construct, props: AppConfig) {
|
|
21
21
|
|
|
22
22
|
super(scope, props);
|
|
23
23
|
|
|
@@ -30,7 +30,7 @@ export class CreateAuthorizer extends BaseResource<TokenAuthorizer> {
|
|
|
30
30
|
}
|
|
31
31
|
|
|
32
32
|
protected createResource(scope: Construct): TokenAuthorizer[] | null {
|
|
33
|
-
return [this.createLambdaAuthorizer(scope, this.
|
|
33
|
+
return [this.createLambdaAuthorizer(scope, this.config.RESOURCES.AUTHORIZER!)];
|
|
34
34
|
}
|
|
35
35
|
|
|
36
36
|
private createLambdaAuthorizer(scope: Construct, lambdaConfig: TsgLambdaProp) {
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { RequestAuthorizer } from "aws-cdk-lib/aws-apigateway";
|
|
2
|
+
import { Construct } from "constructs";
|
|
3
|
+
import { AppConfig } from "../../config/AppConfig";
|
|
4
|
+
import { BaseResource } from "../base/baseResource";
|
|
5
|
+
|
|
6
|
+
import { CfnOutput } from "aws-cdk-lib";
|
|
7
|
+
import { createAuthorizer } from "./createAuthorizerHelpers";
|
|
8
|
+
import { LayerVersion } from "aws-cdk-lib/aws-lambda";
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
export class TsgRequestAuthorizer extends BaseResource<RequestAuthorizer> {
|
|
12
|
+
|
|
13
|
+
get RequestAuthorizer() {
|
|
14
|
+
return this.createdResources[0];
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
constructor(scope: Construct, config: AppConfig, private layers?: LayerVersion[]) {
|
|
18
|
+
super(scope, config);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
protected createResource(scope: Construct): RequestAuthorizer[] | null {
|
|
22
|
+
const authorizer = createAuthorizer(scope, this.config, this.layers);
|
|
23
|
+
return [authorizer];
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
protected createOutput<T>(scope: Construct, createdAssets: T[]): void {
|
|
27
|
+
createdAssets.forEach((asset) => {
|
|
28
|
+
if (asset instanceof RequestAuthorizer) {
|
|
29
|
+
// Output the ARN of the authorizer
|
|
30
|
+
new CfnOutput(scope, "RequestAuthorizerArn", {
|
|
31
|
+
value: `${asset.authorizationType}:${asset.authorizerArn}`
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { Duration } from "aws-cdk-lib";
|
|
2
|
+
import { IdentitySource, RequestAuthorizer } from "aws-cdk-lib/aws-apigateway";
|
|
3
|
+
import { IFunction, LayerVersion, Runtime } from "aws-cdk-lib/aws-lambda";
|
|
4
|
+
import { Construct } from "constructs";
|
|
5
|
+
import { AppConfig } from "../../config/AppConfig";
|
|
6
|
+
import { NodejsFunction, NodejsFunctionProps, SourceMapMode } from "aws-cdk-lib/aws-lambda-nodejs";
|
|
7
|
+
import path = require("path");
|
|
8
|
+
import { RetentionDays } from "aws-cdk-lib/aws-logs";
|
|
9
|
+
|
|
10
|
+
export const createAuthorizer = (scope: Construct, config: AppConfig, layers?: LayerVersion[]) => {
|
|
11
|
+
|
|
12
|
+
const lambda = createLambdaForAuthorizer(scope, config);
|
|
13
|
+
|
|
14
|
+
const lambdaAuthroizer = new RequestAuthorizer(
|
|
15
|
+
scope,
|
|
16
|
+
`lambdaAuthorizer`,
|
|
17
|
+
{
|
|
18
|
+
handler: lambda,
|
|
19
|
+
identitySources: [IdentitySource.header(config.RESOURCES.AUTHORIZER?.headerName!)],
|
|
20
|
+
authorizerName: `${config.AppPrefix}-authorizer`,
|
|
21
|
+
resultsCacheTtl: Duration.seconds(0),
|
|
22
|
+
}
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
return lambdaAuthroizer;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const createLambdaForAuthorizer = (scope: Construct, config: AppConfig, layers?: LayerVersion[]) => {
|
|
29
|
+
|
|
30
|
+
const props = createLambdaProps(config, layers);
|
|
31
|
+
|
|
32
|
+
const lambda = new NodejsFunction(
|
|
33
|
+
scope,
|
|
34
|
+
`${config.AppPrefix}-authorizer`,
|
|
35
|
+
props
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
return lambda;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const createLambdaProps = (appConfig: AppConfig, layers?: LayerVersion[]) => {
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
const prop = appConfig.RESOURCES.AUTHORIZER!;
|
|
45
|
+
|
|
46
|
+
const lambdaProp: NodejsFunctionProps = {
|
|
47
|
+
entry: path.join(prop.codePath),
|
|
48
|
+
functionName: `${appConfig.AppPrefix}-${prop.name}`,
|
|
49
|
+
handler: prop.handler,
|
|
50
|
+
logRetention: (!prop.logDuration) ? RetentionDays.FIVE_DAYS : RetentionDays.ONE_MONTH,
|
|
51
|
+
runtime: prop.runtime || appConfig.GLOBALS.stackRuntime,
|
|
52
|
+
timeout: prop.duration || Duration.minutes(2),
|
|
53
|
+
memorySize: prop.memory || 512,
|
|
54
|
+
environment: {
|
|
55
|
+
"VERBOSE_LOGGING": "true",
|
|
56
|
+
...prop.environment
|
|
57
|
+
},
|
|
58
|
+
bundling: {
|
|
59
|
+
minify: false,
|
|
60
|
+
target: 'esNext',
|
|
61
|
+
sourceMap: true,
|
|
62
|
+
sourceMapMode: SourceMapMode.EXTERNAL,
|
|
63
|
+
environment: prop.environment || prop.environment,
|
|
64
|
+
},
|
|
65
|
+
layers
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return lambdaProp;
|
|
69
|
+
};
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { ServiceBundleConfig } from "../../config/ServiceBundleConfig";
|
|
2
|
-
export declare class CreateMicroServiceBundle {
|
|
3
|
-
private serviceBundleConfig;
|
|
4
|
-
protected readonly requireDynamoTableRefs: boolean;
|
|
5
|
-
protected readonly requireAuthorizer: boolean;
|
|
6
|
-
constructor(serviceBundleConfig: ServiceBundleConfig);
|
|
7
|
-
private onInit;
|
|
8
|
-
private AssignAccessToTables;
|
|
9
|
-
private AssignAccessToTableRefs;
|
|
10
|
-
private AssignReadWriteAccessToTableInRegion;
|
|
11
|
-
private AssignReadWriteAccessToTable;
|
|
12
|
-
private AddRoutes;
|
|
13
|
-
private AssignAccessToSecretManager;
|
|
14
|
-
}
|