11import autoscaling = require( "@aws-cdk/aws-autoscaling" ) ;
2+ import cloudwatch = require( "@aws-cdk/aws-cloudwatch" ) ;
23import codedeploylb = require( "@aws-cdk/aws-codedeploy-api" ) ;
34import ec2 = require( "@aws-cdk/aws-ec2" ) ;
45import iam = require( '@aws-cdk/aws-iam' ) ;
@@ -167,6 +168,8 @@ export interface ServerDeploymentGroupProps {
167168 /**
168169 * The auto-scaling groups belonging to this Deployment Group.
169170 *
171+ * Auto-scaling groups can also be added after the Deployment Group is created using the {@link #addAutoScalingGroup} method.
172+ *
170173 * @default []
171174 */
172175 autoScalingGroups ?: autoscaling . AutoScalingGroup [ ] ;
@@ -189,7 +192,7 @@ export interface ServerDeploymentGroupProps {
189192 */
190193 loadBalancer ?: codedeploylb . ILoadBalancer ;
191194
192- /*
195+ /**
193196 * All EC2 instances matching the given set of tags when a deployment occurs will be added to this Deployment Group.
194197 *
195198 * @default no additional EC2 instances will be added to the Deployment Group
@@ -202,6 +205,25 @@ export interface ServerDeploymentGroupProps {
202205 * @default no additional on-premise instances will be added to the Deployment Group
203206 */
204207 onPremiseInstanceTags ?: InstanceTagSet ;
208+
209+ /**
210+ * The CloudWatch alarms associated with this Deployment Group.
211+ * CodeDeploy will stop (and optionally roll back)
212+ * a deployment if during it any of the alarms trigger.
213+ *
214+ * Alarms can also be added after the Deployment Group is created using the {@link #addAlarm} method.
215+ *
216+ * @default []
217+ * @see https://docs.aws.amazon.com/codedeploy/latest/userguide/monitoring-create-alarms.html
218+ */
219+ alarms ?: cloudwatch . Alarm [ ] ;
220+
221+ /**
222+ * Whether to continue a deployment even if fetching the alarm status from CloudWatch failed.
223+ *
224+ * @default false
225+ */
226+ ignorePollAlarmsFailure ?: boolean ;
205227}
206228
207229/**
@@ -216,6 +238,7 @@ export class ServerDeploymentGroup extends ServerDeploymentGroupRef {
216238 private readonly _autoScalingGroups : autoscaling . AutoScalingGroup [ ] ;
217239 private readonly installAgent : boolean ;
218240 private readonly codeDeployBucket : s3 . BucketRef ;
241+ private readonly alarms : cloudwatch . Alarm [ ] ;
219242
220243 constructor ( parent : cdk . Construct , id : string , props : ServerDeploymentGroupProps = { } ) {
221244 super ( parent , id , props . deploymentConfig ) ;
@@ -237,6 +260,8 @@ export class ServerDeploymentGroup extends ServerDeploymentGroupRef {
237260 this . addCodeDeployAgentInstallUserData ( asg ) ;
238261 }
239262
263+ this . alarms = props . alarms || [ ] ;
264+
240265 const resource = new cloudformation . DeploymentGroupResource ( this , 'Resource' , {
241266 applicationName : this . application . applicationName ,
242267 deploymentGroupName : props . deploymentGroupName ,
@@ -255,18 +280,33 @@ export class ServerDeploymentGroup extends ServerDeploymentGroupRef {
255280 } ,
256281 ec2TagSet : this . ec2TagSet ( props . ec2InstanceTags ) ,
257282 onPremisesTagSet : this . onPremiseTagSet ( props . onPremiseInstanceTags ) ,
283+ alarmConfiguration : new cdk . Token ( ( ) => this . renderAlarmConfiguration ( props . ignorePollAlarmsFailure ) ) ,
258284 } ) ;
259285
260286 this . deploymentGroupName = resource . deploymentGroupName ;
261287 this . deploymentGroupArn = deploymentGroupName2Arn ( this . application . applicationName ,
262288 this . deploymentGroupName ) ;
263289 }
264290
291+ /**
292+ * Adds an additional auto-scaling group to this Deployment Group.
293+ *
294+ * @param asg the auto-scaling group to add to this Deployment Group
295+ */
265296 public addAutoScalingGroup ( asg : autoscaling . AutoScalingGroup ) : void {
266297 this . _autoScalingGroups . push ( asg ) ;
267298 this . addCodeDeployAgentInstallUserData ( asg ) ;
268299 }
269300
301+ /**
302+ * Associates an additional alarm with this Deployment Group.
303+ *
304+ * @param alarm the alarm to associate with this Deployment Group
305+ */
306+ public addAlarm ( alarm : cloudwatch . Alarm ) : void {
307+ this . alarms . push ( alarm ) ;
308+ }
309+
270310 public get autoScalingGroups ( ) : autoscaling . AutoScalingGroup [ ] | undefined {
271311 return this . _autoScalingGroups . slice ( ) ;
272312 }
@@ -404,6 +444,17 @@ export class ServerDeploymentGroup extends ServerDeploymentGroupRef {
404444 }
405445 return tagsInGroup ;
406446 }
447+
448+ private renderAlarmConfiguration ( ignorePollAlarmFailure ?: boolean ) :
449+ cloudformation . DeploymentGroupResource . AlarmConfigurationProperty | undefined {
450+ return this . alarms . length === 0
451+ ? undefined
452+ : {
453+ alarms : this . alarms . map ( a => ( { name : a . alarmName } ) ) ,
454+ enabled : true ,
455+ ignorePollAlarmFailure,
456+ } ;
457+ }
407458}
408459
409460function deploymentGroupName2Arn ( applicationName : string , deploymentGroupName : string ) : string {
0 commit comments