Skip to content

Commit

Permalink
Reverse dependency edge between aws-sns and aws-ec2 packages
Browse files Browse the repository at this point in the history
This is because otherwise the new dependency of aws-lambda => aws-ec2
would introduce the following cycle:

Cycle: @aws-cdk/aws-ec2 => @aws-cdk/aws-sns => @aws-cdk/aws-lambda =>
@aws-cdk/aws-ec2
  • Loading branch information
Rico Huijbers committed Aug 20, 2018
1 parent 6ba0614 commit cfefb7e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
21 changes: 18 additions & 3 deletions packages/@aws-cdk/aws-ec2/lib/auto-scaling-group.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import autoscaling = require('@aws-cdk/aws-autoscaling');
import iam = require('@aws-cdk/aws-iam');
import sns = require('@aws-cdk/aws-sns');
import cdk = require('@aws-cdk/cdk');
import { Connections, IConnectable } from './connections';
import { InstanceType } from './instance-types';
Expand Down Expand Up @@ -62,7 +61,7 @@ export interface AutoScalingGroupProps {
* SNS topic to send notifications about fleet changes
* @default No fleet change notifications will be sent.
*/
notificationsTopic?: sns.cloudformation.TopicResource;
notificationsTopic?: IAutoScalingNotificationTarget;

/**
* Whether the instances can initiate connections to anywhere by default
Expand Down Expand Up @@ -152,7 +151,7 @@ export class AutoScalingGroup extends cdk.Construct implements IClassicLoadBalan
if (props.notificationsTopic) {
asgProps.notificationConfigurations = [];
asgProps.notificationConfigurations.push({
topicArn: props.notificationsTopic.ref,
topicArn: props.notificationsTopic.asAutoScalingNotificationTarget(),
notificationTypes: [
"autoscaling:EC2_INSTANCE_LAUNCH",
"autoscaling:EC2_INSTANCE_LAUNCH_ERROR",
Expand Down Expand Up @@ -192,3 +191,19 @@ export class AutoScalingGroup extends cdk.Construct implements IClassicLoadBalan
this.role.addToPolicy(statement);
}
}

/**
* Interface for subscribing an SNS topic to AutoScalingGroup notifications
*
* (This interface exists to reverse the dependency between the aws-sns
* and aws-ec2 packages.)
*/
export interface IAutoScalingNotificationTarget {
/**
* ARN of the topic to send notifications to.
*/
asAutoScalingNotificationTarget(): cdk.Arn;

// NOTE: this cannot just be "topicArn: Arn" because of lack of return type
// covariance in jsii.
}
1 change: 0 additions & 1 deletion packages/@aws-cdk/aws-ec2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
"@aws-cdk/aws-autoscaling": "^0.8.2",
"@aws-cdk/aws-elasticloadbalancing": "^0.8.2",
"@aws-cdk/aws-iam": "^0.8.2",
"@aws-cdk/aws-sns": "^0.8.2",
"@aws-cdk/cdk": "^0.8.2",
"@aws-cdk/util": "^0.8.2"
},
Expand Down
9 changes: 8 additions & 1 deletion packages/@aws-cdk/aws-sns/lib/topic-ref.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import cloudwatch = require('@aws-cdk/aws-cloudwatch');
import ec2 = require('@aws-cdk/aws-ec2');
import events = require('@aws-cdk/aws-events');
import iam = require('@aws-cdk/aws-iam');
import lambda = require('@aws-cdk/aws-lambda');
Expand All @@ -17,7 +18,9 @@ export class TopicArn extends cdk.Arn { }
/**
* Either a new or imported Topic
*/
export abstract class TopicRef extends cdk.Construct implements events.IEventRuleTarget, cloudwatch.IAlarmAction, s3n.IBucketNotificationDestination {
export abstract class TopicRef extends cdk.Construct implements events.IEventRuleTarget, cloudwatch.IAlarmAction, s3n.IBucketNotificationDestination,
ec2.IAutoScalingNotificationTarget {

/**
* Import a Topic defined elsewhere
*/
Expand Down Expand Up @@ -306,6 +309,10 @@ export abstract class TopicRef extends cdk.Construct implements events.IEventRul
dependencies: [ this.policy! ] // make sure the topic policy resource is created before the notification config
};
}

public asAutoScalingNotificationTarget(): cdk.Arn {
return this.topicArn;
}
}

/**
Expand Down
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-sns/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
},
"dependencies": {
"@aws-cdk/aws-cloudwatch": "^0.8.2",
"@aws-cdk/aws-ec2": "^0.8.2",
"@aws-cdk/aws-events": "^0.8.2",
"@aws-cdk/aws-iam": "^0.8.2",
"@aws-cdk/aws-lambda": "^0.8.2",
Expand Down

0 comments on commit cfefb7e

Please sign in to comment.