11import * as aws from '@pulumi/aws' ;
22import * as pulumi from '@pulumi/pulumi' ;
3- import * as random from '@pulumi/random ' ;
3+ import { Password } from './password ' ;
44import { commonTags } from '../constants' ;
55
66export type DatabaseArgs = {
@@ -68,7 +68,7 @@ export class Database extends pulumi.ComponentResource {
6868 kms : aws . kms . Key ;
6969 dbSubnetGroup : aws . rds . SubnetGroup ;
7070 dbSecurityGroup : aws . ec2 . SecurityGroup ;
71- passwordSecret : aws . secretsmanager . Secret ;
71+ password : Password ;
7272
7373 constructor (
7474 name : string ,
@@ -83,9 +83,12 @@ export class Database extends pulumi.ComponentResource {
8383 this . dbSubnetGroup = this . createSubnetGroup ( { isolatedSubnetIds } ) ;
8484 this . dbSecurityGroup = this . createSecurityGroup ( { vpcId, vpcCidrBlock } ) ;
8585 this . kms = this . createEncryptionKey ( ) ;
86- const { instance, passwordSecret } = this . createDatabaseInstance ( args ) ;
87- this . instance = instance ;
88- this . passwordSecret = passwordSecret ;
86+ this . password = new Password (
87+ `${ this . name } -database-password` ,
88+ { value : args . password } ,
89+ { parent : this } ,
90+ ) ;
91+ this . instance = this . createDatabaseInstance ( args ) ;
8992
9093 this . registerOutputs ( ) ;
9194 }
@@ -144,43 +147,9 @@ export class Database extends pulumi.ComponentResource {
144147 return kms ;
145148 }
146149
147- private createPasswordSecret ( { password } : Pick < DatabaseArgs , 'password' > ) {
148- const project = pulumi . getProject ( ) ;
149- const stack = pulumi . getStack ( ) ;
150-
151- const passwordSecret = new aws . secretsmanager . Secret (
152- `${ this . name } -password-secret` ,
153- {
154- namePrefix : `${ stack } /${ project } /DatabasePassword-` ,
155- tags : commonTags ,
156- } ,
157- { parent : this } ,
158- ) ;
159-
160- const passwordSecretValue = new aws . secretsmanager . SecretVersion (
161- `${ this . name } -password-secret-value` ,
162- {
163- secretId : passwordSecret . id ,
164- secretString : password ,
165- } ,
166- { parent : this , dependsOn : [ passwordSecret ] } ,
167- ) ;
168-
169- return passwordSecret ;
170- }
171-
172150 private createDatabaseInstance ( args : DatabaseArgs ) {
173151 const argsWithDefaults = Object . assign ( { } , defaults , args ) ;
174152 const stack = pulumi . getStack ( ) ;
175- const password =
176- argsWithDefaults . password ||
177- new random . RandomPassword ( `${ this . name } -db-password` , {
178- length : 16 ,
179- overrideSpecial : '_%$' ,
180- special : true ,
181- } ) . result ;
182-
183- const passwordSecret = this . createPasswordSecret ( { password } ) ;
184153
185154 const instance = new aws . rds . Instance (
186155 `${ this . name } -rds` ,
@@ -193,7 +162,7 @@ export class Database extends pulumi.ComponentResource {
193162 instanceClass : argsWithDefaults . instanceClass ,
194163 dbName : argsWithDefaults . dbName ,
195164 username : argsWithDefaults . username ,
196- password,
165+ password : this . password . value ,
197166 dbSubnetGroupName : this . dbSubnetGroup . name ,
198167 vpcSecurityGroupIds : [ this . dbSecurityGroup . id ] ,
199168 storageEncrypted : true ,
@@ -208,8 +177,8 @@ export class Database extends pulumi.ComponentResource {
208177 backupRetentionPeriod : 14 ,
209178 tags : { ...commonTags , ...argsWithDefaults . tags } ,
210179 } ,
211- { parent : this } ,
180+ { parent : this , dependsOn : [ this . password ] } ,
212181 ) ;
213- return { instance, passwordSecret } ;
182+ return instance ;
214183 }
215184}
0 commit comments