1+ using Pulumi ;
2+ using VirtualFinland . UsersAPI . Deployment . Common . Models ;
3+ using Pulumi . Aws . Iam ;
4+ using System . Text . Json ;
5+ using System . Collections . Generic ;
6+ using Pulumi . Aws . Rds ;
7+ using Pulumi . Aws . Rds . Inputs ;
8+ using Pulumi . Random ;
9+
10+ namespace VirtualFinland . UsersAPI . Deployment . Features ;
11+
12+ public class RDSProxy
13+ {
14+ public RDSProxy ( Config config , StackSetup stackSetup , PostgresDatabase database )
15+ {
16+ // RDS proxy access secret
17+ var username = new RandomPassword ( stackSetup . CreateResourceName ( "rdsproxy-username" ) , new ( )
18+ {
19+ Length = 16 ,
20+ Special = false ,
21+ OverrideSpecial = "_%@" ,
22+ } ) ;
23+ var password = new RandomPassword ( stackSetup . CreateResourceName ( "rdsproxy-password" ) , new ( )
24+ {
25+ Length = 16 ,
26+ Special = false ,
27+ OverrideSpecial = "_%@" ,
28+ } ) ;
29+ var rdsProxySecretString = Output . Format ( $ "{{\" username\" :\" { username . Result } \" ,\" password\" :\" { password . Result } \" }}") ;
30+ var rdsProxySecret = new SecretsManager ( config , stackSetup , "rdsProxySecret" , rdsProxySecretString ) ;
31+
32+ // Create role for rds proxy
33+ var rdsProxyRole = new Role ( stackSetup . CreateResourceName ( "database-proxy-role" ) , new RoleArgs ( )
34+ {
35+ AssumeRolePolicy = JsonSerializer . Serialize ( new Dictionary < string , object ? >
36+ {
37+ { "Version" , "2012-10-17" } ,
38+ {
39+ "Statement" , new [ ]
40+ {
41+ new Dictionary < string , object ? >
42+ {
43+ { "Action" , "sts:AssumeRole" } ,
44+ { "Effect" , "Allow" } ,
45+ { "Sid" , "" } ,
46+ {
47+ "Principal" , new Dictionary < string , object ? >
48+ {
49+ { "Service" , "rds.amazonaws.com" }
50+ }
51+ }
52+ }
53+ }
54+ }
55+ } ) ,
56+ Tags = stackSetup . Tags
57+ } ) ;
58+
59+ new RolePolicyAttachment ( $ "{ stackSetup . ProjectName } -RdsProxy-SecretManager-{ stackSetup . Environment } ", new RolePolicyAttachmentArgs
60+ {
61+ Role = rdsProxyRole . Name ,
62+ PolicyArn = rdsProxySecret . Arn
63+ } ) ;
64+
65+ // AWS RDS Proxy
66+ var rdsProxy = new Proxy ( stackSetup . CreateResourceName ( "database-proxy" ) , new ( )
67+ {
68+ DebugLogging = false ,
69+ EngineFamily = "POSTGRESQL" ,
70+ RequireTls = true ,
71+ RoleArn = rdsProxyRole . Arn ,
72+ VpcSubnetIds = stackSetup . VpcSetup . PrivateSubnetIds ,
73+ VpcSecurityGroupIds = new [ ] { stackSetup . VpcSetup . SecurityGroupId } ,
74+ Auths = new [ ] {
75+ new ProxyAuthArgs
76+ {
77+ AuthScheme = "SECRETS" ,
78+ Description = "Secrets authentication" ,
79+ SecretArn = rdsProxySecret . Arn ,
80+ IamAuth = "DISABLED"
81+ }
82+ } ,
83+ Tags = stackSetup . Tags ,
84+ } ) ;
85+
86+ // RDS Proxy Target
87+ new ProxyTarget ( stackSetup . CreateResourceName ( "database-proxy-target" ) , new ProxyTargetArgs ( )
88+ {
89+ DbProxyName = rdsProxy . Name ,
90+ DbInstanceIdentifier = database . DBIdentifier ,
91+ } ) ;
92+
93+ // Set outputs
94+ ProxyEndpoint = rdsProxy . Endpoint ;
95+ ProxyIdentifier = rdsProxy . Id ;
96+
97+ var DbName = config . Require ( "dbName" ) ;
98+ DatabaseConnectionString = Output . Format ( $ "Host={ rdsProxy . Endpoint } ;Database={ DbName } ;Username={ username . Result } ;Password={ password . Result } ") ;
99+ }
100+
101+ [ Output ]
102+ public Output < string > ProxyEndpoint { get ; set ; }
103+ public Output < string > ProxyIdentifier { get ; set ; }
104+ public Output < string > DatabaseConnectionString { get ; set ; }
105+ }
0 commit comments