Skip to content

Commit 89cfea9

Browse files
committed
Add option to create database from the snapshot
1 parent 409f1bf commit 89cfea9

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

src/components/database.ts

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ export type DatabaseArgs = {
5959
* DB parameters to this instance.
6060
*/
6161
parameterGroupName?: pulumi.Input<string>;
62+
/**
63+
* Specifies whether or not to create this database from a snapshot.
64+
* This correlates to the snapshot ID you'd find in the RDS console,
65+
* e.g: rds:production-2015-06-26-06-05.
66+
*/
67+
snapshotIdentifier?: pulumi.Input<string>;
6268
/**
6369
* A map of tags to assign to the resource.
6470
*/
@@ -84,6 +90,7 @@ export class Database extends pulumi.ComponentResource {
8490
dbSubnetGroup: aws.rds.SubnetGroup;
8591
dbSecurityGroup: aws.ec2.SecurityGroup;
8692
password: Password;
93+
encryptedSnapshotCopy?: aws.rds.SnapshotCopy;
8794
monitoringRole?: aws.iam.Role;
8895

8996
constructor(
@@ -96,8 +103,13 @@ export class Database extends pulumi.ComponentResource {
96103
this.name = name;
97104

98105
const argsWithDefaults = Object.assign({}, defaults, args);
99-
const { vpcId, isolatedSubnetIds, vpcCidrBlock, enableMonitoring } =
100-
argsWithDefaults;
106+
const {
107+
vpcId,
108+
isolatedSubnetIds,
109+
vpcCidrBlock,
110+
enableMonitoring,
111+
snapshotIdentifier,
112+
} = argsWithDefaults;
101113
this.dbSubnetGroup = this.createSubnetGroup({ isolatedSubnetIds });
102114
this.dbSecurityGroup = this.createSecurityGroup({ vpcId, vpcCidrBlock });
103115
this.kms = this.createEncryptionKey();
@@ -109,6 +121,10 @@ export class Database extends pulumi.ComponentResource {
109121
if (enableMonitoring) {
110122
this.monitoringRole = this.createMonitoringRole();
111123
}
124+
if (snapshotIdentifier) {
125+
this.encryptedSnapshotCopy =
126+
this.createEncryptedSnapshotCopy(snapshotIdentifier);
127+
}
112128
this.instance = this.createDatabaseInstance(args);
113129

114130
this.registerOutputs();
@@ -196,6 +212,20 @@ export class Database extends pulumi.ComponentResource {
196212
return monitoringRole;
197213
}
198214

215+
private createEncryptedSnapshotCopy(
216+
snapshotIdentifier: NonNullable<DatabaseArgs['snapshotIdentifier']>,
217+
) {
218+
const encryptedSnapshotCopy = new aws.rds.SnapshotCopy(
219+
`${this.name}-encrypted-snapshot-copy`,
220+
{
221+
sourceDbSnapshotIdentifier: snapshotIdentifier,
222+
targetDbSnapshotIdentifier: `${snapshotIdentifier}-${Date.now()}`,
223+
kmsKeyId: this.kms.arn,
224+
},
225+
);
226+
return encryptedSnapshotCopy;
227+
}
228+
199229
private createDatabaseInstance(args: DatabaseArgs) {
200230
const argsWithDefaults = Object.assign({}, defaults, args);
201231
const stack = pulumi.getStack();
@@ -238,6 +268,8 @@ export class Database extends pulumi.ComponentResource {
238268
caCertIdentifier: 'rds-ca-rsa2048-g1',
239269
parameterGroupName: argsWithDefaults.parameterGroupName,
240270
...monitoringOptions,
271+
snapshotIdentifier:
272+
this.encryptedSnapshotCopy?.targetDbSnapshotIdentifier,
241273
tags: { ...commonTags, ...argsWithDefaults.tags },
242274
},
243275
{ parent: this, dependsOn: [this.password] },

0 commit comments

Comments
 (0)