Skip to content

Commit 5f4baf5

Browse files
committed
Extend database component with monitoring and multiAz
1 parent 92e44f6 commit 5f4baf5

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,13 @@ type DatabaseServiceOptions = {
109109
dbName: pulumi.Input<string>;
110110
username: pulumi.Input<string>;
111111
password?: pulumi.Input<string>;
112+
multiAz?: pulumi.Input<boolean>;
112113
applyImmediately?: pulumi.Input<boolean>;
113114
skipFinalSnapshot?: pulumi.Input<boolean>;
114115
allocatedStorage?: pulumi.Input<number>;
115116
maxAllocatedStorage?: pulumi.Input<number>;
116117
instanceClass?: pulumi.Input<string>;
118+
enableMonitoring?: pulumi.Input<boolean>;
117119
tags?: pulumi.Input<{
118120
[key: string]: pulumi.Input<string>;
119121
}>;
@@ -359,11 +361,13 @@ type DatabaseArgs = {
359361
isolatedSubnetIds: pulumi.Input<pulumi.Input<string>[]>;
360362
vpcCidrBlock: pulumi.Input<string>;
361363
password?: pulumi.Input<string>;
364+
multiAz?: pulumi.Input<boolean>;
362365
applyImmediately?: pulumi.Input<boolean>;
363366
skipFinalSnapshot?: pulumi.Input<boolean>;
364367
allocatedStorage?: pulumi.Input<number>;
365368
maxAllocatedStorage?: pulumi.Input<number>;
366369
instanceClass?: pulumi.Input<string>;
370+
enableMonitoring?: pulumi.Input<boolean>;
367371
tags?: pulumi.Input<{
368372
[key: string]: pulumi.Input<string>;
369373
}>;

src/components/database.ts

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ export type DatabaseArgs = {
1818
* The IPv4 CIDR block for the VPC.
1919
*/
2020
vpcCidrBlock: pulumi.Input<string>;
21+
/**
22+
* Specifies if the RDS instance is multi-AZ. Defaults to false.
23+
*/
24+
multiAz?: pulumi.Input<boolean>;
2125
/**
2226
* Password for the master DB user. If not specified it will be autogenerated.
2327
* The value will be stored as a secret in AWS Secret Manager.
@@ -46,6 +50,10 @@ export type DatabaseArgs = {
4650
* The instance type of the RDS instance. Defaults to 'db.t4g.micro'.
4751
*/
4852
instanceClass?: pulumi.Input<string>;
53+
/**
54+
* Set this to true to enable database monitoring. Defaults to false.
55+
*/
56+
enableMonitoring?: pulumi.Input<boolean>;
4957
/**
5058
* A map of tags to assign to the resource.
5159
*/
@@ -55,11 +63,13 @@ export type DatabaseArgs = {
5563
};
5664

5765
const defaults = {
66+
multiAz: false,
5867
applyImmediately: false,
5968
skipFinalSnapshot: false,
6069
allocatedStorage: 20,
6170
maxAllocatedStorage: 100,
6271
instanceClass: 'db.t4g.micro',
72+
enableMonitoring: false,
6373
};
6474

6575
export class Database extends pulumi.ComponentResource {
@@ -69,6 +79,7 @@ export class Database extends pulumi.ComponentResource {
6979
dbSubnetGroup: aws.rds.SubnetGroup;
7080
dbSecurityGroup: aws.ec2.SecurityGroup;
7181
password: Password;
82+
monitoringRole?: aws.iam.Role;
7283

7384
constructor(
7485
name: string,
@@ -79,7 +90,9 @@ export class Database extends pulumi.ComponentResource {
7990

8091
this.name = name;
8192

82-
const { vpcId, isolatedSubnetIds, vpcCidrBlock } = args;
93+
const argsWithDefaults = Object.assign({}, defaults, args);
94+
const { vpcId, isolatedSubnetIds, vpcCidrBlock, enableMonitoring } =
95+
argsWithDefaults;
8396
this.dbSubnetGroup = this.createSubnetGroup({ isolatedSubnetIds });
8497
this.dbSecurityGroup = this.createSecurityGroup({ vpcId, vpcCidrBlock });
8598
this.kms = this.createEncryptionKey();
@@ -88,6 +101,9 @@ export class Database extends pulumi.ComponentResource {
88101
{ value: args.password },
89102
{ parent: this },
90103
);
104+
if (enableMonitoring) {
105+
this.monitoringRole = this.createMonitoringRole();
106+
}
91107
this.instance = this.createDatabaseInstance(args);
92108

93109
this.registerOutputs();
@@ -147,10 +163,48 @@ export class Database extends pulumi.ComponentResource {
147163
return kms;
148164
}
149165

166+
private createMonitoringRole() {
167+
const monitoringRole = new aws.iam.Role(`${this.name}-rds-monitoring`, {
168+
assumeRolePolicy: {
169+
Version: '2012-10-17',
170+
Statement: [
171+
{
172+
Action: 'sts:AssumeRole',
173+
Effect: 'Allow',
174+
Principal: {
175+
Service: 'monitoring.rds.amazonaws.com',
176+
},
177+
},
178+
],
179+
},
180+
});
181+
182+
new aws.iam.RolePolicyAttachment(
183+
`${this.name}-rds-monitoring-role-attachment`,
184+
{
185+
role: monitoringRole.name,
186+
policyArn:
187+
'arn:aws:iam::aws:policy/service-role/AmazonRDSEnhancedMonitoringRole',
188+
},
189+
);
190+
191+
return monitoringRole;
192+
}
193+
150194
private createDatabaseInstance(args: DatabaseArgs) {
151195
const argsWithDefaults = Object.assign({}, defaults, args);
152196
const stack = pulumi.getStack();
153197

198+
const monitoringOptions =
199+
argsWithDefaults.enableMonitoring && this.monitoringRole
200+
? {
201+
monitoringInterval: 60,
202+
monitoringRoleArn: this.monitoringRole.arn,
203+
performanceInsightsEnabled: true,
204+
performanceInsightsRetentionPeriod: 7,
205+
}
206+
: {};
207+
154208
const instance = new aws.rds.Instance(
155209
`${this.name}-rds`,
156210
{
@@ -167,6 +221,7 @@ export class Database extends pulumi.ComponentResource {
167221
vpcSecurityGroupIds: [this.dbSecurityGroup.id],
168222
storageEncrypted: true,
169223
kmsKeyId: this.kms.arn,
224+
multiAz: argsWithDefaults.multiAz,
170225
publiclyAccessible: false,
171226
skipFinalSnapshot: argsWithDefaults.skipFinalSnapshot,
172227
applyImmediately: argsWithDefaults.applyImmediately,
@@ -175,6 +230,7 @@ export class Database extends pulumi.ComponentResource {
175230
finalSnapshotIdentifier: `${this.name}-final-snapshot-${stack}`,
176231
backupWindow: '06:00-06:30',
177232
backupRetentionPeriod: 14,
233+
...monitoringOptions,
178234
tags: { ...commonTags, ...argsWithDefaults.tags },
179235
},
180236
{ parent: this, dependsOn: [this.password] },

0 commit comments

Comments
 (0)