@@ -11,7 +11,7 @@ import * as kms from '../../aws-kms';
11
11
import * as logs from '../../aws-logs' ;
12
12
import * as s3 from '../../aws-s3' ;
13
13
import * as cloudmap from '../../aws-servicediscovery' ;
14
- import { Duration , IResource , Resource , Stack , Aspects , ArnFormat , IAspect , Token } from '../../core' ;
14
+ import { Duration , IResource , Resource , Stack , Aspects , ArnFormat , IAspect , Token , Names } from '../../core' ;
15
15
16
16
const CLUSTER_SYMBOL = Symbol . for ( '@aws-cdk/aws-ecs/lib/cluster.Cluster' ) ;
17
17
@@ -1168,6 +1168,8 @@ export interface AsgCapacityProviderProps extends AddAutoScalingGroupCapacityOpt
1168
1168
* The name of the capacity provider. If a name is specified,
1169
1169
* it cannot start with `aws`, `ecs`, or `fargate`. If no name is specified,
1170
1170
* a default name in the CFNStackName-CFNResourceName-RandomString format is used.
1171
+ * If the stack name starts with `aws`, `ecs`, or `fargate`, a unique resource name
1172
+ * is generated that starts with `cp-`.
1171
1173
*
1172
1174
* @default CloudFormation-generated name
1173
1175
*/
@@ -1288,6 +1290,7 @@ export class AsgCapacityProvider extends Construct {
1288
1290
1289
1291
constructor ( scope : Construct , id : string , props : AsgCapacityProviderProps ) {
1290
1292
super ( scope , id ) ;
1293
+ let capacityProviderName = props . capacityProviderName ;
1291
1294
this . autoScalingGroup = props . autoScalingGroup as autoscaling . AutoScalingGroup ;
1292
1295
this . machineImageType = props . machineImageType ?? MachineImageType . AMAZON_LINUX_2 ;
1293
1296
this . canContainersAccessInstanceRole = props . canContainersAccessInstanceRole ;
@@ -1306,9 +1309,17 @@ export class AsgCapacityProvider extends Construct {
1306
1309
this . autoScalingGroup . protectNewInstancesFromScaleIn ( ) ;
1307
1310
}
1308
1311
1309
- if ( props . capacityProviderName ) {
1310
- if ( ! ( / ^ (? ! a w s | e c s | f a r g a t e ) .+ / gm. test ( props . capacityProviderName ) ) ) {
1311
- throw new Error ( `Invalid Capacity Provider Name: ${ props . capacityProviderName } , If a name is specified, it cannot start with aws, ecs, or fargate.` ) ;
1312
+ const capacityProviderNameRegex = / ^ (? ! a w s | e c s | f a r g a t e ) .+ / gm;
1313
+ if ( capacityProviderName ) {
1314
+ if ( ! ( capacityProviderNameRegex . test ( capacityProviderName ) ) ) {
1315
+ throw new Error ( `Invalid Capacity Provider Name: ${ capacityProviderName } , If a name is specified, it cannot start with aws, ecs, or fargate.` ) ;
1316
+ }
1317
+ } else {
1318
+ if ( ! ( capacityProviderNameRegex . test ( Stack . of ( this ) . stackName ) ) ) {
1319
+ // name cannot start with 'aws|ecs|fargate', so append 'cp-'
1320
+ // 255 is the max length, subtract 3 because of 'cp-'
1321
+ // if the regex condition isn't met, CFN will name the capacity provider
1322
+ capacityProviderName = 'cp-' + Names . uniqueResourceName ( this , { maxLength : 252 , allowedSpecialCharacters : '-_' } ) ;
1312
1323
}
1313
1324
}
1314
1325
@@ -1319,7 +1330,7 @@ export class AsgCapacityProvider extends Construct {
1319
1330
}
1320
1331
1321
1332
const capacityProvider = new CfnCapacityProvider ( this , id , {
1322
- name : props . capacityProviderName ,
1333
+ name : capacityProviderName ,
1323
1334
autoScalingGroupProvider : {
1324
1335
autoScalingGroupArn : this . autoScalingGroup . autoScalingGroupName ,
1325
1336
managedScaling : props . enableManagedScaling === false ? undefined : {
0 commit comments