-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatabase.yaml
115 lines (102 loc) · 3.56 KB
/
database.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#Resources reqd
# DB Cluster, DB SG, DB SG rules(ingress, egress)(only ingress here),
AWSTemplateFormatVersion: "2010-09-09"
Description: Database template
Parameters:
Environment:
Type: String
Default: test
AllowedValues: [ "test", "prod" ]
Conditions:
ProdEnv: !Equals [!Ref Environment, "prod"]
TestEnv: !Equals [!Ref Environment, "test"]
Resources:
DatabaseSecret:
Type: "AWS"::SecretsManager::Secret"
Properties:
Description: "Generated Secret"
GenerateSecretString:
SecretStringTemplate: '{"username": "sai"}'
GenerateStringKey: "password"
PasswordLength: "16"
ExcludeCharacters: '"@/\'
Tags:
- Key: Env
Value: !Ref Environment
- Key: Name
Value: !Join [ "-", [ !Ref Environment, !Ref "AWS::StackName", "secret" ] ]
DatabaseSg:
Type: "AWS::EC2::SecurityGroup"
Properties:
GroupDescription: Database security group
VpcId: !ImportValue VpcId
Tags:
- Key: Env
Value: !Ref Environment
- Key: Name
Value: !Join [ "-", [ !Ref Environment, !Ref "AWS::StackName", "sg" ] ]
TestDatabase:
Condition: TestEnv
DeletionPolicy: Snapshot
Type: "AWS::RDS::DBCluster"
Properties:
Engine: aurora
EngineType: serverless
DbSubnetGroupName: !ImportValue DbSubnetGroupId
ScalingConfiguration:
AutoPause: True
MaxCapacity: 1
MinCapacity: 1
SecondsUntilAutoPause: 300
MasterUsername: !Join [ ":", [ "{{resolve:secretsmanager", !Ref DatabaseSecret, "SecretString", "username}}" ] ]
MasterUserPassword: !Join [ ":", [ "{{resolve:secretsmanager", !Ref DatabaseSecret, "SecretString", "password}}" ] ]
VpcSecurityGroupIds:
- !Ref DatabaseSg
Tags:
- Key: Env
Value: !Ref Environment
- Key: Name
Value: !Join [ "-", [ !Ref Environment, !Ref "AWS::StackName", "cluster" ] ]
ProdDatabase:
Condition: ProdEnv
DeletionPolicy: Retain
Type: "AWS::RDS::DBCluster"
Properties:
Engine: aurora
EngineMode: serverless
DBSubnetGroupName: !ImportValue DbSubnetGroupId
ScalingConfiguration:
AutoPause: True
MaxCapacity: 1
MinCapacity: 1
SecondsUntilAutoPause: 300
MasterUsername: !Join [ ":", [ "{{resolve:secretsmanager", !Ref DatabaseSecret, "SecretString", "username}}" ] ]
MasterUserPassword: !Join [ ":", [ "{{resolve:secretsmanager", !Ref DatabaseSecret, "SecretString", "password}}" ] ]
VpcSecurityGroupIds:
- !Ref DatabaseSg
Tags:
- Key: Env
Value: !Ref Environment
- Key: Name
Value: !Join [ "-", [ !Ref Environment, !Ref "AWS::StackName", "cluster" ] ]
DatabaseSgIngressRule:
Type: "AWS::EC2::SecurityGroupIngress"
Properties:
IpProtocol: tcp
FromPort: !If [ProdEnv, !GetAtt ProdDatabase.Enpoint.Port, !GetAtt TestDatabase.Endpoint.Port]
ToPort: !If [ProdEnv, !GetAtt ProdDatabase.Enpoint.Port, !GetAtt TestDatabase.Endpoint.Port]
SourceSecurityGroupId: !ImportValue MiddlewareInstanceSg
GroupId: !Ref DatabaseSg
Outputs:
DatabaseEndpointAddress:
Value: !If [ProdEnv, !GetAtt ProdDatabase.Endpoint.Address, !GetAtt TestDatabase.Endpoint.Address]
Export:
Name: DatabaseEndpointAddress
DatabseEndpointPort:
Value: !If [ProdEnv, !GetAtt ProdDatabase.Endpoint.Port, !GetAtt TestDatabase.Endpoint.Port]
Export:
Name: DatabseEndpointPort
DbCredentials:
Value: !Ref DatabaseSecret
Export:
Name: DbCredentials