@gradientedge/cdk-utils 8.71.0 → 8.73.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/lib/construct/graphql-api-lambda-with-cache/main.d.ts +2 -1
- package/dist/src/lib/construct/graphql-api-lambda-with-cache/main.js +2 -1
- package/dist/src/lib/construct/index.d.ts +2 -0
- package/dist/src/lib/construct/index.js +2 -0
- package/dist/src/lib/construct/rest-api-lambda/index.d.ts +1 -0
- package/dist/src/lib/construct/rest-api-lambda/index.js +17 -0
- package/dist/src/lib/construct/rest-api-lambda/main.d.ts +114 -0
- package/dist/src/lib/construct/rest-api-lambda/main.js +214 -0
- package/dist/src/lib/construct/rest-api-lambda-with-cache/index.d.ts +1 -0
- package/dist/src/lib/construct/rest-api-lambda-with-cache/index.js +17 -0
- package/dist/src/lib/construct/rest-api-lambda-with-cache/main.d.ts +67 -0
- package/dist/src/lib/construct/rest-api-lambda-with-cache/main.js +137 -0
- package/dist/src/lib/construct/site-with-ecs-backend/main.js +5 -1
- package/dist/src/lib/manager/aws/iam-manager.d.ts +6 -0
- package/dist/src/lib/manager/aws/iam-manager.js +12 -0
- package/dist/src/lib/types/aws/index.d.ts +38 -0
- package/dist/src/lib/utils/index.js +1 -1
- package/package.json +9 -9
- package/src/lib/construct/graphql-api-lambda-with-cache/main.ts +2 -1
- package/src/lib/construct/index.ts +2 -0
- package/src/lib/construct/rest-api-lambda/index.ts +1 -0
- package/src/lib/construct/rest-api-lambda/main.ts +261 -0
- package/src/lib/construct/rest-api-lambda-with-cache/index.ts +1 -0
- package/src/lib/construct/rest-api-lambda-with-cache/main.ts +151 -0
- package/src/lib/construct/site-with-ecs-backend/main.ts +8 -1
- package/src/lib/manager/aws/iam-manager.ts +13 -0
- package/src/lib/types/aws/index.ts +40 -0
|
@@ -4,7 +4,8 @@ import { Construct } from 'constructs';
|
|
|
4
4
|
import { GraphQLApiLambda } from '..';
|
|
5
5
|
import { GraphQlApiLambdaWithCacheProps } from '../../types';
|
|
6
6
|
/**
|
|
7
|
-
* @
|
|
7
|
+
* @deprecated Use RestApiLambdaWithCache instead. This will be removed in a future release.
|
|
8
|
+
*
|
|
8
9
|
* @category cdk-utils.graphql-api-lambda-with-cache
|
|
9
10
|
* @subcategory construct
|
|
10
11
|
* @classdesc Provides a construct to create and deploy a Graphql API as Lambda with Caching
|
|
@@ -30,7 +30,8 @@ const iam = __importStar(require("aws-cdk-lib/aws-iam"));
|
|
|
30
30
|
const __1 = require("..");
|
|
31
31
|
const utils = __importStar(require("../../utils"));
|
|
32
32
|
/**
|
|
33
|
-
* @
|
|
33
|
+
* @deprecated Use RestApiLambdaWithCache instead. This will be removed in a future release.
|
|
34
|
+
*
|
|
34
35
|
* @category cdk-utils.graphql-api-lambda-with-cache
|
|
35
36
|
* @subcategory construct
|
|
36
37
|
* @classdesc Provides a construct to create and deploy a Graphql API as Lambda with Caching
|
|
@@ -3,5 +3,7 @@ export * from './api-to-eventbridge-target-with-sns';
|
|
|
3
3
|
export * from './api-to-lambda-target';
|
|
4
4
|
export * from './graphql-api-lambda';
|
|
5
5
|
export * from './graphql-api-lambda-with-cache';
|
|
6
|
+
export * from './rest-api-lambda';
|
|
7
|
+
export * from './rest-api-lambda-with-cache';
|
|
6
8
|
export * from './site-with-ecs-backend';
|
|
7
9
|
export * from './static-site';
|
|
@@ -19,5 +19,7 @@ __exportStar(require("./api-to-eventbridge-target-with-sns"), exports);
|
|
|
19
19
|
__exportStar(require("./api-to-lambda-target"), exports);
|
|
20
20
|
__exportStar(require("./graphql-api-lambda"), exports);
|
|
21
21
|
__exportStar(require("./graphql-api-lambda-with-cache"), exports);
|
|
22
|
+
__exportStar(require("./rest-api-lambda"), exports);
|
|
23
|
+
__exportStar(require("./rest-api-lambda-with-cache"), exports);
|
|
22
24
|
__exportStar(require("./site-with-ecs-backend"), exports);
|
|
23
25
|
__exportStar(require("./static-site"), exports);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './main';
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./main"), exports);
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import * as apig from 'aws-cdk-lib/aws-apigateway';
|
|
2
|
+
import * as acm from 'aws-cdk-lib/aws-certificatemanager';
|
|
3
|
+
import * as iam from 'aws-cdk-lib/aws-iam';
|
|
4
|
+
import * as lambda from 'aws-cdk-lib/aws-lambda';
|
|
5
|
+
import * as route53 from 'aws-cdk-lib/aws-route53';
|
|
6
|
+
import * as secretsmanager from 'aws-cdk-lib/aws-secretsmanager';
|
|
7
|
+
import { Construct } from 'constructs';
|
|
8
|
+
import { CommonConstruct } from '../../common';
|
|
9
|
+
import { RestApiLambdaEnvironment, RestApiLambdaProps } from '../../types';
|
|
10
|
+
/**
|
|
11
|
+
* @deprecated Use RestApiLambda instead. This will be removed in a future release.
|
|
12
|
+
*
|
|
13
|
+
* @category cdk-utils.rest-api-lambda
|
|
14
|
+
* @subcategory construct
|
|
15
|
+
* @classdesc Provides a construct to create and deploy a RestApi as Lambda
|
|
16
|
+
*
|
|
17
|
+
* <b>Architecture</b><br/> 
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* import { RestApiLambda, RestApiLambdaProps } '@gradientedge/cdk-utils'
|
|
21
|
+
* import { Construct } from 'constructs'
|
|
22
|
+
*
|
|
23
|
+
* class CustomConstruct extends RestApiLambda {
|
|
24
|
+
* constructor(parent: Construct, id: string, props: RestApiLambdaProps) {
|
|
25
|
+
* super(parent, id, props)
|
|
26
|
+
* this.props = props
|
|
27
|
+
* this.id = id
|
|
28
|
+
* this.initResources()
|
|
29
|
+
* }
|
|
30
|
+
* }
|
|
31
|
+
* @mixin
|
|
32
|
+
*/
|
|
33
|
+
export declare class RestApiLambda extends CommonConstruct {
|
|
34
|
+
props: RestApiLambdaProps;
|
|
35
|
+
id: string;
|
|
36
|
+
applicationSecrets: secretsmanager.ISecret[];
|
|
37
|
+
restApiLambdaPolicy: iam.PolicyDocument;
|
|
38
|
+
restApiLambdaRole: iam.Role;
|
|
39
|
+
restApiLambdaEnvironment: RestApiLambdaEnvironment;
|
|
40
|
+
restApiLambdaLayers: lambda.LayerVersion[];
|
|
41
|
+
restApiLambdaFunction: lambda.Function;
|
|
42
|
+
restApi: apig.RestApi;
|
|
43
|
+
restApiHostedZone: route53.IHostedZone;
|
|
44
|
+
restApiCertificate: acm.ICertificate;
|
|
45
|
+
restApiDomain: apig.DomainName;
|
|
46
|
+
restApiBasePathMappings: apig.BasePathMapping[];
|
|
47
|
+
constructor(parent: Construct, id: string, props: RestApiLambdaProps);
|
|
48
|
+
/**
|
|
49
|
+
* @summary Initialise and provision resources
|
|
50
|
+
* @protected
|
|
51
|
+
*/
|
|
52
|
+
protected initResources(): void;
|
|
53
|
+
/**
|
|
54
|
+
* @summary Method to resolve secrets from SecretsManager
|
|
55
|
+
* - To be implemented in the overriding method in the implementation class
|
|
56
|
+
* @protected
|
|
57
|
+
*/
|
|
58
|
+
protected resolveSecrets(): void;
|
|
59
|
+
/**
|
|
60
|
+
* @summary Method to resolve a hosted zone based on domain attributes
|
|
61
|
+
* @protected
|
|
62
|
+
*/
|
|
63
|
+
protected resolveHostedZone(): void;
|
|
64
|
+
/**
|
|
65
|
+
* @summary Method to resolve a certificate based on attributes
|
|
66
|
+
* @protected
|
|
67
|
+
*/
|
|
68
|
+
protected resolveCertificate(): void;
|
|
69
|
+
/**
|
|
70
|
+
* @summary Method to create iam policy for RestApi Lambda function
|
|
71
|
+
* @protected
|
|
72
|
+
*/
|
|
73
|
+
protected createLambdaPolicy(): void;
|
|
74
|
+
/**
|
|
75
|
+
* @summary Method to create iam role for RestApi Lambda function
|
|
76
|
+
* @protected
|
|
77
|
+
*/
|
|
78
|
+
protected createLambdaRole(): void;
|
|
79
|
+
/**
|
|
80
|
+
* @summary Method to create environment variables for RestApi Lambda function
|
|
81
|
+
* @protected
|
|
82
|
+
*/
|
|
83
|
+
protected createLambdaEnvironment(): void;
|
|
84
|
+
/**
|
|
85
|
+
* @summary Method to create layers for RestApi Lambda function
|
|
86
|
+
* @protected
|
|
87
|
+
*/
|
|
88
|
+
protected createLambdaLayers(): void;
|
|
89
|
+
/**
|
|
90
|
+
* @summary Method to create lambda function for RestApi
|
|
91
|
+
* @protected
|
|
92
|
+
*/
|
|
93
|
+
protected createLambdaFunction(): void;
|
|
94
|
+
/**
|
|
95
|
+
* @summary Method to create rest restApiLambda for RestApi
|
|
96
|
+
* @protected
|
|
97
|
+
*/
|
|
98
|
+
protected createRestApi(): void;
|
|
99
|
+
/**
|
|
100
|
+
* @summary Method to create custom restApiLambda domain for RestApi API
|
|
101
|
+
* @protected
|
|
102
|
+
*/
|
|
103
|
+
protected createApiDomain(): void;
|
|
104
|
+
/**
|
|
105
|
+
* @summary Method to create base path mappings for RestApi API
|
|
106
|
+
* @protected
|
|
107
|
+
*/
|
|
108
|
+
protected createApiBasePathMapping(): void;
|
|
109
|
+
/**
|
|
110
|
+
* @summary Method to create route53 records for RestApi API
|
|
111
|
+
* @protected
|
|
112
|
+
*/
|
|
113
|
+
protected createApiRouteAssets(): void;
|
|
114
|
+
}
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.RestApiLambda = void 0;
|
|
27
|
+
const apig = __importStar(require("aws-cdk-lib/aws-apigateway"));
|
|
28
|
+
const iam = __importStar(require("aws-cdk-lib/aws-iam"));
|
|
29
|
+
const common_1 = require("../../common");
|
|
30
|
+
/**
|
|
31
|
+
* @deprecated Use RestApiLambda instead. This will be removed in a future release.
|
|
32
|
+
*
|
|
33
|
+
* @category cdk-utils.rest-api-lambda
|
|
34
|
+
* @subcategory construct
|
|
35
|
+
* @classdesc Provides a construct to create and deploy a RestApi as Lambda
|
|
36
|
+
*
|
|
37
|
+
* <b>Architecture</b><br/> 
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* import { RestApiLambda, RestApiLambdaProps } '@gradientedge/cdk-utils'
|
|
41
|
+
* import { Construct } from 'constructs'
|
|
42
|
+
*
|
|
43
|
+
* class CustomConstruct extends RestApiLambda {
|
|
44
|
+
* constructor(parent: Construct, id: string, props: RestApiLambdaProps) {
|
|
45
|
+
* super(parent, id, props)
|
|
46
|
+
* this.props = props
|
|
47
|
+
* this.id = id
|
|
48
|
+
* this.initResources()
|
|
49
|
+
* }
|
|
50
|
+
* }
|
|
51
|
+
* @mixin
|
|
52
|
+
*/
|
|
53
|
+
class RestApiLambda extends common_1.CommonConstruct {
|
|
54
|
+
/* restApiLambda props */
|
|
55
|
+
props;
|
|
56
|
+
id;
|
|
57
|
+
/* restApiLambda resources */
|
|
58
|
+
applicationSecrets;
|
|
59
|
+
restApiLambdaPolicy;
|
|
60
|
+
restApiLambdaRole;
|
|
61
|
+
restApiLambdaEnvironment;
|
|
62
|
+
restApiLambdaLayers = [];
|
|
63
|
+
restApiLambdaFunction;
|
|
64
|
+
restApi;
|
|
65
|
+
restApiHostedZone;
|
|
66
|
+
restApiCertificate;
|
|
67
|
+
restApiDomain;
|
|
68
|
+
restApiBasePathMappings = [];
|
|
69
|
+
constructor(parent, id, props) {
|
|
70
|
+
super(parent, id, props);
|
|
71
|
+
this.props = props;
|
|
72
|
+
this.id = id;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* @summary Initialise and provision resources
|
|
76
|
+
* @protected
|
|
77
|
+
*/
|
|
78
|
+
initResources() {
|
|
79
|
+
this.resolveSecrets();
|
|
80
|
+
this.resolveHostedZone();
|
|
81
|
+
this.resolveCertificate();
|
|
82
|
+
this.createLambdaPolicy();
|
|
83
|
+
this.createLambdaRole();
|
|
84
|
+
this.createLambdaEnvironment();
|
|
85
|
+
this.createLambdaLayers();
|
|
86
|
+
this.createLambdaFunction();
|
|
87
|
+
this.createRestApi();
|
|
88
|
+
this.createApiDomain();
|
|
89
|
+
this.createApiBasePathMapping();
|
|
90
|
+
this.createApiRouteAssets();
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* @summary Method to resolve secrets from SecretsManager
|
|
94
|
+
* - To be implemented in the overriding method in the implementation class
|
|
95
|
+
* @protected
|
|
96
|
+
*/
|
|
97
|
+
resolveSecrets() {
|
|
98
|
+
this.applicationSecrets = [];
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* @summary Method to resolve a hosted zone based on domain attributes
|
|
102
|
+
* @protected
|
|
103
|
+
*/
|
|
104
|
+
resolveHostedZone() {
|
|
105
|
+
this.restApiHostedZone = this.route53Manager.withHostedZoneFromFullyQualifiedDomainName(`${this.id}-hosted-zone`, this, this.props.useExistingHostedZone);
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* @summary Method to resolve a certificate based on attributes
|
|
109
|
+
* @protected
|
|
110
|
+
*/
|
|
111
|
+
resolveCertificate() {
|
|
112
|
+
if (this.props.restApiCertificate.useExistingCertificate &&
|
|
113
|
+
this.props.restApiCertificate.certificateSsmName &&
|
|
114
|
+
this.props.restApiCertificate.certificateRegion) {
|
|
115
|
+
this.props.restApiCertificate.certificateArn = this.ssmManager.readStringParameterFromRegion(`${this.id}-certificate-param`, this, this.props.restApiCertificate.certificateSsmName, this.props.restApiCertificate.certificateRegion);
|
|
116
|
+
}
|
|
117
|
+
this.restApiCertificate = this.acmManager.resolveCertificate(`${this.id}-certificate`, this, this.props.restApiCertificate);
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* @summary Method to create iam policy for RestApi Lambda function
|
|
121
|
+
* @protected
|
|
122
|
+
*/
|
|
123
|
+
createLambdaPolicy() {
|
|
124
|
+
this.restApiLambdaPolicy = new iam.PolicyDocument({
|
|
125
|
+
statements: [this.iamManager.statementForCreateAnyLogStream()],
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* @summary Method to create iam role for RestApi Lambda function
|
|
130
|
+
* @protected
|
|
131
|
+
*/
|
|
132
|
+
createLambdaRole() {
|
|
133
|
+
this.restApiLambdaRole = this.iamManager.createRoleForLambda(`${this.id}-lambda-role`, this, this.restApiLambdaPolicy);
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* @summary Method to create environment variables for RestApi Lambda function
|
|
137
|
+
* @protected
|
|
138
|
+
*/
|
|
139
|
+
createLambdaEnvironment() {
|
|
140
|
+
this.restApiLambdaEnvironment = {
|
|
141
|
+
NODE_ENV: this.props.nodeEnv,
|
|
142
|
+
LOG_LEVEL: this.props.logLevel,
|
|
143
|
+
TZ: this.props.timezone,
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* @summary Method to create layers for RestApi Lambda function
|
|
148
|
+
* @protected
|
|
149
|
+
*/
|
|
150
|
+
createLambdaLayers() {
|
|
151
|
+
const layers = [];
|
|
152
|
+
if (!this.props.restApiLambdaLayerSources)
|
|
153
|
+
return;
|
|
154
|
+
this.props.restApiLambdaLayerSources.forEach((source, index) => {
|
|
155
|
+
layers.push(this.lambdaManager.createLambdaLayer(`${this.id}-layer-${index}`, this, source));
|
|
156
|
+
});
|
|
157
|
+
this.restApiLambdaLayers = layers;
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* @summary Method to create lambda function for RestApi
|
|
161
|
+
* @protected
|
|
162
|
+
*/
|
|
163
|
+
createLambdaFunction() {
|
|
164
|
+
this.restApiLambdaFunction = this.lambdaManager.createLambdaFunction(`${this.id}-restapi-server`, this, this.props.restApiLambda, this.restApiLambdaRole, this.restApiLambdaLayers, this.props.restApiSource, this.props.restApiHandler || 'index.handler', this.restApiLambdaEnvironment);
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* @summary Method to create rest restApiLambda for RestApi
|
|
168
|
+
* @protected
|
|
169
|
+
*/
|
|
170
|
+
createRestApi() {
|
|
171
|
+
this.restApi = this.apiManager.createLambdaRestApi(`${this.id}-lambda-rest-api`, this, this.props.restApi, this.restApiLambdaFunction);
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* @summary Method to create custom restApiLambda domain for RestApi API
|
|
175
|
+
* @protected
|
|
176
|
+
*/
|
|
177
|
+
createApiDomain() {
|
|
178
|
+
this.restApiDomain = this.apiManager.createApiDomain(`${this.id}-api-domain`, this, this.isProductionStage() || this.props.skipStageForARecords
|
|
179
|
+
? `${this.props.apiSubDomain}.${this.fullyQualifiedDomainName}`
|
|
180
|
+
: `${this.props.apiSubDomain}-${this.props.stage}.${this.fullyQualifiedDomainName}`, this.restApiCertificate);
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* @summary Method to create base path mappings for RestApi API
|
|
184
|
+
* @protected
|
|
185
|
+
*/
|
|
186
|
+
createApiBasePathMapping() {
|
|
187
|
+
const apiRootPaths = this.props.apiRootPaths;
|
|
188
|
+
if (apiRootPaths && apiRootPaths.length > 0) {
|
|
189
|
+
apiRootPaths.forEach((apiRootPath) => {
|
|
190
|
+
this.restApiBasePathMappings.push(new apig.BasePathMapping(this, `${this.id}-base-bath-mapping-${apiRootPath}`, {
|
|
191
|
+
basePath: apiRootPath,
|
|
192
|
+
domainName: this.restApiDomain,
|
|
193
|
+
restApi: this.restApi,
|
|
194
|
+
stage: this.restApi.deploymentStage,
|
|
195
|
+
}));
|
|
196
|
+
});
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
// add default mapping if apiRootPaths not set
|
|
200
|
+
new apig.BasePathMapping(this, `${this.id}-base-bath-mapping`, {
|
|
201
|
+
domainName: this.restApiDomain,
|
|
202
|
+
restApi: this.restApi,
|
|
203
|
+
stage: this.restApi.deploymentStage,
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* @summary Method to create route53 records for RestApi API
|
|
208
|
+
* @protected
|
|
209
|
+
*/
|
|
210
|
+
createApiRouteAssets() {
|
|
211
|
+
this.route53Manager.createApiGatewayARecord(`${this.id}-custom-domain-a-record`, this, this.props.apiSubDomain, this.restApiDomain, this.restApiHostedZone, this.props.skipStageForARecords);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
exports.RestApiLambda = RestApiLambda;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './main';
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./main"), exports);
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import * as ec2 from 'aws-cdk-lib/aws-ec2';
|
|
2
|
+
import * as elasticache from 'aws-cdk-lib/aws-elasticache';
|
|
3
|
+
import { Construct } from 'constructs';
|
|
4
|
+
import { RestApiLambda } from '..';
|
|
5
|
+
import { RestApiLambdaWithCacheProps } from '../../types';
|
|
6
|
+
/**
|
|
7
|
+
* @stability stable
|
|
8
|
+
* @category cdk-utils.rest-api-lambda-with-cache
|
|
9
|
+
* @subcategory construct
|
|
10
|
+
* @classdesc Provides a construct to create and deploy a RestApi API as Lambda with Caching
|
|
11
|
+
*
|
|
12
|
+
* <b>Architecture</b><br/> 
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* import { RestApiLambdaWithCacheProps } '@gradientedge/cdk-utils'
|
|
16
|
+
* import { Construct } from 'constructs'
|
|
17
|
+
*
|
|
18
|
+
* class CustomConstruct extends RestApiLambdaWithCache {
|
|
19
|
+
* constructor(parent: Construct, id: string, props: RestApiLambdaWithCacheProps) {
|
|
20
|
+
* super(parent, id, props)
|
|
21
|
+
* this.props = props
|
|
22
|
+
* this.id = id
|
|
23
|
+
* this.initResources()
|
|
24
|
+
* }
|
|
25
|
+
* }
|
|
26
|
+
* @mixin
|
|
27
|
+
*/
|
|
28
|
+
export declare class RestApiLambdaWithCache extends RestApiLambda {
|
|
29
|
+
props: RestApiLambdaWithCacheProps;
|
|
30
|
+
id: string;
|
|
31
|
+
restApivpc: ec2.IVpc;
|
|
32
|
+
restApiCache: elasticache.CfnReplicationGroup;
|
|
33
|
+
restApiSecurityGroup: ec2.ISecurityGroup;
|
|
34
|
+
restApiSecurityGroupExportName: string;
|
|
35
|
+
constructor(parent: Construct, id: string, props: RestApiLambdaWithCacheProps);
|
|
36
|
+
protected initResources(): void;
|
|
37
|
+
/**
|
|
38
|
+
* Create VPC
|
|
39
|
+
* @protected
|
|
40
|
+
*/
|
|
41
|
+
protected resolveVpc(): void;
|
|
42
|
+
/**
|
|
43
|
+
* Resolve Security Group
|
|
44
|
+
* @protected
|
|
45
|
+
*/
|
|
46
|
+
protected resolveSecurityGroup(): void;
|
|
47
|
+
/**
|
|
48
|
+
* Create ElastiCache
|
|
49
|
+
* @protected
|
|
50
|
+
*/
|
|
51
|
+
protected createElastiCache(): void;
|
|
52
|
+
/**
|
|
53
|
+
* Create Lambda Role
|
|
54
|
+
* @protected
|
|
55
|
+
*/
|
|
56
|
+
protected createLambdaRole(): void;
|
|
57
|
+
/**
|
|
58
|
+
* @summary Method to create environment variables for RestApi Lambda function
|
|
59
|
+
* @protected
|
|
60
|
+
*/
|
|
61
|
+
protected createLambdaEnvironment(): void;
|
|
62
|
+
/**
|
|
63
|
+
* @summary Method to create lambda function for RestApi API
|
|
64
|
+
* @protected
|
|
65
|
+
*/
|
|
66
|
+
protected createLambdaFunction(): void;
|
|
67
|
+
}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.RestApiLambdaWithCache = void 0;
|
|
27
|
+
const cdk = __importStar(require("aws-cdk-lib"));
|
|
28
|
+
const ec2 = __importStar(require("aws-cdk-lib/aws-ec2"));
|
|
29
|
+
const iam = __importStar(require("aws-cdk-lib/aws-iam"));
|
|
30
|
+
const __1 = require("..");
|
|
31
|
+
const utils = __importStar(require("../../utils"));
|
|
32
|
+
/**
|
|
33
|
+
* @stability stable
|
|
34
|
+
* @category cdk-utils.rest-api-lambda-with-cache
|
|
35
|
+
* @subcategory construct
|
|
36
|
+
* @classdesc Provides a construct to create and deploy a RestApi API as Lambda with Caching
|
|
37
|
+
*
|
|
38
|
+
* <b>Architecture</b><br/> 
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* import { RestApiLambdaWithCacheProps } '@gradientedge/cdk-utils'
|
|
42
|
+
* import { Construct } from 'constructs'
|
|
43
|
+
*
|
|
44
|
+
* class CustomConstruct extends RestApiLambdaWithCache {
|
|
45
|
+
* constructor(parent: Construct, id: string, props: RestApiLambdaWithCacheProps) {
|
|
46
|
+
* super(parent, id, props)
|
|
47
|
+
* this.props = props
|
|
48
|
+
* this.id = id
|
|
49
|
+
* this.initResources()
|
|
50
|
+
* }
|
|
51
|
+
* }
|
|
52
|
+
* @mixin
|
|
53
|
+
*/
|
|
54
|
+
class RestApiLambdaWithCache extends __1.RestApiLambda {
|
|
55
|
+
/* restApiLambdaWithCache props */
|
|
56
|
+
props;
|
|
57
|
+
id;
|
|
58
|
+
/* restApiLambda resources */
|
|
59
|
+
restApivpc;
|
|
60
|
+
restApiCache;
|
|
61
|
+
restApiSecurityGroup;
|
|
62
|
+
restApiSecurityGroupExportName;
|
|
63
|
+
constructor(parent, id, props) {
|
|
64
|
+
super(parent, id, props);
|
|
65
|
+
this.props = props;
|
|
66
|
+
this.id = id;
|
|
67
|
+
}
|
|
68
|
+
initResources() {
|
|
69
|
+
this.resolveVpc();
|
|
70
|
+
this.resolveSecurityGroup();
|
|
71
|
+
this.createElastiCache();
|
|
72
|
+
super.initResources();
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Create VPC
|
|
76
|
+
* @protected
|
|
77
|
+
*/
|
|
78
|
+
resolveVpc() {
|
|
79
|
+
if (this.props.useExistingVpc) {
|
|
80
|
+
this.restApivpc = this.vpcManager.retrieveCommonVpc(`${this.id}`, this, this.props.vpcName);
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
this.restApivpc = this.vpcManager.createCommonVpc(this, this.props.restApiVpc, this.props.restApiVpc.vpcName);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Resolve Security Group
|
|
88
|
+
* @protected
|
|
89
|
+
*/
|
|
90
|
+
resolveSecurityGroup() {
|
|
91
|
+
if (this.props.securityGroupExportName) {
|
|
92
|
+
this.restApiSecurityGroup = ec2.SecurityGroup.fromSecurityGroupId(this, `${this.id}`, cdk.Fn.importValue(this.props.securityGroupExportName));
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
this.restApiSecurityGroup = new ec2.SecurityGroup(this, `${this.id}-security-group-${this.props.stage}`, {
|
|
96
|
+
securityGroupName: `${this.id}-security-group-${this.props.stage}`,
|
|
97
|
+
vpc: this.restApivpc,
|
|
98
|
+
});
|
|
99
|
+
this.restApiSecurityGroup.addIngressRule(ec2.Peer.anyIpv4(), ec2.Port.allTraffic(), 'All Traffic');
|
|
100
|
+
utils.createCfnOutput(`${this.id}-security-group-id`, this, this.restApiSecurityGroup.securityGroupId);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Create ElastiCache
|
|
105
|
+
* @protected
|
|
106
|
+
*/
|
|
107
|
+
createElastiCache() {
|
|
108
|
+
this.restApiCache = this.elasticacheManager.createReplicatedElastiCache(`${this.id}-elasticache`, this, this.props.restApiElastiCache, this.restApivpc.privateSubnets.map(subnet => subnet.subnetId), [this.restApiSecurityGroup.securityGroupId]);
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Create Lambda Role
|
|
112
|
+
* @protected
|
|
113
|
+
*/
|
|
114
|
+
createLambdaRole() {
|
|
115
|
+
super.createLambdaRole();
|
|
116
|
+
this.restApiLambdaRole.addManagedPolicy(iam.ManagedPolicy.fromAwsManagedPolicyName('service-role/AWSLambdaVPCAccessExecutionRole'));
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* @summary Method to create environment variables for RestApi Lambda function
|
|
120
|
+
* @protected
|
|
121
|
+
*/
|
|
122
|
+
createLambdaEnvironment() {
|
|
123
|
+
this.restApiLambdaEnvironment = {
|
|
124
|
+
NODE_ENV: this.props.nodeEnv,
|
|
125
|
+
LOG_LEVEL: this.props.logLevel,
|
|
126
|
+
TZ: this.props.timezone,
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* @summary Method to create lambda function for RestApi API
|
|
131
|
+
* @protected
|
|
132
|
+
*/
|
|
133
|
+
createLambdaFunction() {
|
|
134
|
+
this.restApiLambdaFunction = this.lambdaManager.createLambdaFunction(`${this.id}-restapi-server`, this, this.props.restApiLambda, this.restApiLambdaRole, this.restApiLambdaLayers, this.props.restApiSource, this.props.restApiHandler || 'index.handler', this.restApiLambdaEnvironment, this.restApivpc, [this.restApiSecurityGroup], undefined, undefined, this.restApivpc);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
exports.RestApiLambdaWithCache = RestApiLambdaWithCache;
|
|
@@ -232,7 +232,9 @@ class SiteWithEcsBackend extends common_1.CommonConstruct {
|
|
|
232
232
|
enableECSManagedTags: true,
|
|
233
233
|
serviceName: `${this.id}-${this.props.stage}`,
|
|
234
234
|
cpu: this.props.siteTask.cpu,
|
|
235
|
-
loadBalancerName:
|
|
235
|
+
loadBalancerName: this.props.siteTask.loadBalancerName
|
|
236
|
+
? `${this.props.siteTask.loadBalancerName}-${this.props.stage}`
|
|
237
|
+
: `${this.id}-${this.props.stage}`,
|
|
236
238
|
certificate: this.siteRegionalCertificate,
|
|
237
239
|
domainName: this.siteInternalDomainName,
|
|
238
240
|
domainZone: this.siteHostedZone,
|
|
@@ -310,6 +312,8 @@ class SiteWithEcsBackend extends common_1.CommonConstruct {
|
|
|
310
312
|
/* allow access to/from EFS from Fargate ECS service */
|
|
311
313
|
this.siteFileSystem.connections.allowDefaultPortFrom(this.siteEcsService.connections);
|
|
312
314
|
this.siteFileSystem.connections.allowDefaultPortTo(this.siteEcsService.connections);
|
|
315
|
+
/* add EFS permissions to ECS Role */
|
|
316
|
+
this.siteEcsRole.addToPolicy(new iam.PolicyStatement(this.iamManager.statementForWriteEfs([this.siteFileSystem.fileSystemArn])));
|
|
313
317
|
/* add the efs volume to ecs task definition */
|
|
314
318
|
this.siteEcsTaskDefinition.addVolume({
|
|
315
319
|
name: `${this.id}-fs`,
|
|
@@ -100,6 +100,12 @@ export declare class IamManager {
|
|
|
100
100
|
* @param {string[]} resourceArns list of ARNs to allow access to
|
|
101
101
|
*/
|
|
102
102
|
statementForCloudfrontInvalidation(resourceArns?: string[]): cdk.aws_iam.PolicyStatement;
|
|
103
|
+
/**
|
|
104
|
+
* @summary Method to create iam statement to access efs
|
|
105
|
+
* @param {common.CommonConstruct} scope scope in which this resource is defined
|
|
106
|
+
* @param {string[]} resourceArns list of ARNs to allow access to
|
|
107
|
+
*/
|
|
108
|
+
statementForWriteEfs(resourceArns?: string[]): cdk.aws_iam.PolicyStatement;
|
|
103
109
|
/**
|
|
104
110
|
* @summary Method to create iam policy to invalidate cloudfront cache
|
|
105
111
|
* @param {string[]} resourceArns list of ARNs to allow access to
|
|
@@ -214,6 +214,18 @@ class IamManager {
|
|
|
214
214
|
resources: resourceArns ?? ['*'],
|
|
215
215
|
});
|
|
216
216
|
}
|
|
217
|
+
/**
|
|
218
|
+
* @summary Method to create iam statement to access efs
|
|
219
|
+
* @param {common.CommonConstruct} scope scope in which this resource is defined
|
|
220
|
+
* @param {string[]} resourceArns list of ARNs to allow access to
|
|
221
|
+
*/
|
|
222
|
+
statementForWriteEfs(resourceArns) {
|
|
223
|
+
return new iam.PolicyStatement({
|
|
224
|
+
effect: iam.Effect.ALLOW,
|
|
225
|
+
actions: ['elasticfilesystem:*'],
|
|
226
|
+
resources: resourceArns ?? ['*'],
|
|
227
|
+
});
|
|
228
|
+
}
|
|
217
229
|
/**
|
|
218
230
|
* @summary Method to create iam policy to invalidate cloudfront cache
|
|
219
231
|
* @param {string[]} resourceArns list of ARNs to allow access to
|