-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathinstance.ts
234 lines (204 loc) · 5.99 KB
/
instance.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import { Construct } from 'constructs';
import { IDatabaseCluster } from './cluster-ref';
import { CfnDBInstance } from './docdb.generated';
import { Endpoint } from './endpoint';
import * as ec2 from '../../aws-ec2';
import { ArnFormat } from '../../core';
import * as cdk from '../../core';
/**
* A database instance
*/
export interface IDatabaseInstance extends cdk.IResource {
/**
* The instance identifier.
*/
readonly instanceIdentifier: string;
/**
* The instance arn.
*/
readonly instanceArn: string;
/**
* The instance endpoint address.
*
* @attribute Endpoint
*/
readonly dbInstanceEndpointAddress: string;
/**
* The instance endpoint port.
*
* @attribute Port
*/
readonly dbInstanceEndpointPort: string;
/**
* The instance endpoint.
*/
readonly instanceEndpoint: Endpoint;
}
/**
* Properties that describe an existing instance
*/
export interface DatabaseInstanceAttributes {
/**
* The instance identifier.
*/
readonly instanceIdentifier: string;
/**
* The endpoint address.
*/
readonly instanceEndpointAddress: string;
/**
* The database port.
*/
readonly port: number;
}
/**
* A new or imported database instance.
*/
abstract class DatabaseInstanceBase extends cdk.Resource implements IDatabaseInstance {
/**
* Import an existing database instance.
*/
public static fromDatabaseInstanceAttributes(scope: Construct, id: string, attrs: DatabaseInstanceAttributes): IDatabaseInstance {
class Import extends DatabaseInstanceBase implements IDatabaseInstance {
public readonly defaultPort = ec2.Port.tcp(attrs.port);
public readonly instanceIdentifier = attrs.instanceIdentifier;
public readonly dbInstanceEndpointAddress = attrs.instanceEndpointAddress;
public readonly dbInstanceEndpointPort = attrs.port.toString();
public readonly instanceEndpoint = new Endpoint(attrs.instanceEndpointAddress, attrs.port);
}
return new Import(scope, id);
}
/**
* @inheritdoc
*/
public abstract readonly instanceIdentifier: string;
/**
* @inheritdoc
*/
public abstract readonly dbInstanceEndpointAddress: string;
/**
* @inheritdoc
*/
public abstract readonly dbInstanceEndpointPort: string;
/**
* @inheritdoc
*/
public abstract readonly instanceEndpoint: Endpoint;
/**
* The instance arn.
*/
public get instanceArn(): string {
return cdk.Stack.of(this).formatArn({
service: 'rds',
resource: 'db',
arnFormat: ArnFormat.COLON_RESOURCE_NAME,
resourceName: this.instanceIdentifier,
});
}
}
/**
* Construction properties for a DatabaseInstanceNew
*/
export interface DatabaseInstanceProps {
/**
* The DocumentDB database cluster the instance should launch into.
*/
readonly cluster: IDatabaseCluster;
/**
* The name of the compute and memory capacity classes.
*/
readonly instanceType: ec2.InstanceType;
/**
* The name of the Availability Zone where the DB instance will be located.
*
* @default - no preference
*/
readonly availabilityZone?: string;
/**
* A name for the DB instance. If you specify a name, AWS CloudFormation
* converts it to lowercase.
*
* @default - a CloudFormation generated name
*/
readonly dbInstanceName?: string;
/**
* Indicates that minor engine upgrades are applied automatically to the
* DB instance during the maintenance window.
*
* @default true
*/
readonly autoMinorVersionUpgrade?: boolean;
/**
* The weekly time range (in UTC) during which system maintenance can occur.
*
* Format: `ddd:hh24:mi-ddd:hh24:mi`
* Constraint: Minimum 30-minute window
*
* @default - a 30-minute window selected at random from an 8-hour block of
* time for each AWS Region, occurring on a random day of the week. To see
* the time blocks available, see https://docs.aws.amazon.com/documentdb/latest/developerguide/db-instance-maintain.html#maintenance-window
*/
readonly preferredMaintenanceWindow?: string;
/**
* The CloudFormation policy to apply when the instance is removed from the
* stack or replaced during an update.
*
* @default RemovalPolicy.Retain
*/
readonly removalPolicy?: cdk.RemovalPolicy;
/**
* A value that indicates whether to enable Performance Insights for the DB Instance.
*
* @default - false
*/
readonly enablePerformanceInsights?: boolean;
}
/**
* A database instance
*
* @resource AWS::DocDB::DBInstance
*/
export class DatabaseInstance extends DatabaseInstanceBase implements IDatabaseInstance {
/**
* The instance's database cluster
*/
public readonly cluster: IDatabaseCluster;
/**
* @inheritdoc
*/
public readonly instanceIdentifier: string;
/**
* @inheritdoc
*/
public readonly dbInstanceEndpointAddress: string;
/**
* @inheritdoc
*/
public readonly dbInstanceEndpointPort: string;
/**
* @inheritdoc
*/
public readonly instanceEndpoint: Endpoint;
constructor(scope: Construct, id: string, props: DatabaseInstanceProps) {
super(scope, id);
const instance = new CfnDBInstance(this, 'Resource', {
dbClusterIdentifier: props.cluster.clusterIdentifier,
dbInstanceClass: `db.${props.instanceType}`,
autoMinorVersionUpgrade: props.autoMinorVersionUpgrade ?? true,
availabilityZone: props.availabilityZone,
dbInstanceIdentifier: props.dbInstanceName,
preferredMaintenanceWindow: props.preferredMaintenanceWindow,
enablePerformanceInsights: props.enablePerformanceInsights,
});
this.cluster = props.cluster;
this.instanceIdentifier = instance.ref;
this.dbInstanceEndpointAddress = instance.attrEndpoint;
this.dbInstanceEndpointPort = instance.attrPort;
// create a number token that represents the port of the instance
const portAttribute = cdk.Token.asNumber(instance.attrPort);
this.instanceEndpoint = new Endpoint(instance.attrEndpoint, portAttribute);
instance.applyRemovalPolicy(props.removalPolicy, {
applyToUpdateReplacePolicy: true,
});
}
}