-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathaws-custom-resource.ts
643 lines (569 loc) · 21.7 KB
/
aws-custom-resource.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
import { Construct } from 'constructs';
import { Logging } from './logging';
import * as ec2 from '../../../aws-ec2';
import * as iam from '../../../aws-iam';
import * as logs from '../../../aws-logs';
import * as cdk from '../../../core';
import { Annotations } from '../../../core';
import { AwsCustomResourceSingletonFunction } from '../../../custom-resource-handlers/dist/custom-resources/aws-custom-resource-provider.generated';
import * as cxapi from '../../../cx-api';
import { awsSdkToIamAction } from '../helpers-internal/sdk-info';
// Shared definition with packages/@aws-cdk/custom-resource-handlers/lib/custom-resources/aws-custom-resource-handler/shared.ts
const PHYSICAL_RESOURCE_ID_REFERENCE = 'PHYSICAL:RESOURCEID:';
/**
* Reference to the physical resource id that can be passed to the AWS operation as a parameter.
*/
export class PhysicalResourceIdReference implements cdk.IResolvable {
public readonly creationStack: string[] = cdk.captureStackTrace();
/**
* toJSON serialization to replace `PhysicalResourceIdReference` with a magic string.
*/
public toJSON() {
return PHYSICAL_RESOURCE_ID_REFERENCE;
}
public resolve(_context: cdk.IResolveContext): any {
return PHYSICAL_RESOURCE_ID_REFERENCE;
}
public toString(): string {
return PHYSICAL_RESOURCE_ID_REFERENCE;
}
}
/**
* Physical ID of the custom resource.
*/
export class PhysicalResourceId {
/**
* Extract the physical resource id from the path (dot notation) to the data in the API call response.
*/
public static fromResponse(responsePath: string): PhysicalResourceId {
return new PhysicalResourceId(responsePath, undefined);
}
/**
* Explicit physical resource id.
*/
public static of(id: string): PhysicalResourceId {
return new PhysicalResourceId(undefined, id);
}
/**
* @param responsePath Path to a response data element to be used as the physical id.
* @param id Literal string to be used as the physical id.
*/
private constructor(public readonly responsePath?: string, public readonly id?: string) { }
}
/**
* An AWS SDK call.
*
* @example
*
* new cr.AwsCustomResource(this, 'GetParameterCustomResource', {
* onUpdate: { // will also be called for a CREATE event
* service: 'SSM',
* action: 'getParameter',
* parameters: {
* Name: 'my-parameter',
* WithDecryption: true,
* },
* physicalResourceId: cr.PhysicalResourceId.fromResponse('Parameter.ARN'),
* },
* policy: cr.AwsCustomResourcePolicy.fromSdkCalls({
* resources: cr.AwsCustomResourcePolicy.ANY_RESOURCE,
* }),
* });
*
*/
export interface AwsSdkCall {
/**
* The service to call
*
* This is the name of an AWS service, in one of the following forms:
*
* - An AWS SDK for JavaScript v3 package name (`@aws-sdk/client-api-gateway`)
* - An AWS SDK for JavaScript v3 client name (`api-gateway`)
* - An AWS SDK for JavaScript v2 constructor name (`APIGateway`)
* - A lowercase AWS SDK for JavaScript v2 constructor name (`apigateway`)
*
* @see https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/index.html
*/
readonly service: string;
/**
* The service action to call
*
* This is the name of an AWS API call, in one of the following forms:
*
* - An API call name as found in the API Reference documentation (`GetObject`)
* - The API call name starting with a lowercase letter (`getObject`)
* - The AWS SDK for JavaScript v3 command class name (`GetObjectCommand`)
*
* @see https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/index.html
*/
readonly action: string;
/**
* The parameters for the service action
*
* @default - no parameters
* @see https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/index.html
*/
readonly parameters?: any;
/**
* The physical resource id of the custom resource for this call.
* Mandatory for onCreate call.
* In onUpdate, you can omit this to passthrough it from request.
*
* @default - no physical resource id
*/
readonly physicalResourceId?: PhysicalResourceId;
/**
* The regex pattern to use to catch API errors. The `code` property of the
* `Error` object will be tested against this pattern. If there is a match an
* error will not be thrown.
*
* @default - do not catch errors
*/
readonly ignoreErrorCodesMatching?: string;
/**
* API version to use for the service
*
* @see https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/locking-api-versions.html
* @default - use latest available API version
*/
readonly apiVersion?: string;
/**
* The region to send service requests to.
* **Note: Cross-region operations are generally considered an anti-pattern.**
* **Consider first deploying a stack in that region.**
*
* @default - the region where this custom resource is deployed
*/
readonly region?: string;
/**
* Restrict the data returned by the custom resource to a specific path in
* the API response. Use this to limit the data returned by the custom
* resource if working with API calls that could potentially result in custom
* response objects exceeding the hard limit of 4096 bytes.
*
* Example for ECS / updateService: 'service.deploymentConfiguration.maximumPercent'
*
* @default - return all data
*
* @deprecated use outputPaths instead
*/
readonly outputPath?: string;
/**
* Restrict the data returned by the custom resource to specific paths in
* the API response. Use this to limit the data returned by the custom
* resource if working with API calls that could potentially result in custom
* response objects exceeding the hard limit of 4096 bytes.
*
* Example for ECS / updateService: ['service.deploymentConfiguration.maximumPercent']
*
* @default - return all data
*/
readonly outputPaths?: string[];
/**
* Used for running the SDK calls in underlying lambda with a different role.
* Can be used primarily for cross-account requests to for example connect
* hostedzone with a shared vpc.
* Region controls where assumeRole call is made.
*
* Example for Route53 / associateVPCWithHostedZone
*
* @default - run without assuming role
*/
readonly assumedRoleArn?: string;
/**
* A property used to configure logging during lambda function execution.
*
* Note: The default Logging configuration is all. This configuration will enable logging on all logged data
* in the lambda handler. This includes:
* - The event object that is received by the lambda handler
* - The response received after making a API call
* - The response object that the lambda handler will return
* - SDK versioning information
* - Caught and uncaught errors
*
* @default Logging.all()
*/
readonly logging?: Logging;
}
/**
* Options for the auto-generation of policies based on the configured SDK calls.
*/
export interface SdkCallsPolicyOptions {
/**
* The resources that the calls will have access to.
*
* It is best to use specific resource ARN's when possible. However, you can also use `AwsCustomResourcePolicy.ANY_RESOURCE`
* to allow access to all resources. For example, when `onCreate` is used to create a resource which you don't
* know the physical name of in advance.
*
* Note that will apply to ALL SDK calls.
*/
readonly resources: string[];
}
/**
* The IAM Policy that will be applied to the different calls.
*/
export class AwsCustomResourcePolicy {
/**
* Use this constant to configure access to any resource.
*/
public static readonly ANY_RESOURCE = ['*'];
/**
* Explicit IAM Policy Statements.
*
* @param statements the statements to propagate to the SDK calls.
*/
public static fromStatements(statements: iam.PolicyStatement[]) {
return new AwsCustomResourcePolicy(statements, undefined);
}
/**
* Generate IAM Policy Statements from the configured SDK calls.
*
* Each SDK call with be translated to an IAM Policy Statement in the form of: `call.service:call.action` (e.g `s3:PutObject`).
*
* This policy generator assumes the IAM policy name has the same name as the API
* call. This is true in 99% of cases, but there are exceptions (for example,
* S3's `PutBucketLifecycleConfiguration` requires
* `s3:PutLifecycleConfiguration` permissions, Lambda's `Invoke` requires
* `lambda:InvokeFunction` permissions). Use `fromStatements` if you want to
* do a call that requires different IAM action names.
*
* @param options options for the policy generation
*/
public static fromSdkCalls(options: SdkCallsPolicyOptions) {
return new AwsCustomResourcePolicy([], options.resources);
}
/**
* @param statements statements for explicit policy.
* @param resources resources for auto-generated from SDK calls.
*/
private constructor(public readonly statements: iam.PolicyStatement[], public readonly resources?: string[]) {}
}
/**
* Properties for AwsCustomResource.
*
* Note that at least onCreate, onUpdate or onDelete must be specified.
*/
export interface AwsCustomResourceProps {
/**
* Cloudformation Resource type.
*
* @default - Custom::AWS
*/
readonly resourceType?: string;
/**
* The AWS SDK call to make when the resource is created.
*
* @default - the call when the resource is updated
*/
readonly onCreate?: AwsSdkCall;
/**
* The AWS SDK call to make when the resource is updated
*
* @default - no call
*/
readonly onUpdate?: AwsSdkCall;
/**
* The AWS SDK call to make when the resource is deleted
*
* @default - no call
*/
readonly onDelete?: AwsSdkCall;
/**
* The policy that will be added to the execution role of the Lambda
* function implementing this custom resource provider.
*
* The custom resource also implements `iam.IGrantable`, making it possible
* to use the `grantXxx()` methods.
*
* As this custom resource uses a singleton Lambda function, it's important
* to note the that function's role will eventually accumulate the
* permissions/grants from all resources.
*
* Note that a policy must be specified if `role` is not provided, as
* by default a new role is created which requires policy changes to access
* resources.
*
* @default - no policy added
*
* @see Policy.fromStatements
* @see Policy.fromSdkCalls
*/
readonly policy?: AwsCustomResourcePolicy;
/**
* The execution role for the singleton Lambda function implementing this custom
* resource provider. This role will apply to all `AwsCustomResource`
* instances in the stack. The role must be assumable by the
* `lambda.amazonaws.com` service principal.
*
* @default - a new role is created
*/
readonly role?: iam.IRole;
/**
* The timeout for the singleton Lambda function implementing this custom resource.
*
* @default Duration.minutes(2)
*/
readonly timeout?: cdk.Duration;
/**
* The memory size for the singleton Lambda function implementing this custom resource.
*
* @default 512 mega in case if installLatestAwsSdk is false.
*/
readonly memorySize?: number;
/**
* The number of days log events of the singleton Lambda function implementing
* this custom resource are kept in CloudWatch Logs.
*
* This is a legacy API and we strongly recommend you migrate to `logGroup` if you can.
* `logGroup` allows you to create a fully customizable log group and instruct the Lambda function to send logs to it.
*
* @default logs.RetentionDays.INFINITE
*/
readonly logRetention?: logs.RetentionDays;
/**
* The Log Group used for logging of events emitted by the custom resource's lambda function.
*
* Providing a user-controlled log group was rolled out to commercial regions on 2023-11-16.
* If you are deploying to another type of region, please check regional availability first.
*
* @default - a default log group created by AWS Lambda
*/
readonly logGroup?: logs.ILogGroup;
/**
* Whether to install the latest AWS SDK v2.
*
* If not specified, this uses whatever JavaScript SDK version is the default in
* AWS Lambda at the time of execution.
*
* Otherwise, installs the latest version from 'npmjs.com'. The installation takes
* around 60 seconds and requires internet connectivity.
*
* The default can be controlled using the context key
* `@aws-cdk/customresources:installLatestAwsSdkDefault` is.
*
* @default - The value of `@aws-cdk/customresources:installLatestAwsSdkDefault`, otherwise `true`
*/
readonly installLatestAwsSdk?: boolean;
/**
* A name for the singleton Lambda function implementing this custom resource.
* The function name will remain the same after the first AwsCustomResource is created in a stack.
*
* @default - AWS CloudFormation generates a unique physical ID and uses that
* ID for the function's name. For more information, see Name Type.
*/
readonly functionName?: string;
/**
* The policy to apply when this resource is removed from the application.
*
* @default cdk.RemovalPolicy.Destroy
*/
readonly removalPolicy?: cdk.RemovalPolicy;
/**
* The vpc to provision the lambda function in.
*
* @default - the function is not provisioned inside a vpc.
*/
readonly vpc?: ec2.IVpc;
/**
* Which subnets from the VPC to place the lambda function in.
*
* Only used if 'vpc' is supplied. Note: internet access for Lambdas
* requires a NAT gateway, so picking Public subnets is not allowed.
*
* @default - the Vpc default strategy if not specified
*/
readonly vpcSubnets?: ec2.SubnetSelection;
}
/**
* Defines a custom resource that is materialized using specific AWS API calls. These calls are created using
* a singleton Lambda function.
*
* Use this to bridge any gap that might exist in the CloudFormation Coverage.
* You can specify exactly which calls are invoked for the 'CREATE', 'UPDATE' and 'DELETE' life cycle events.
*
*/
export class AwsCustomResource extends Construct implements iam.IGrantable {
/**
* The uuid of the custom resource provider singleton lambda function.
*/
public static readonly PROVIDER_FUNCTION_UUID = '679f53fa-c002-430c-b0da-5b7982bd2287';
private static breakIgnoreErrorsCircuit(sdkCalls: Array<AwsSdkCall | undefined>, caller: string) {
for (const call of sdkCalls) {
if (call?.ignoreErrorCodesMatching) {
throw new Error(`\`${caller}\`` + ' cannot be called along with `ignoreErrorCodesMatching`.');
}
}
}
public readonly grantPrincipal: iam.IPrincipal;
private readonly customResource: cdk.CustomResource;
private readonly props: AwsCustomResourceProps;
// 'props' cannot be optional, even though all its properties are optional.
// this is because at least one sdk call must be provided.
constructor(scope: Construct, id: string, props: AwsCustomResourceProps) {
super(scope, id);
if (!props.onCreate && !props.onUpdate && !props.onDelete) {
throw new Error('At least `onCreate`, `onUpdate` or `onDelete` must be specified.');
}
if (!props.role && !props.policy) {
throw new Error('At least one of `policy` or `role` (or both) must be specified.');
}
if (props.onCreate && !props.onCreate.physicalResourceId) {
throw new Error("'physicalResourceId' must be specified for 'onCreate' call.");
}
if (!props.onCreate && props.onUpdate && !props.onUpdate.physicalResourceId) {
throw new Error("'physicalResourceId' must be specified for 'onUpdate' call when 'onCreate' is omitted.");
}
for (const call of [props.onCreate, props.onUpdate, props.onDelete]) {
if (call?.physicalResourceId?.responsePath) {
AwsCustomResource.breakIgnoreErrorsCircuit([call], 'PhysicalResourceId.fromResponse');
}
}
if (includesPhysicalResourceIdRef(props.onCreate?.parameters)) {
throw new Error('`PhysicalResourceIdReference` must not be specified in `onCreate` parameters.');
}
this.props = props;
let memorySize = props.memorySize;
if (props.installLatestAwsSdk) {
memorySize ??= 512;
}
const provider = new AwsCustomResourceSingletonFunction(this, 'Provider', {
uuid: AwsCustomResource.PROVIDER_FUNCTION_UUID,
lambdaPurpose: 'AWS',
memorySize: memorySize,
timeout: props.timeout || cdk.Duration.minutes(2),
role: props.role,
// props.logRetention is deprecated, make sure we only set it if it is actually provided
// otherwise jsii will print warnings even for users that don't use this directly
...(props.logRetention ? { logRetention: props.logRetention } : {}),
logGroup: props.logGroup,
functionName: props.functionName,
vpc: props.vpc,
vpcSubnets: props.vpcSubnets,
});
this.grantPrincipal = provider.grantPrincipal;
const installLatestAwsSdk = (props.installLatestAwsSdk
?? this.node.tryGetContext(cxapi.AWS_CUSTOM_RESOURCE_LATEST_SDK_DEFAULT)
?? true);
if (installLatestAwsSdk && props.installLatestAwsSdk === undefined) {
// This is dangerous. Add a warning.
Annotations.of(this).addWarningV2('@aws-cdk/custom-resources:installLatestAwsSdkNotSpecified', [
'installLatestAwsSdk was not specified, and defaults to true. You probably do not want this.',
`Set the global context flag \'${cxapi.AWS_CUSTOM_RESOURCE_LATEST_SDK_DEFAULT}\' to false to switch this behavior off project-wide,`,
'or set the property explicitly to true if you know you need to call APIs that are not in Lambda\'s built-in SDK version.',
].join(' '));
}
const create = props.onCreate || props.onUpdate;
this.customResource = new cdk.CustomResource(this, 'Resource', {
resourceType: props.resourceType || 'Custom::AWS',
serviceToken: provider.functionArn,
pascalCaseProperties: true,
removalPolicy: props.removalPolicy,
properties: {
create: create && this.formatSdkCall(create),
update: props.onUpdate && this.formatSdkCall(props.onUpdate),
delete: props.onDelete && this.formatSdkCall(props.onDelete),
installLatestAwsSdk,
},
});
// Create the policy statements for the custom resource function role, or use the user-provided ones
if (props.policy) {
const statements = [];
if (props.policy.statements.length !== 0) {
// Use custom statements provided by the user
for (const statement of props.policy.statements) {
statements.push(statement);
}
} else {
// Derive statements from AWS SDK calls
for (const call of [props.onCreate, props.onUpdate, props.onDelete]) {
if (call && call.assumedRoleArn == null) {
const statement = new iam.PolicyStatement({
actions: [awsSdkToIamAction(call.service, call.action)],
resources: props.policy.resources,
});
statements.push(statement);
} else if (call && call.assumedRoleArn != null) {
const statement = new iam.PolicyStatement({
actions: ['sts:AssumeRole'],
resources: [call.assumedRoleArn],
});
statements.push(statement);
}
}
}
const policy = new iam.Policy(this, 'CustomResourcePolicy', {
statements: statements,
});
if (provider.role !== undefined) {
policy.attachToRole(provider.role);
}
// If the policy was deleted first, then the function might lose permissions to delete the custom resource
// This is here so that the policy doesn't get removed before onDelete is called
this.customResource.node.addDependency(policy);
}
}
/**
* Returns response data for the AWS SDK call.
*
* Example for S3 / listBucket : 'Buckets.0.Name'
*
* Use `Token.asXxx` to encode the returned `Reference` as a specific type or
* use the convenience `getDataString` for string attributes.
*
* Note that you cannot use this method if `ignoreErrorCodesMatching`
* is configured for any of the SDK calls. This is because in such a case,
* the response data might not exist, and will cause a CloudFormation deploy time error.
*
* @param dataPath the path to the data
*/
public getResponseFieldReference(dataPath: string) {
AwsCustomResource.breakIgnoreErrorsCircuit([this.props.onCreate, this.props.onUpdate], 'getData');
return this.customResource.getAtt(dataPath);
}
/**
* Returns response data for the AWS SDK call as string.
*
* Example for S3 / listBucket : 'Buckets.0.Name'
*
* Note that you cannot use this method if `ignoreErrorCodesMatching`
* is configured for any of the SDK calls. This is because in such a case,
* the response data might not exist, and will cause a CloudFormation deploy time error.
*
* @param dataPath the path to the data
*/
public getResponseField(dataPath: string): string {
AwsCustomResource.breakIgnoreErrorsCircuit([this.props.onCreate, this.props.onUpdate], 'getDataString');
return this.customResource.getAttString(dataPath);
}
private formatSdkCall(sdkCall: AwsSdkCall) {
const { logging, ...call } = sdkCall;
const renderedLogging = (logging ?? Logging.all())._render();
return this.encodeJson({
...call,
...renderedLogging,
});
}
private encodeJson(obj: any) {
return cdk.Lazy.uncachedString({ produce: () => cdk.Stack.of(this).toJsonString(obj) });
}
}
/**
* Returns true if `obj` includes a `PhysicalResourceIdReference` in one of the
* values.
* @param obj Any object.
*/
function includesPhysicalResourceIdRef(obj: any | undefined) {
if (obj === undefined) {
return false;
}
let foundRef = false;
// we use JSON.stringify as a way to traverse all values in the object.
JSON.stringify(obj, (_, v) => {
if (v === PHYSICAL_RESOURCE_ID_REFERENCE) {
foundRef = true;
}
return v;
});
return foundRef;
}