1+ AWSTemplateFormatVersion : ' 2010-09-09'
2+ Description : A stack that creates an RDS instance and places it into two subnets
3+ Parameters :
4+ NetworkStackName :
5+ Type : String
6+ Description : The name of the networking stack that this stack will build upon.
7+ DBInstanceClass :
8+ Type : String
9+ Description : The ID of the second subnet to place the RDS instance into.
10+ Default : ' db.t2.micro'
11+ DBName :
12+ Type : String
13+ Description : The name of the database that is created within the PostgreSQL instance.
14+ DBUsername :
15+ Type : String
16+ Description : The master user name for the PostgreSQL instance.
17+ Resources :
18+
19+ Secret :
20+ Type : " AWS::SecretsManager::Secret"
21+ Properties :
22+ Name : !Ref 'DBUsername'
23+ GenerateSecretString :
24+ # This will generate a JSON object with the keys "username" and password.
25+ SecretStringTemplate : !Join ['', ['{"username": "', !Ref 'DBUsername' ,'"}']]
26+ GenerateStringKey : " password"
27+ PasswordLength : 32
28+
29+ DBSubnetGroup :
30+ Type : AWS::RDS::DBSubnetGroup
31+ Properties :
32+ DBSubnetGroupDescription : Subnet group for the RDS instance
33+ DBSubnetGroupName : DBSubnetGroup
34+ SubnetIds :
35+ - Fn::ImportValue :
36+ !Join [':', [!Ref 'NetworkStackName', 'PrivateSubnetOne']]
37+ - Fn::ImportValue :
38+ !Join [':', [!Ref 'NetworkStackName', 'PrivateSubnetTwo']]
39+
40+ PostgresInstance :
41+ Type : AWS::RDS::DBInstance
42+ Properties :
43+ AllocatedStorage : 20
44+ AvailabilityZone :
45+ Fn::Select :
46+ - 0
47+ - Fn::GetAZs : {Ref: 'AWS::Region'}
48+ DBInstanceClass : !Ref 'DBInstanceClass'
49+ DBName : !Ref 'DBName'
50+ DBSubnetGroupName : !Ref 'DBSubnetGroup'
51+ Engine : postgres
52+ EngineVersion : 11.5
53+ MasterUsername : !Ref 'DBUsername'
54+ MasterUserPassword : !Join ['', ['{{resolve:secretsmanager:', !Ref Secret, ':SecretString:password}}' ]]
55+ PubliclyAccessible : false
56+ VPCSecurityGroups :
57+ - Fn::ImportValue :
58+ !Join [':', [!Ref 'NetworkStackName', 'DBSecurityGroupId']]
59+
60+ SecretRDSInstanceAttachment :
61+ Type : " AWS::SecretsManager::SecretTargetAttachment"
62+ Properties :
63+ SecretId : !Ref Secret
64+ TargetId : !Ref PostgresInstance
65+ TargetType : AWS::RDS::DBInstance
66+
67+ Outputs :
68+ EndpointAddress :
69+ Description : Address of the RDS endpoint.
70+ Value : !GetAtt 'PostgresInstance.Endpoint.Address'
71+ Export :
72+ Name : !Join [ ':', [ !Ref 'AWS::StackName', 'EndpointAddress' ] ]
73+ EndpointPort :
74+ Description : Port of the RDS endpoint.
75+ Value : !GetAtt 'PostgresInstance.Endpoint.Port'
76+ Export :
77+ Name : !Join [ ':', [ !Ref 'AWS::StackName', 'EndpointPort' ] ]
78+ Secret :
79+ Description : Reference to the secret containing the password to the database.
80+ Value : !Ref 'Secret'
81+ Export :
82+ Name : !Join [ ':', [ !Ref 'AWS::StackName', 'Secret' ] ]
0 commit comments