-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathexport-backend.ts
244 lines (224 loc) · 8.1 KB
/
export-backend.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import {
CfnInclude,
IncludedNestedStack,
} from 'aws-cdk-lib/cloudformation-include';
import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import { AmplifyExportedBackendProps } from './amplify-exported-backend-props';
import { BaseAmplifyExportedBackend } from './base-exported-backend';
import { Constants } from './constants';
import { AmplifyExportAssetHandler } from './export-backend-asset-handler';
import {
APIGraphQLIncludedNestedStack,
APIRestIncludedStack,
AuthIncludedNestedStack,
} from './include-nested-stacks';
import {
LambdaFunctionIncludedNestedStack,
} from './include-nested-stacks/lambda-function/lambda-function-nested-stack';
import { CategoryStackMapping } from './types/category-stack-mapping';
import { AwsCustomResource, AwsCustomResourcePolicy, PhysicalResourceId } from "aws-cdk-lib/custom-resources";
import { CfnParameter } from 'aws-cdk-lib';
import { CfnRole } from 'aws-cdk-lib/aws-iam';
import { CfnBucket } from 'aws-cdk-lib/aws-s3';
const { API_CATEGORY, AUTH_CATEGORY, FUNCTION_CATEGORY } = Constants;
/***
* Used to include the backend generated by running `amplify export --out <path>` into the cdk app
* @example
* @see <amplify-export-docs-path>
*/
export class AmplifyExportedBackend
extends BaseAmplifyExportedBackend {
/**
* cfnInclude of the Amplify backend
*/
cfnInclude: CfnInclude;
/**
* The root stack created
*/
rootStack: cdk.Stack;
/**
* @param scope The parent construct of this template
* @param id The ID of this construct
* @param props Initialization properties.
*/
constructor(
scope: Construct,
id: string,
props: AmplifyExportedBackendProps,
) {
super(scope, id, props.path, props.amplifyEnvironment, props.amplifyAppId);
this.rootStack = new cdk.Stack(scope, `${id}-amplify-backend-stack`, {
...props,
stackName: this.exportBackendManifest.stackName,
});
const amplifyExportHandler = new AmplifyExportAssetHandler(
this.rootStack,
'asset',
{
backendPath: props.path,
categoryStackMapping: this.categoryStackMappings,
env: this.env,
exportManifest: this.exportBackendManifest,
},
);
this.exportBackendManifest =
amplifyExportHandler.createAssetsAndUpdateParameters();
const include = new CfnInclude(
this.rootStack,
'AmplifyCfnInclude',
this.transformTemplateFile(this.exportBackendManifest.props, this.exportPath),
);
this.cfnInclude = include;
amplifyExportHandler.setDependencies(include);
// used to emulate the input parameters that the Amplify CLI uses
const deploymentBucket = this.cfnInclude.getResource('DeploymentBucket') as CfnBucket;
const authRole = this.cfnInclude.getResource('AuthRole') as CfnRole;
const unauthRole = this.cfnInclude.getResource('UnauthRole') as CfnRole;
new CfnParameter(this.rootStack, 'AuthRoleName', {
type: 'String',
default: authRole.roleName
});
new CfnParameter(this.rootStack, 'DeploymentBucketName', {
type: 'String',
default: deploymentBucket.bucketName
});
new CfnParameter(this.rootStack, 'UnauthRoleName', {
type: 'String',
default: unauthRole.roleName
});
// just like in amplify env add, we add the backend to the amplify application
// and then deploy the CFN. This also ensures the amplify env only deletes when
// the CFN is gone too.
if(this.appId) {
new AwsCustomResource(this.rootStack, 'CreateBackendEnvironment', {
onCreate: {
service: 'Amplify',
action: 'createBackendEnvironment',
parameters: {
appId: this.appId,
environmentName: this.env,
stackName: this.rootStack.stackName,
deploymentArtifacts: deploymentBucket.bucketName,
},
physicalResourceId: PhysicalResourceId.of(`${this.appId}-${this.env}-backendEnvironment`)
},
onDelete: {
service: 'Amplify',
action: 'deleteBackendEnvironment',
parameters: {
appId: this.appId,
environmentName: this.env
}
},
policy: AwsCustomResourcePolicy.fromSdkCalls({
resources: AwsCustomResourcePolicy.ANY_RESOURCE,
}),
});
}
this.applyTags(this.rootStack, this.env);
}
private applyTags(rootStack: cdk.Stack, env: string) {
this.exportTags.forEach((tag) => {
rootStack.tags.setTag(tag.key, tag.value.replace('{project-env}', env));
});
}
/**
* Method to get the auth stack
* @returns the nested stack of type {@link AuthIncludedNestedStack}
* @throws {AmplifyCategoryNotFoundError} if the auth stack doesn't exist
* @method
* @function
*/
authNestedStack(): AuthIncludedNestedStack {
const cognitoResource = this.findResourceForNestedStack(
AUTH_CATEGORY.NAME,
AUTH_CATEGORY.SERVICE.COGNITO,
);
const stack = this.getCategoryNestedStack(cognitoResource);
return new AuthIncludedNestedStack(stack);
}
/**
* Use this to get the api graphql stack from the backend
* @returns the nested stack of type {@link APIGraphQLIncludedNestedStack}
* @throws {AmplifyCategoryNotFoundError} if the API graphql stack doesn't exist
*/
graphqlNestedStacks(): APIGraphQLIncludedNestedStack {
const categoryStackMapping = this.findResourceForNestedStack(
API_CATEGORY.NAME,
API_CATEGORY.SERVICE.APP_SYNC,
);
return new APIGraphQLIncludedNestedStack(
this.getCategoryNestedStack(categoryStackMapping),
);
}
/**
* Use this to get all the lambda functions from the backend
* @returns {LambdaFunctionIncludedNestedStack[]}
* @throws {AmplifyCategoryNotFoundError} if the no Lambda Function stacks are found
*/
lambdaFunctionNestedStacks(): LambdaFunctionIncludedNestedStack[] {
return this.filterCategory(
FUNCTION_CATEGORY.NAME,
FUNCTION_CATEGORY.SERVICE.LAMBDA_FUNCTION,
)
.map((category) => this.getCategoryNestedStack(category))
.map((stack) => new LambdaFunctionIncludedNestedStack(stack));
}
/**
* Use this to get a specific lambda function from the backend
* @returns {LambdaFunctionIncludedNestedStack}
* @param functionName the function name to get from the nested stack
* @throws {AmplifyCategoryNotFoundError} if the lambda function stack doesn't exist
*/
lambdaFunctionNestedStackByName(
functionName: string,
): LambdaFunctionIncludedNestedStack {
const category = this.findResourceForNestedStack(
FUNCTION_CATEGORY.NAME,
FUNCTION_CATEGORY.SERVICE.LAMBDA_FUNCTION,
functionName,
);
return new LambdaFunctionIncludedNestedStack(
this.getCategoryNestedStack(category),
);
}
nestedStackByCategortService(category: string, service: string) : IncludedNestedStack[] {
return this.filterCategory(category, service).map(categoryMapping => this.getCategoryNestedStack(categoryMapping));
}
/**
* Returns the stacks defined in the backend
* @param category Categories defined in Amplify CLI like function, api, auth etc
* @param resourceName @default is undefined
*/
nestedStacksByCategory(
category: string,
resourceName?: string,
): IncludedNestedStack[] {
return this.filterCategory(category, undefined, resourceName).map(
this.getCategoryNestedStack,
);
}
/**
* Use this to get rest api stack from the backend
* @param resourceName
* @return {APIRestIncludedStack} the nested of type Rest API
* @throws {AmplifyCategoryNotFoundError} if the API Rest stack doesn't exist
*/
apiRestNestedStack(resourceName: string): APIRestIncludedStack {
const categoryStackMapping = this.findResourceForNestedStack(
API_CATEGORY.NAME,
API_CATEGORY.SERVICE.API_GATEWAY,
resourceName,
);
const stack = this.getCategoryNestedStack(categoryStackMapping);
return new APIRestIncludedStack(stack, resourceName);
}
private getCategoryNestedStack(
categoryStackMapping: CategoryStackMapping,
): IncludedNestedStack {
return this.cfnInclude.getNestedStack(
categoryStackMapping.category + categoryStackMapping.resourceName,
);
}
}