Skip to content

Commit

Permalink
generate cp name
Browse files Browse the repository at this point in the history
  • Loading branch information
msambol committed Feb 27, 2024
1 parent bc3767f commit 4ed3083
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
16 changes: 10 additions & 6 deletions packages/aws-cdk-lib/aws-ecs/lib/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import * as kms from '../../aws-kms';
import * as logs from '../../aws-logs';
import * as s3 from '../../aws-s3';
import * as cloudmap from '../../aws-servicediscovery';
import { Duration, IResource, Resource, Stack, Aspects, ArnFormat, IAspect, Token } from '../../core';
import { Duration, IResource, Resource, Stack, Aspects, ArnFormat, IAspect, Token, Names } from '../../core';

const CLUSTER_SYMBOL = Symbol.for('@aws-cdk/aws-ecs/lib/cluster.Cluster');

Expand Down Expand Up @@ -1282,6 +1282,7 @@ export class AsgCapacityProvider extends Construct {

constructor(scope: Construct, id: string, props: AsgCapacityProviderProps) {
super(scope, id);
let capacityProviderName = props.capacityProviderName;
this.autoScalingGroup = props.autoScalingGroup as autoscaling.AutoScalingGroup;
this.machineImageType = props.machineImageType ?? MachineImageType.AMAZON_LINUX_2;
this.canContainersAccessInstanceRole = props.canContainersAccessInstanceRole;
Expand All @@ -1301,13 +1302,16 @@ export class AsgCapacityProvider extends Construct {
}

const capacityProviderNameRegex = /^(?!aws|ecs|fargate).+/gm;
if (props.capacityProviderName) {
if (!(capacityProviderNameRegex.test(props.capacityProviderName))) {
throw new Error(`Invalid Capacity Provider Name: ${props.capacityProviderName}, If a name is specified, it cannot start with aws, ecs, or fargate.`);
if (capacityProviderName) {
if (!(capacityProviderNameRegex.test(capacityProviderName))) {
throw new Error(`Invalid Capacity Provider Name: ${capacityProviderName}, If a name is specified, it cannot start with aws, ecs, or fargate.`);
}
} else {
if (!(capacityProviderNameRegex.test(Stack.of(this).stackName))) {
throw new Error(`Invalid Capacity Provider Name: ${Stack.of(this).stackName}, No name was specified, the stack name cannot start with aws, ecs, or fargate.`);
// name cannot start with 'aws|ecs|fargate', so append 'cp-'
// 255 is the max length, subtract 3 because of 'cp-'
// if the regex condition isn't met, CFN will name the capacity provider
capacityProviderName = 'cp-' + Names.uniqueResourceName(this, { maxLength: 252, allowedSpecialCharacters: '-_' });
}
}

Expand All @@ -1318,7 +1322,7 @@ export class AsgCapacityProvider extends Construct {
}

const capacityProvider = new CfnCapacityProvider(this, id, {
name: props.capacityProviderName,
name: capacityProviderName,
autoScalingGroupProvider: {
autoScalingGroupArn: this.autoScalingGroup.autoScalingGroupName,
managedScaling: props.enableManagedScaling === false ? undefined : {
Expand Down
7 changes: 4 additions & 3 deletions packages/aws-cdk-lib/aws-ecs/test/cluster.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2960,13 +2960,14 @@ test('throws when ASG Capacity Provider with no capacityProviderName but stack n

expect(() => {
// WHEN Capacity Provider when stack name starts with ecs.
const capacityProviderAl2 = new ecs.AsgCapacityProvider(stack, 'provideral2-2', {
const capacityProvider = new ecs.AsgCapacityProvider(stack, 'provideral2-2', {
autoScalingGroup: autoScalingGroupAl2,
enableManagedTerminationProtection: false,
});

cluster.addAsgCapacityProvider(capacityProviderAl2);
}).toThrow(/Invalid Capacity Provider Name: ecscp, No name was specified, the stack name cannot start with aws, ecs, or fargate./);
cluster.addAsgCapacityProvider(capacityProvider);

}).not.toThrow();
});

test('throws when InstanceWarmupPeriod is less than 0', () => {
Expand Down

0 comments on commit 4ed3083

Please sign in to comment.