Skip to content

Commit

Permalink
fix(aws-ecs): check for invalid capacityProviderName (#17291)
Browse files Browse the repository at this point in the history
fix(aws-ecs): add precheck name of capacity provider when specify it.
chore(aws-ecs): update capacityProviderName Description


close #17321
----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
neilkuan authored Nov 20, 2021
1 parent 674fbe9 commit 6e2fde4
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
10 changes: 8 additions & 2 deletions packages/@aws-cdk/aws-ecs/lib/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -996,7 +996,9 @@ export interface ExecuteCommandLogConfiguration {
*/
export interface AsgCapacityProviderProps extends AddAutoScalingGroupCapacityOptions {
/**
* The name for the capacity provider.
* The name of the capacity provider. If a name is specified,
* it cannot start with `aws`, `ecs`, or `fargate`. If no name is specified,
* a default name in the CFNStackName-CFNResourceName-RandomString format is used.
*
* @default CloudFormation-generated name
*/
Expand Down Expand Up @@ -1085,7 +1087,11 @@ export class AsgCapacityProvider extends CoreConstruct {
if (this.enableManagedTerminationProtection) {
this.autoScalingGroup.protectNewInstancesFromScaleIn();
}

if (props.capacityProviderName) {
if (!(/^(?!aws|ecs|fargate).+/gm.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.`);
}
}
const capacityProvider = new CfnCapacityProvider(this, id, {
name: props.capacityProviderName,
autoScalingGroupProvider: {
Expand Down
37 changes: 37 additions & 0 deletions packages/@aws-cdk/aws-ecs/test/cluster.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2247,3 +2247,40 @@ test('can add ASG capacity via Capacity Provider by not specifying machineImageT
});

});

test('throws when ASG Capacity Provider with capacityProviderName starting with aws, ecs or faragte', () => {
// GIVEN
const app = new cdk.App();
const stack = new cdk.Stack(app, 'test');
const vpc = new ec2.Vpc(stack, 'Vpc');
const cluster = new ecs.Cluster(stack, 'EcsCluster');

const autoScalingGroupAl2 = new autoscaling.AutoScalingGroup(stack, 'asgal2', {
vpc,
instanceType: new ec2.InstanceType('bogus'),
machineImage: ecs.EcsOptimizedImage.amazonLinux2(),
});

// THEN
expect(() => {
// WHEN Capacity Provider define capacityProviderName start with aws.
const capacityProviderAl2 = new ecs.AsgCapacityProvider(stack, 'provideral2', {
autoScalingGroup: autoScalingGroupAl2,
enableManagedTerminationProtection: false,
capacityProviderName: 'awscp',
});

cluster.addAsgCapacityProvider(capacityProviderAl2);
}).toThrow(/Invalid Capacity Provider Name: awscp, If a name is specified, it cannot start with aws, ecs, or fargate./);

expect(() => {
// WHEN Capacity Provider define capacityProviderName start with ecs.
const capacityProviderAl2 = new ecs.AsgCapacityProvider(stack, 'provideral2-2', {
autoScalingGroup: autoScalingGroupAl2,
enableManagedTerminationProtection: false,
capacityProviderName: 'ecscp',
});

cluster.addAsgCapacityProvider(capacityProviderAl2);
}).toThrow(/Invalid Capacity Provider Name: ecscp, If a name is specified, it cannot start with aws, ecs, or fargate./);
});

0 comments on commit 6e2fde4

Please sign in to comment.