-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
parameter.ts
792 lines (695 loc) · 26.4 KB
/
parameter.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
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
import { Construct } from 'constructs';
import * as ssm from './ssm.generated';
import { arnForParameterName, AUTOGEN_MARKER } from './util';
import * as iam from '../../aws-iam';
import * as kms from '../../aws-kms';
import * as cxschema from '../../cloud-assembly-schema';
import {
CfnDynamicReference, CfnDynamicReferenceService, CfnParameter,
ContextProvider, Fn, IResource, Resource, Stack, Token,
Tokenization,
} from '../../core';
/**
* An SSM Parameter reference.
*/
export interface IParameter extends IResource {
/**
* The ARN of the SSM Parameter resource.
* @attribute
*/
readonly parameterArn: string;
/**
* The name of the SSM Parameter resource.
* @attribute
*/
readonly parameterName: string;
/**
* The type of the SSM Parameter resource.
* @attribute
*/
readonly parameterType: string;
/**
* Grants read (DescribeParameter, GetParameters, GetParameter, GetParameterHistory) permissions on the SSM Parameter.
*
* @param grantee the role to be granted read-only access to the parameter.
*/
grantRead(grantee: iam.IGrantable): iam.Grant;
/**
* Grants write (PutParameter) permissions on the SSM Parameter.
*
* @param grantee the role to be granted write access to the parameter.
*/
grantWrite(grantee: iam.IGrantable): iam.Grant;
}
/**
* A String SSM Parameter.
*/
export interface IStringParameter extends IParameter {
/**
* The parameter value. Value must not nest another parameter. Do not use {{}} in the value.
*
* @attribute Value
*/
readonly stringValue: string;
}
/**
* A StringList SSM Parameter.
*/
export interface IStringListParameter extends IParameter {
/**
* The parameter value. Value must not nest another parameter. Do not use {{}} in the value. Values in the array
* cannot contain commas (``,``).
*
* @attribute Value
*/
readonly stringListValue: string[];
}
/**
* Properties needed to create a new SSM Parameter.
*/
export interface ParameterOptions {
/**
* A regular expression used to validate the parameter value. For example, for String types with values restricted to
* numbers, you can specify the following: ``^\d+$``
*
* @default no validation is performed
*/
readonly allowedPattern?: string;
/**
* Information about the parameter that you want to add to the system.
*
* @default none
*/
readonly description?: string;
/**
* The name of the parameter.
*
* @default - a name will be generated by CloudFormation
*/
readonly parameterName?: string;
/**
* Indicates if the parameter name is a simple name (i.e. does not include "/"
* separators).
*
* This is required only if `parameterName` is a token, which means we
* are unable to detect if the name is simple or "path-like" for the purpose
* of rendering SSM parameter ARNs.
*
* If `parameterName` is not specified, `simpleName` must be `true` (or
* undefined) since the name generated by AWS CloudFormation is always a
* simple name.
*
* @default - auto-detect based on `parameterName`
*/
readonly simpleName?: boolean;
/**
* The tier of the string parameter
*
* @default - undefined
*/
readonly tier?: ParameterTier;
}
/**
* Properties needed to create a String SSM parameter.
*/
export interface StringParameterProps extends ParameterOptions {
/**
* The value of the parameter. It may not reference another parameter and ``{{}}`` cannot be used in the value.
*/
readonly stringValue: string;
/**
* The type of the string parameter
*
* @default ParameterType.STRING
* @deprecated - type will always be 'String'
*/
readonly type?: ParameterType;
/**
* The data type of the parameter, such as `text` or `aws:ec2:image`.
*
* @default ParameterDataType.TEXT
*/
readonly dataType?: ParameterDataType;
}
/**
* Properties needed to create a StringList SSM Parameter
*/
export interface StringListParameterProps extends ParameterOptions {
/**
* The values of the parameter. It may not reference another parameter and ``{{}}`` cannot be used in the value.
*/
readonly stringListValue: string[];
}
/**
* Basic features shared across all types of SSM Parameters.
*/
abstract class ParameterBase extends Resource implements IParameter {
public abstract readonly parameterArn: string;
public abstract readonly parameterName: string;
public abstract readonly parameterType: string;
/**
* The encryption key that is used to encrypt this parameter.
*
* @default - default master key
*/
public readonly encryptionKey?: kms.IKey;
public grantRead(grantee: iam.IGrantable): iam.Grant {
if (this.encryptionKey) {
this.encryptionKey.grantDecrypt(grantee);
}
return iam.Grant.addToPrincipal({
grantee,
actions: [
'ssm:DescribeParameters',
'ssm:GetParameters',
'ssm:GetParameter',
'ssm:GetParameterHistory',
],
resourceArns: [this.parameterArn],
});
}
public grantWrite(grantee: iam.IGrantable): iam.Grant {
if (this.encryptionKey) {
this.encryptionKey.grantEncrypt(grantee);
}
return iam.Grant.addToPrincipal({
grantee,
actions: ['ssm:PutParameter'],
resourceArns: [this.parameterArn],
});
}
}
/**
* The type of CFN SSM Parameter
*
* Using specific types can be helpful in catching invalid values
* at the start of creating or updating a stack. CloudFormation validates
* the values against existing values in the account.
*
* @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/parameters-section-structure.html#aws-ssm-parameter-types
*/
export enum ParameterValueType {
/**
* String
*/
STRING = 'String',
/**
* An Availability Zone, such as us-west-2a.
*/
AWS_EC2_AVAILABILITYZONE_NAME = 'AWS::EC2::AvailabilityZone::Name',
/**
* An Amazon EC2 image ID, such as ami-0ff8a91507f77f867.
*/
AWS_EC2_IMAGE_ID = 'AWS::EC2::Image::Id',
/**
* An Amazon EC2 instance ID, such as i-1e731a32.
*/
AWS_EC2_INSTANCE_ID = 'AWS::EC2::Instance::Id',
/**
* An Amazon EC2 key pair name.
*/
AWS_EC2_KEYPAIR_KEYNAME = 'AWS::EC2::KeyPair::KeyName',
/**
* An EC2-Classic or default VPC security group name, such as my-sg-abc.
*/
AWS_EC2_SECURITYGROUP_GROUPNAME = 'AWS::EC2::SecurityGroup::GroupName',
/**
* A security group ID, such as sg-a123fd85.
*/
AWS_EC2_SECURITYGROUP_ID = 'AWS::EC2::SecurityGroup::Id',
/**
* A subnet ID, such as subnet-123a351e.
*/
AWS_EC2_SUBNET_ID = 'AWS::EC2::Subnet::Id',
/**
* An Amazon EBS volume ID, such as vol-3cdd3f56.
*/
AWS_EC2_VOLUME_ID = 'AWS::EC2::Volume::Id',
/**
* A VPC ID, such as vpc-a123baa3.
*/
AWS_EC2_VPC_ID = 'AWS::EC2::VPC::Id',
/**
* An Amazon Route 53 hosted zone ID, such as Z23YXV4OVPL04A.
*/
AWS_ROUTE53_HOSTEDZONE_ID = 'AWS::Route53::HostedZone::Id',
}
/**
* SSM parameter type
* @deprecated these types are no longer used
*/
export enum ParameterType {
/**
* String
*/
STRING = 'String',
/**
* Secure String
*
* Parameter Store uses an AWS Key Management Service (KMS) customer master key (CMK) to encrypt the parameter value.
* Parameters of type SecureString cannot be created directly from a CDK application.
*/
SECURE_STRING = 'SecureString',
/**
* String List
*/
STRING_LIST = 'StringList',
/**
* An Amazon EC2 image ID, such as ami-0ff8a91507f77f867
*/
AWS_EC2_IMAGE_ID = 'AWS::EC2::Image::Id',
}
/**
* SSM parameter data type
*/
export enum ParameterDataType {
/**
* Text
*/
TEXT = 'text',
/**
* Aws Ec2 Image
*/
AWS_EC2_IMAGE = 'aws:ec2:image',
}
/**
* SSM parameter tier
*/
export enum ParameterTier {
/**
* String
*/
ADVANCED = 'Advanced',
/**
* String
*/
INTELLIGENT_TIERING = 'Intelligent-Tiering',
/**
* String
*/
STANDARD = 'Standard',
}
/**
* Common attributes for string parameters.
*/
export interface CommonStringParameterAttributes {
/**
* The name of the parameter store value.
*
* This value can be a token or a concrete string. If it is a concrete string
* and includes "/" it must also be prefixed with a "/" (fully-qualified).
*/
readonly parameterName: string;
/**
* Indicates if the parameter name is a simple name (i.e. does not include "/"
* separators).
*
* This is required only if `parameterName` is a token, which means we
* are unable to detect if the name is simple or "path-like" for the purpose
* of rendering SSM parameter ARNs.
*
* If `parameterName` is not specified, `simpleName` must be `true` (or
* undefined) since the name generated by AWS CloudFormation is always a
* simple name.
*
* @default - auto-detect based on `parameterName`
*/
readonly simpleName?: boolean;
}
/**
* Attributes for parameters of various types of string.
*
* @see ParameterType
*/
export interface StringParameterAttributes extends CommonStringParameterAttributes {
/**
* The version number of the value you wish to retrieve.
*
* @default The latest version will be retrieved.
*/
readonly version?: number;
/**
* The type of the string parameter
*
* @default ParameterType.STRING
* @deprecated - use valueType instead
*/
readonly type?: ParameterType;
/**
* The type of the string parameter value
*
* Using specific types can be helpful in catching invalid values
* at the start of creating or updating a stack. CloudFormation validates
* the values against existing values in the account.
*
* Note - if you want to allow values from different AWS accounts, use
* ParameterValueType.STRING
*
* @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/parameters-section-structure.html#aws-ssm-parameter-types
*
* @default ParameterValueType.STRING
*/
readonly valueType?: ParameterValueType;
/**
* Use a dynamic reference as the representation in CloudFormation template level.
* By default, CDK tries to deduce an appropriate representation based on the parameter value (a CfnParameter or a dynamic reference). Use this flag to override the representation when it does not work.
*
* @default false
*/
readonly forceDynamicReference?: boolean;
}
/**
* Attributes for parameters of string list type.
*
* @see ParameterType
*/
export interface ListParameterAttributes extends CommonStringParameterAttributes {
/**
* The version number of the value you wish to retrieve.
*
* @default The latest version will be retrieved.
*/
readonly version?: number;
/**
* The type of the string list parameter value.
*
* Using specific types can be helpful in catching invalid values
* at the start of creating or updating a stack. CloudFormation validates
* the values against existing values in the account.
*
* Note - if you want to allow values from different AWS accounts, use
* ParameterValueType.STRING
*
* @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/parameters-section-structure.html#aws-ssm-parameter-types
*
* @default ParameterValueType.STRING
*/
readonly elementType?: ParameterValueType;
}
/**
* Attributes for secure string parameters.
*/
export interface SecureStringParameterAttributes extends CommonStringParameterAttributes {
/**
* The version number of the value you wish to retrieve.
*
* @default - AWS CloudFormation uses the latest version of the parameter
*/
readonly version?: number;
/**
* The encryption key that is used to encrypt this parameter
*
* @default - default master key
*/
readonly encryptionKey?: kms.IKey;
}
/**
* Creates a new String SSM Parameter.
* @resource AWS::SSM::Parameter
*
* @example
* const ssmParameter = new ssm.StringParameter(this, 'mySsmParameter', {
* parameterName: 'mySsmParameter',
* stringValue: 'mySsmParameterValue',
* });
*/
export class StringParameter extends ParameterBase implements IStringParameter {
/**
* Imports an external string parameter by name.
*/
public static fromStringParameterName(scope: Construct, id: string, stringParameterName: string): IStringParameter {
return this.fromStringParameterAttributes(scope, id, { parameterName: stringParameterName });
}
/**
* Imports an external string parameter with name and optional version.
*/
public static fromStringParameterAttributes(scope: Construct, id: string, attrs: StringParameterAttributes): IStringParameter {
if (!attrs.parameterName) {
throw new Error('parameterName cannot be an empty string');
}
if (attrs.type && ![ParameterType.STRING, ParameterType.AWS_EC2_IMAGE_ID].includes(attrs.type)) {
throw new Error(`fromStringParameterAttributes does not support ${attrs.type}. Please use ParameterType.STRING or ParameterType.AWS_EC2_IMAGE_ID`);
}
const type = attrs.type ?? attrs.valueType ?? ParameterValueType.STRING;
const forceDynamicReference = attrs.forceDynamicReference ?? false;
let stringValue: string;
if (attrs.version) {
stringValue = new CfnDynamicReference(CfnDynamicReferenceService.SSM, `${attrs.parameterName}:${Tokenization.stringifyNumber(attrs.version)}`).toString();
} else if (forceDynamicReference) {
stringValue = new CfnDynamicReference(CfnDynamicReferenceService.SSM, attrs.parameterName).toString();
} else if (Token.isUnresolved(attrs.parameterName) && Fn._isFnBase(Tokenization.reverseString(attrs.parameterName).firstToken)) {
// the default value of a CfnParameter can only contain strings, so we cannot use it when a parameter name contains tokens.
stringValue = new CfnDynamicReference(CfnDynamicReferenceService.SSM, attrs.parameterName).toString();
} else {
stringValue = new CfnParameter(scope, `${id}.Parameter`, { type: `AWS::SSM::Parameter::Value<${type}>`, default: attrs.parameterName }).valueAsString;
}
class Import extends ParameterBase {
public readonly parameterName = attrs.parameterName;
public readonly parameterArn = arnForParameterName(this, attrs.parameterName, { simpleName: attrs.simpleName });
public readonly parameterType = ParameterType.STRING; // this is the type returned by CFN @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ssm-parameter.html#aws-resource-ssm-parameter-return-values
public readonly stringValue = stringValue;
}
return new Import(scope, id);
}
/**
* Imports a secure string parameter from the SSM parameter store.
*/
public static fromSecureStringParameterAttributes(scope: Construct, id: string, attrs: SecureStringParameterAttributes): IStringParameter {
const version = attrs.version ? Tokenization.stringifyNumber(attrs.version) : '';
const stringValue = new CfnDynamicReference(
CfnDynamicReferenceService.SSM_SECURE,
version ? `${attrs.parameterName}:${version}` : attrs.parameterName,
).toString();
class Import extends ParameterBase {
public readonly parameterName = attrs.parameterName;
public readonly parameterArn = arnForParameterName(this, attrs.parameterName, { simpleName: attrs.simpleName });
public readonly parameterType = ParameterType.SECURE_STRING;
public readonly stringValue = stringValue;
public readonly encryptionKey = attrs.encryptionKey;
}
return new Import(scope, id);
}
/**
* Reads the value of an SSM parameter during synthesis through an
* environmental context provider.
*
* Requires that the stack this scope is defined in will have explicit
* account/region information. Otherwise, it will fail during synthesis.
*/
public static valueFromLookup(scope: Construct, parameterName: string): string {
const value = ContextProvider.getValue(scope, {
provider: cxschema.ContextProvider.SSM_PARAMETER_PROVIDER,
props: { parameterName },
dummyValue: `dummy-value-for-${parameterName}`,
}).value;
return value;
}
/**
* Returns a token that will resolve (during deployment) to the string value of an SSM string parameter.
* @param scope Some scope within a stack
* @param parameterName The name of the SSM parameter.
* @param version The parameter version (recommended in order to ensure that the value won't change during deployment)
*/
public static valueForStringParameter(scope: Construct, parameterName: string, version?: number): string {
return StringParameter.valueForTypedStringParameterV2(scope, parameterName, ParameterValueType.STRING, version);
}
/**
* Returns a token that will resolve (during deployment) to the string value of an SSM string parameter.
* @param scope Some scope within a stack
* @param parameterName The name of the SSM parameter.
* @param type The type of the SSM parameter.
* @param version The parameter version (recommended in order to ensure that the value won't change during deployment)
*/
public static valueForTypedStringParameterV2(scope: Construct, parameterName: string, type = ParameterValueType.STRING, version?: number): string {
const stack = Stack.of(scope);
const id = makeIdentityForImportedValue(parameterName);
const exists = stack.node.tryFindChild(id) as IStringParameter;
if (exists) { return exists.stringValue; }
return this.fromStringParameterAttributes(stack, id, { parameterName, version, valueType: type }).stringValue;
}
/**
* Returns a token that will resolve (during deployment) to the string value of an SSM string parameter.
* @param scope Some scope within a stack
* @param parameterName The name of the SSM parameter.
* @param type The type of the SSM parameter.
* @param version The parameter version (recommended in order to ensure that the value won't change during deployment)
* @deprecated - use valueForTypedStringParameterV2 instead
*/
public static valueForTypedStringParameter(scope: Construct, parameterName: string, type = ParameterType.STRING, version?: number): string {
if (type === ParameterType.STRING_LIST) {
throw new Error('valueForTypedStringParameter does not support STRING_LIST, '
+'use valueForTypedListParameter instead');
}
const stack = Stack.of(scope);
const id = makeIdentityForImportedValue(parameterName);
const exists = stack.node.tryFindChild(id) as IStringParameter;
if (exists) { return exists.stringValue; }
return this.fromStringParameterAttributes(stack, id, { parameterName, version, type }).stringValue;
}
/**
* Returns a token that will resolve (during deployment)
* @param scope Some scope within a stack
* @param parameterName The name of the SSM parameter
* @param version The parameter version (required for secure strings)
* @deprecated Use `SecretValue.ssmSecure()` instead, it will correctly type the imported value as a `SecretValue` and allow importing without version. `SecretValue` lives in the core `aws-cdk-lib` module.
*/
public static valueForSecureStringParameter(scope: Construct, parameterName: string, version: number): string {
const stack = Stack.of(scope);
const id = makeIdentityForImportedValue(parameterName);
const exists = stack.node.tryFindChild(id) as IStringParameter;
if (exists) { return exists.stringValue; }
return this.fromSecureStringParameterAttributes(stack, id, { parameterName, version }).stringValue;
}
public readonly parameterArn: string;
public readonly parameterName: string;
public readonly parameterType: string;
public readonly stringValue: string;
constructor(scope: Construct, id: string, props: StringParameterProps) {
super(scope, id, {
physicalName: props.parameterName,
});
if (props.allowedPattern) {
_assertValidValue(props.stringValue, props.allowedPattern);
}
validateParameterName(this.physicalName);
if (props.description && props.description?.length > 1024) {
throw new Error('Description cannot be longer than 1024 characters.');
}
if (props.type && props.type === ParameterType.AWS_EC2_IMAGE_ID) {
throw new Error('The type must either be ParameterType.STRING or ParameterType.STRING_LIST. Did you mean to set dataType: ParameterDataType.AWS_EC2_IMAGE instead?');
}
const resource = new ssm.CfnParameter(this, 'Resource', {
allowedPattern: props.allowedPattern,
description: props.description,
name: this.physicalName,
tier: props.tier,
type: props.type || ParameterType.STRING,
dataType: props.dataType,
value: props.stringValue,
});
this.parameterName = this.getResourceNameAttribute(resource.ref);
this.parameterArn = arnForParameterName(this, this.parameterName, {
physicalName: props.parameterName || AUTOGEN_MARKER,
simpleName: props.simpleName,
});
this.parameterType = resource.attrType;
this.stringValue = resource.attrValue;
}
}
/**
* Creates a new StringList SSM Parameter.
* @resource AWS::SSM::Parameter
*/
export class StringListParameter extends ParameterBase implements IStringListParameter {
/**
* Imports an external parameter of type string list.
* Returns a token and should not be parsed.
*/
public static fromStringListParameterName(scope: Construct, id: string, stringListParameterName: string): IStringListParameter {
class Import extends ParameterBase {
public readonly parameterName = stringListParameterName;
public readonly parameterArn = arnForParameterName(this, this.parameterName);
public readonly parameterType = ParameterType.STRING_LIST;
public readonly stringListValue = Fn.split(',', new CfnDynamicReference(CfnDynamicReferenceService.SSM, stringListParameterName).toString());
}
return new Import(scope, id);
}
/**
* Imports an external string list parameter with name and optional version.
*/
public static fromListParameterAttributes(scope: Construct, id: string, attrs: ListParameterAttributes): IStringListParameter {
if (!attrs.parameterName) {
throw new Error('parameterName cannot be an empty string');
}
const type = attrs.elementType ?? ParameterValueType.STRING;
const valueType = `List<${type}>`;
const stringValue = attrs.version
? new CfnDynamicReference(CfnDynamicReferenceService.SSM, `${attrs.parameterName}:${Tokenization.stringifyNumber(attrs.version)}`).toStringList()
: new CfnParameter(scope, `${id}.Parameter`, { type: `AWS::SSM::Parameter::Value<${valueType}>`, default: attrs.parameterName }).valueAsList;
class Import extends ParameterBase {
public readonly parameterName = attrs.parameterName;
public readonly parameterArn = arnForParameterName(this, attrs.parameterName, { simpleName: attrs.simpleName });
public readonly parameterType = valueType; // it doesn't really matter what this is since a CfnParameter can only be `String | StringList`
public readonly stringListValue = stringValue;
}
return new Import(scope, id);
}
/**
* Returns a token that will resolve (during deployment) to the list value of an SSM StringList parameter.
* @param scope Some scope within a stack
* @param parameterName The name of the SSM parameter.
* @param type the type of the SSM list parameter
* @param version The parameter version (recommended in order to ensure that the value won't change during deployment)
*/
public static valueForTypedListParameter(scope: Construct, parameterName: string, type = ParameterValueType.STRING, version?: number): string[] {
const stack = Stack.of(scope);
const id = makeIdentityForImportedValue(parameterName);
const exists = stack.node.tryFindChild(id) as IStringListParameter;
if (exists) { return exists.stringListValue; }
return this.fromListParameterAttributes(stack, id, { parameterName, elementType: type, version }).stringListValue;
}
public readonly parameterArn: string;
public readonly parameterName: string;
public readonly parameterType: string;
public readonly stringListValue: string[];
constructor(scope: Construct, id: string, props: StringListParameterProps) {
super(scope, id, {
physicalName: props.parameterName,
});
if (props.stringListValue.find(str => !Token.isUnresolved(str) && str.indexOf(',') !== -1)) {
throw new Error('Values of a StringList SSM Parameter cannot contain the \',\' character. Use a string parameter instead.');
}
if (props.allowedPattern && !Token.isUnresolved(props.stringListValue)) {
props.stringListValue.forEach(str => _assertValidValue(str, props.allowedPattern!));
}
validateParameterName(this.physicalName);
if (props.description && props.description?.length > 1024) {
throw new Error('Description cannot be longer than 1024 characters.');
}
const resource = new ssm.CfnParameter(this, 'Resource', {
allowedPattern: props.allowedPattern,
description: props.description,
name: this.physicalName,
tier: props.tier,
type: ParameterType.STRING_LIST,
value: Fn.join(',', props.stringListValue),
});
this.parameterName = this.getResourceNameAttribute(resource.ref);
this.parameterArn = arnForParameterName(this, this.parameterName, {
physicalName: props.parameterName || AUTOGEN_MARKER,
simpleName: props.simpleName,
});
this.parameterType = resource.attrType;
this.stringListValue = Fn.split(',', resource.attrValue);
}
}
/**
* Validates whether a supplied value conforms to the allowedPattern, granted neither is an unresolved token.
*
* @param value the value to be validated.
* @param allowedPattern the regular expression to use for validation.
*
* @throws if the ``value`` does not conform to the ``allowedPattern`` and neither is an unresolved token (per
* ``cdk.unresolved``).
*/
function _assertValidValue(value: string, allowedPattern: string): void {
if (Token.isUnresolved(value) || Token.isUnresolved(allowedPattern)) {
// Unable to perform validations against unresolved tokens
return;
}
if (!new RegExp(allowedPattern).test(value)) {
throw new Error(`The supplied value (${value}) does not match the specified allowedPattern (${allowedPattern})`);
}
}
function makeIdentityForImportedValue(parameterName: string) {
return `SsmParameterValue:${parameterName}:C96584B6-F00A-464E-AD19-53AFF4B05118`;
}
function validateParameterName(parameterName: string) {
if (Token.isUnresolved(parameterName)) { return; }
if (parameterName.length > 2048) {
throw new Error('name cannot be longer than 2048 characters.');
}
if (!parameterName.match(/^[\/\w.-]+$/)) {
throw new Error(`name must only contain letters, numbers, and the following 4 symbols .-_/; got ${parameterName}`);
}
}