Skip to content

Commit

Permalink
feat(autoscaling): add instanceMonitoring option (#8213)
Browse files Browse the repository at this point in the history
Gives users the option to choose between detailed and basic monitoring.
Defaults to detailed when not specified, maintaining current behavior.

Fixes #8212 
----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
rbobrowicz committed Jun 11, 2020
1 parent 6df546f commit 6e23ae7
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/@aws-cdk/aws-autoscaling/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,11 @@ To enable the max instance lifetime support, specify `maxInstanceLifetime` prope
for the `AutoscalingGroup` resource. The value must be between 7 and 365 days(inclusive).
To clear a previously set value, just leave this property undefinied.

### Instance Monitoring

To disable detailed instance monitoring, specify `instanceMonitoring` property
for the `AutoscalingGroup` resource as `Monitoring.BASIC`. Otherwise detailed monitoring
will be enabled.


### Future work
Expand Down
28 changes: 28 additions & 0 deletions packages/@aws-cdk/aws-autoscaling/lib/auto-scaling-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,21 @@ import { BlockDevice, BlockDeviceVolume, EbsDeviceVolumeType } from './volume';
*/
const NAME_TAG: string = 'Name';

/**
* The monitoring mode for instances launched in an autoscaling group
*/
export enum Monitoring {
/**
* Generates metrics every 5 minutes
*/
BASIC,

/**
* Generates metrics every minute
*/
DETAILED,
}

/**
* Basic properties of an AutoScalingGroup, except the exact machines to run and where they should run
*
Expand Down Expand Up @@ -207,6 +222,18 @@ export interface CommonAutoScalingGroupProps {
* @default none
*/
readonly maxInstanceLifetime?: Duration;

/**
* Controls whether instances in this group are launched with detailed or basic monitoring.
*
* When detailed monitoring is enabled, Amazon CloudWatch generates metrics every minute and your account
* is charged a fee. When you disable detailed monitoring, CloudWatch generates metrics every 5 minutes.
*
* @see https://docs.aws.amazon.com/autoscaling/latest/userguide/as-instance-monitoring.html#enable-as-instance-metrics
*
* @default - Monitoring.DETAILED
*/
readonly instanceMonitoring?: Monitoring;
}

/**
Expand Down Expand Up @@ -477,6 +504,7 @@ export class AutoScalingGroup extends AutoScalingGroupBase implements
imageId: imageConfig.imageId,
keyName: props.keyName,
instanceType: props.instanceType.toString(),
instanceMonitoring: (props.instanceMonitoring !== undefined ? (props.instanceMonitoring === Monitoring.DETAILED) : undefined),
securityGroups: securityGroupsToken,
iamInstanceProfile: iamProfile.ref,
userData: userDataToken,
Expand Down
39 changes: 39 additions & 0 deletions packages/@aws-cdk/aws-autoscaling/test/test.auto-scaling-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,45 @@ export = {
test.done();
},

'can configure instance monitoring'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const vpc = mockVpc(stack);

// WHEN
new autoscaling.AutoScalingGroup(stack, 'MyStack', {
instanceType: ec2.InstanceType.of(ec2.InstanceClass.M4, ec2.InstanceSize.MICRO),
machineImage: new ec2.AmazonLinuxImage(),
vpc,
instanceMonitoring: autoscaling.Monitoring.BASIC,
});

// THEN
expect(stack).to(haveResource('AWS::AutoScaling::LaunchConfiguration', {
InstanceMonitoring: false,
}));
test.done();
},

'instance monitoring defaults to absent'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const vpc = mockVpc(stack);

// WHEN
new autoscaling.AutoScalingGroup(stack, 'MyStack', {
instanceType: ec2.InstanceType.of(ec2.InstanceClass.M4, ec2.InstanceSize.MICRO),
machineImage: new ec2.AmazonLinuxImage(),
vpc,
});

// THEN
expect(stack).to(haveResource('AWS::AutoScaling::LaunchConfiguration', {
InstanceMonitoring: ABSENT,
}));
test.done();
},

'throws if ephemeral volumeIndex < 0'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
Expand Down

0 comments on commit 6e23ae7

Please sign in to comment.