-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathcustom-resource-provider.ts
315 lines (277 loc) · 9.96 KB
/
custom-resource-provider.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import * as fs from 'fs';
import * as path from 'path';
import * as cxapi from '@aws-cdk/cx-api';
import { Construct } from 'constructs';
import { AssetStaging } from '../asset-staging';
import { FileAssetPackaging } from '../assets';
import { CfnResource } from '../cfn-resource';
import { Duration } from '../duration';
import { Size } from '../size';
import { Stack } from '../stack';
import { Token } from '../token';
const ENTRYPOINT_FILENAME = '__entrypoint__';
const ENTRYPOINT_NODEJS_SOURCE = path.join(__dirname, 'nodejs-entrypoint.js');
// v2 - keep this import as a separate section to reduce merge conflict when forward merging with the v2 branch.
// eslint-disable-next-line
import { Construct as CoreConstruct } from '../construct-compat';
/**
* Initialization properties for `CustomResourceProvider`.
*
*/
export interface CustomResourceProviderProps {
/**
* A local file system directory with the provider's code. The code will be
* bundled into a zip asset and wired to the provider's AWS Lambda function.
*/
readonly codeDirectory: string;
/**
* The AWS Lambda runtime and version to use for the provider.
*/
readonly runtime: CustomResourceProviderRuntime;
/**
* A set of IAM policy statements to include in the inline policy of the
* provider's lambda function.
*
* **Please note**: these are direct IAM JSON policy blobs, *not* `iam.PolicyStatement`
* objects like you will see in the rest of the CDK.
*
* @default - no additional inline policy
*
* @example
* const provider = CustomResourceProvider.getOrCreateProvider(this, 'Custom::MyCustomResourceType', {
* codeDirectory: `${__dirname}/my-handler`,
* runtime: CustomResourceProviderRuntime.NODEJS_14_X,
* policyStatements: [
* {
* Effect: 'Allow',
* Action: 's3:PutObject*',
* Resource: '*',
* }
* ],
* });
*/
readonly policyStatements?: any[];
/**
* AWS Lambda timeout for the provider.
*
* @default Duration.minutes(15)
*/
readonly timeout?: Duration;
/**
* The amount of memory that your function has access to. Increasing the
* function's memory also increases its CPU allocation.
*
* @default Size.mebibytes(128)
*/
readonly memorySize?: Size;
/**
* Key-value pairs that are passed to Lambda as Environment
*
* @default - No environment variables.
*/
readonly environment?: { [key: string]: string };
/**
* A description of the function.
*
* @default - No description.
*/
readonly description?: string;
}
/**
* The lambda runtime to use for the resource provider. This also indicates
* which language is used for the handler.
*/
export enum CustomResourceProviderRuntime {
/**
* Node.js 12.x
*
* @deprecated Use {@link NODEJS_14_X}
*/
NODEJS_12 = 'deprecated_nodejs12.x',
/**
* Node.js 12.x
*/
NODEJS_12_X = 'nodejs12.x',
/**
* Node.js 14.x
*/
NODEJS_14_X = 'nodejs14.x',
/**
* Node.js 16.x
*/
NODEJS_16_X = 'nodejs16.x',
}
/**
* An AWS-Lambda backed custom resource provider, for CDK Construct Library constructs
*
* This is a provider for `CustomResource` constructs, backed by an AWS Lambda
* Function. It only supports NodeJS runtimes.
*
* **This is not a generic custom resource provider class**. It is specifically
* intended to be used only by constructs in the AWS CDK Construct Library, and
* only exists here because of reverse dependency issues (for example, it cannot
* use `iam.PolicyStatement` objects, since the `iam` library already depends on
* the CDK `core` library and we cannot have cyclic dependencies).
*
* If you are not writing constructs for the AWS Construct Library, you should
* use the `Provider` class in the `custom-resources` module instead, which has
* a better API and supports all Lambda runtimes, not just Node.
*
* N.B.: When you are writing Custom Resource Providers, there are a number of
* lifecycle events you have to pay attention to. These are documented in the
* README of the `custom-resources` module. Be sure to give the documentation
* in that module a read, regardless of whether you end up using the Provider
* class in there or this one.
*/
export class CustomResourceProvider extends CoreConstruct {
/**
* Returns a stack-level singleton ARN (service token) for the custom resource
* provider.
*
* @param scope Construct scope
* @param uniqueid A globally unique id that will be used for the stack-level
* construct.
* @param props Provider properties which will only be applied when the
* provider is first created.
* @returns the service token of the custom resource provider, which should be
* used when defining a `CustomResource`.
*/
public static getOrCreate(scope: Construct, uniqueid: string, props: CustomResourceProviderProps) {
return this.getOrCreateProvider(scope, uniqueid, props).serviceToken;
}
/**
* Returns a stack-level singleton for the custom resource provider.
*
* @param scope Construct scope
* @param uniqueid A globally unique id that will be used for the stack-level
* construct.
* @param props Provider properties which will only be applied when the
* provider is first created.
* @returns the service token of the custom resource provider, which should be
* used when defining a `CustomResource`.
*/
public static getOrCreateProvider(scope: Construct, uniqueid: string, props: CustomResourceProviderProps) {
const id = `${uniqueid}CustomResourceProvider`;
const stack = Stack.of(scope);
const provider = stack.node.tryFindChild(id) as CustomResourceProvider
?? new CustomResourceProvider(stack, id, props);
return provider;
}
/**
* The ARN of the provider's AWS Lambda function which should be used as the
* `serviceToken` when defining a custom resource.
*
* @example
* declare const myProvider: CustomResourceProvider;
*
* new CustomResource(this, 'MyCustomResource', {
* serviceToken: myProvider.serviceToken,
* properties: {
* myPropertyOne: 'one',
* myPropertyTwo: 'two',
* },
* });
*/
public readonly serviceToken: string;
/**
* The ARN of the provider's AWS Lambda function role.
*/
public readonly roleArn: string;
/**
* The hash of the lambda code backing this provider. Can be used to trigger updates
* on code changes, even when the properties of a custom resource remain unchanged.
*/
public readonly codeHash: string;
protected constructor(scope: Construct, id: string, props: CustomResourceProviderProps) {
super(scope, id);
const stack = Stack.of(scope);
// copy the entry point to the code directory
fs.copyFileSync(ENTRYPOINT_NODEJS_SOURCE, path.join(props.codeDirectory, `${ENTRYPOINT_FILENAME}.js`));
// verify we have an index file there
if (!fs.existsSync(path.join(props.codeDirectory, 'index.js'))) {
throw new Error(`cannot find ${props.codeDirectory}/index.js`);
}
const staging = new AssetStaging(this, 'Staging', {
sourcePath: props.codeDirectory,
});
const assetFileName = staging.relativeStagedPath(stack);
const asset = stack.synthesizer.addFileAsset({
fileName: assetFileName,
sourceHash: staging.assetHash,
packaging: FileAssetPackaging.ZIP_DIRECTORY,
});
const policies = !props.policyStatements ? undefined : [
{
PolicyName: 'Inline',
PolicyDocument: {
Version: '2012-10-17',
Statement: props.policyStatements,
},
},
];
const role = new CfnResource(this, 'Role', {
type: 'AWS::IAM::Role',
properties: {
AssumeRolePolicyDocument: {
Version: '2012-10-17',
Statement: [{ Action: 'sts:AssumeRole', Effect: 'Allow', Principal: { Service: 'lambda.amazonaws.com' } }],
},
ManagedPolicyArns: [
{ 'Fn::Sub': 'arn:${AWS::Partition}:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole' },
],
Policies: policies,
},
});
this.roleArn = Token.asString(role.getAtt('Arn'));
const timeout = props.timeout ?? Duration.minutes(15);
const memory = props.memorySize ?? Size.mebibytes(128);
const handler = new CfnResource(this, 'Handler', {
type: 'AWS::Lambda::Function',
properties: {
Code: {
S3Bucket: asset.bucketName,
S3Key: asset.objectKey,
},
Timeout: timeout.toSeconds(),
MemorySize: memory.toMebibytes(),
Handler: `${ENTRYPOINT_FILENAME}.handler`,
Role: role.getAtt('Arn'),
Runtime: customResourceProviderRuntimeToString(props.runtime),
Environment: this.renderEnvironmentVariables(props.environment),
Description: props.description ?? undefined,
},
});
handler.addDependsOn(role);
if (this.node.tryGetContext(cxapi.ASSET_RESOURCE_METADATA_ENABLED_CONTEXT)) {
handler.addMetadata(cxapi.ASSET_RESOURCE_METADATA_PATH_KEY, assetFileName);
handler.addMetadata(cxapi.ASSET_RESOURCE_METADATA_PROPERTY_KEY, 'Code');
}
this.serviceToken = Token.asString(handler.getAtt('Arn'));
this.codeHash = staging.assetHash;
}
private renderEnvironmentVariables(env?: { [key: string]: string }) {
if (!env || Object.keys(env).length === 0) {
return undefined;
}
// Sort environment so the hash of the function used to create
// `currentVersion` is not affected by key order (this is how lambda does
// it)
const variables: { [key: string]: string } = {};
const keys = Object.keys(env).sort();
for (const key of keys) {
variables[key] = env[key];
}
return { Variables: variables };
}
}
function customResourceProviderRuntimeToString(x: CustomResourceProviderRuntime): string {
switch (x) {
case CustomResourceProviderRuntime.NODEJS_12:
case CustomResourceProviderRuntime.NODEJS_12_X:
return 'nodejs12.x';
case CustomResourceProviderRuntime.NODEJS_14_X:
return 'nodejs14.x';
case CustomResourceProviderRuntime.NODEJS_16_X:
return 'nodejs16.x';
}
}