1
1
import { ICertificate } from '@aws-cdk/aws-certificatemanager' ;
2
+ import ec2 = require( '@aws-cdk/aws-ec2' ) ;
2
3
import ecs = require( '@aws-cdk/aws-ecs' ) ;
3
4
import elbv2 = require( '@aws-cdk/aws-elasticloadbalancingv2' ) ;
4
5
import { AddressRecordTarget , ARecord , IHostedZone } from '@aws-cdk/aws-route53' ;
@@ -16,8 +17,19 @@ export enum LoadBalancerType {
16
17
export interface LoadBalancedServiceBaseProps {
17
18
/**
18
19
* The cluster where your service will be deployed
20
+ * You can only specify either vpc or cluster. Alternatively, you can leave both blank
21
+ *
22
+ * @default - create a new cluster; if you do not specify a cluster nor a vpc, a new VPC will be created for you as well
23
+ */
24
+ readonly cluster ?: ecs . ICluster ;
25
+
26
+ /**
27
+ * VPC that the cluster instances or tasks are running in
28
+ * You can only specify either vpc or cluster. Alternatively, you can leave both blank
29
+ *
30
+ * @default - use vpc of cluster or create a new one
19
31
*/
20
- readonly cluster : ecs . ICluster ;
32
+ readonly vpc ?: ec2 . IVpc ;
21
33
22
34
/**
23
35
* The image to start.
@@ -115,11 +127,18 @@ export abstract class LoadBalancedServiceBase extends cdk.Construct {
115
127
116
128
public readonly targetGroup : elbv2 . ApplicationTargetGroup | elbv2 . NetworkTargetGroup ;
117
129
130
+ public readonly cluster : ecs . ICluster ;
131
+
118
132
public readonly logDriver ?: ecs . LogDriver ;
119
133
120
134
constructor ( scope : cdk . Construct , id : string , props : LoadBalancedServiceBaseProps ) {
121
135
super ( scope , id ) ;
122
136
137
+ if ( props . cluster && props . vpc ) {
138
+ throw new Error ( `You can only specify either vpc or cluster. Alternatively, you can leave both blank` ) ;
139
+ }
140
+ this . cluster = props . cluster || this . getDefaultCluster ( this , props . vpc ) ;
141
+
123
142
// Create log driver if logging is enabled
124
143
const enableLogging = props . enableLogging !== undefined ? props . enableLogging : true ;
125
144
this . logDriver = enableLogging ? this . createAWSLogDriver ( this . node . id ) : undefined ;
@@ -134,7 +153,7 @@ export abstract class LoadBalancedServiceBase extends cdk.Construct {
134
153
const internetFacing = props . publicLoadBalancer !== undefined ? props . publicLoadBalancer : true ;
135
154
136
155
const lbProps = {
137
- vpc : props . cluster . vpc ,
156
+ vpc : this . cluster . vpc ,
138
157
internetFacing
139
158
} ;
140
159
@@ -183,6 +202,13 @@ export abstract class LoadBalancedServiceBase extends cdk.Construct {
183
202
new cdk . CfnOutput ( this , 'LoadBalancerDNS' , { value : this . loadBalancer . loadBalancerDnsName } ) ;
184
203
}
185
204
205
+ protected getDefaultCluster ( scope : cdk . Construct , vpc ?: ec2 . IVpc ) : ecs . Cluster {
206
+ // magic string to avoid collision with user-defined constructs
207
+ const DEFAULT_CLUSTER_ID = `EcsDefaultClusterMnL3mNNYN${ vpc ? vpc . node . id : '' } ` ;
208
+ const stack = cdk . Stack . of ( scope ) ;
209
+ return stack . node . tryFindChild ( DEFAULT_CLUSTER_ID ) as ecs . Cluster || new ecs . Cluster ( stack , DEFAULT_CLUSTER_ID , { vpc } ) ;
210
+ }
211
+
186
212
protected addServiceAsTarget ( service : ecs . BaseService ) {
187
213
if ( this . loadBalancerType === LoadBalancerType . APPLICATION ) {
188
214
( this . targetGroup as elbv2 . ApplicationTargetGroup ) . addTarget ( service ) ;
0 commit comments