From 6e23ae75184116953833ce93e87853fe9f933037 Mon Sep 17 00:00:00 2001 From: rbobrowicz Date: Thu, 11 Jun 2020 12:42:55 -0500 Subject: [PATCH] feat(autoscaling): add instanceMonitoring option (#8213) 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* --- packages/@aws-cdk/aws-autoscaling/README.md | 5 +++ .../aws-autoscaling/lib/auto-scaling-group.ts | 28 +++++++++++++ .../test/test.auto-scaling-group.ts | 39 +++++++++++++++++++ 3 files changed, 72 insertions(+) diff --git a/packages/@aws-cdk/aws-autoscaling/README.md b/packages/@aws-cdk/aws-autoscaling/README.md index 260d8d0e0b693..ce55c411671f7 100644 --- a/packages/@aws-cdk/aws-autoscaling/README.md +++ b/packages/@aws-cdk/aws-autoscaling/README.md @@ -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 diff --git a/packages/@aws-cdk/aws-autoscaling/lib/auto-scaling-group.ts b/packages/@aws-cdk/aws-autoscaling/lib/auto-scaling-group.ts index 52ca56748b0f3..31483d53d40a6 100644 --- a/packages/@aws-cdk/aws-autoscaling/lib/auto-scaling-group.ts +++ b/packages/@aws-cdk/aws-autoscaling/lib/auto-scaling-group.ts @@ -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 * @@ -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; } /** @@ -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, diff --git a/packages/@aws-cdk/aws-autoscaling/test/test.auto-scaling-group.ts b/packages/@aws-cdk/aws-autoscaling/test/test.auto-scaling-group.ts index c17c90b17f06e..faea5fe7cd8f9 100644 --- a/packages/@aws-cdk/aws-autoscaling/test/test.auto-scaling-group.ts +++ b/packages/@aws-cdk/aws-autoscaling/test/test.auto-scaling-group.ts @@ -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();