-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.yaml
240 lines (229 loc) · 8.82 KB
/
template.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Parameters:
VpcId:
Type: AWS::EC2::VPC::Id
Description: VPC ID where the lambdas will be deployed.
SubnetIds:
Type: List<AWS::EC2::Subnet::Id>
Description: List of SUbnet IDs where the lambdas will be deployed.
DatamasqueBaseUrl:
Type: String
Description: DATAMASQUE instance URL.
DatamasqueSecretArn:
Type: String
Description: AWS secret for DATAMASQUE instance credentials.
DataMasqueSecurityGroup:
Type: String
Description: Security Group ID for connectivity between Staged DB and DM instance.
Resources:
DatamasqueRunSg:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: DATAMASQUE blueprint API run Lambda security group
VpcId:
Ref: VpcId
DatamasqueRun:
Type: AWS::Serverless::Function
Properties:
CodeUri: functions/datamasque_run/
Handler: app.lambda_handler
Runtime: python3.9
Timeout: 10
Architectures:
- x86_64
Policies:
- VPCAccessPolicy: {}
- AWSSecretsManagerGetSecretValuePolicy:
SecretArn: !Ref DatamasqueSecretArn
- AWSSecretsManagerGetSecretValuePolicy:
SecretArn: !Sub "arn:aws:secretsmanager:${AWS::Region}:${AWS::AccountId}:secret:datamasque/*connections*"
VpcConfig:
SubnetIds:
Ref: SubnetIds
SecurityGroupIds:
- Ref: DatamasqueRunSg
Environment:
Variables:
DATANASQUE_BASE_URL: !Ref DatamasqueBaseUrl
DATAMASQUE_SECRET_ARN: !Ref DatamasqueSecretArn
DescribeDBInstances:
Type: AWS::Serverless::Function # More info about Function Resource: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-function.html
Properties:
CodeUri: functions/describe_db_instances/
Handler: app.lambda_handler
Runtime: python3.9
Timeout: 10
Architectures:
- x86_64
Policies:
- AmazonRDSReadOnlyAccess
DescribeDBSnapshot:
Type: AWS::Serverless::Function # More info about Function Resource: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-function.html
Properties:
CodeUri: functions/describe_db_snapshots/
Handler: app.lambda_handler
Runtime: python3.9
Timeout: 10
Architectures:
- x86_64
Policies:
- AmazonRDSReadOnlyAccess
- Statement:
- Action:
- rds:CreateDBSnapshot
- rds:CreateDBClusterSnapshot
- rds:AddTagsToResource
Effect: Allow
Resource: "*"
RestoreDBInstanceFromSnapshot:
Type: AWS::Serverless::Function # More info about Function Resource: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-function.html
Properties:
CodeUri: functions/restore_db_instance_from_db_snapshot/
Handler: app.lambda_handler
Runtime: python3.9
Timeout: 10
Environment:
Variables:
DATANASQUE_SG: !Ref DataMasqueSecurityGroup
Architectures:
- x86_64
Policies:
- AmazonRDSReadOnlyAccess
- Statement:
- Action:
- iam:PassRole
- rds:AddTagsToResource
- rds:RestoreDBInstanceFromDBSnapshot
- rds:RestoreDBClusterFromSnapshot
Effect: Allow
Resource: "*"
CheckDBAvailability:
Type: AWS::Serverless::Function # More info about Function Resource: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-function.html
Properties:
CodeUri: functions/check_db_availability/
Handler: app.lambda_handler
Runtime: python3.9
Timeout: 10
Architectures:
- x86_64
Policies:
- AmazonRDSReadOnlyAccess
- Statement:
- Action:
- rds:AddTagsToResource
Effect: Allow
Resource: "*"
- Action:
- rds:CreateDBInstance
Effect: Allow
Resource: "*"
CreateMaskedSnapshot:
Type: AWS::Serverless::Function # More info about Function Resource: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-function.html
Properties:
CodeUri: functions/create_masked_snapshot/
Handler: app.lambda_handler
Runtime: python3.9
Timeout: 60
Architectures:
- x86_64
Policies:
- AmazonRDSReadOnlyAccess
- Statement:
- Action:
- rds:CreateDBSnapshot
- rds:CreateDBClusterSnapshot
Effect: Allow
Resource: "*"
CheckMaskedSnapshot:
Type: AWS::Serverless::Function # More info about Function Resource: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-function.html
Properties:
CodeUri: functions/check_masked_snapshot/
Handler: app.lambda_handler
Runtime: python3.9
Timeout: 60
Architectures:
- x86_64
Policies:
- AmazonRDSReadOnlyAccess
CheckMaskingRunStatus:
Type: AWS::Serverless::Function # More info about Function Resource: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-function.html
Properties:
CodeUri: functions/check_masking_run/
Handler: app.lambda_handler
Runtime: python3.9
Timeout: 20
Architectures:
- x86_64
Policies:
- VPCAccessPolicy: {}
- AWSSecretsManagerGetSecretValuePolicy:
SecretArn: !Ref DatamasqueSecretArn
VpcConfig:
SubnetIds:
Ref: SubnetIds
SecurityGroupIds:
- Ref: DatamasqueRunSg
Environment:
Variables:
DATANASQUE_BASE_URL: !Ref DatamasqueBaseUrl
DATAMASQUE_SECRET_ARN: !Ref DatamasqueSecretArn
DatamasqueBlueprintStateMachine:
Type: AWS::Serverless::StateMachine # More info about State Machine Resource: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-statemachine.html
Properties:
DefinitionUri: statemachine/datamasque_blueprint.asl.json
DefinitionSubstitutions:
DatamasqueRunFunctionArn: !GetAtt DatamasqueRun.Arn
DescribeDBSnapshotFunctionArn: !GetAtt DescribeDBSnapshot.Arn
DescribeDBInstancesFunctionArn: !GetAtt DescribeDBInstances.Arn
RestoreDBInstanceFromSnapshotFunctionArn: !GetAtt RestoreDBInstanceFromSnapshot.Arn
CheckDBAvailabilityArn: !GetAtt CheckDBAvailability.Arn
CheckMaskingRunStatus: !GetAtt CheckMaskingRunStatus.Arn
CreateMaskedSnapshot: !GetAtt CreateMaskedSnapshot.Arn
CheckMaskedSnapshot: !GetAtt CheckMaskedSnapshot.Arn
Policies:
- LambdaInvokePolicy:
FunctionName: !Ref DescribeDBInstances
- LambdaInvokePolicy:
FunctionName: !Ref DescribeDBSnapshot
- LambdaInvokePolicy:
FunctionName: !Ref RestoreDBInstanceFromSnapshot
- LambdaInvokePolicy:
FunctionName: !Ref DatamasqueRun
- LambdaInvokePolicy:
FunctionName: !Ref CheckDBAvailability
- LambdaInvokePolicy:
FunctionName: !Ref CheckMaskingRunStatus
- LambdaInvokePolicy:
FunctionName: !Ref CreateMaskedSnapshot
- LambdaInvokePolicy:
FunctionName: !Ref CheckMaskedSnapshot
- Statement:
- Action:
- rds:DeleteDBInstance
Effect: Allow
Resource:
- !Sub "arn:aws:rds:${AWS::Region}:${AWS::AccountId}:db:*-datamasque"
- !Sub "arn:aws:rds:${AWS::Region}:${AWS::AccountId}:db:*-datamasque-1"
- Action:
- rds:DeleteDBCluster
Effect: Allow
Resource: !Sub "arn:aws:rds:${AWS::Region}:${AWS::AccountId}:cluster:*-datamasque"
- Action:
- rds:CreateDBSnapshot
- rds:CreateDBClusterSnapshot
Effect: Allow
Resource: "*"
Events:
Schedule:
Type: Schedule # More info about Schedule Event Source: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-property-statemachine-schedule.html
Properties:
Description: Schedule to run the DATAMASQUE state machine weekly
Enabled: False
Schedule: "rate(7 days)"
Input: '{
"DBInstanceIdentifier": "demo-oracle-datamasque-03-rds",
"DBSecretIdentifier": "arn:aws:secretsmanager:${AWS::Region}:${AWS::AccountId}:secret:datamasque/demo/oracle_connections-TpvoiO",
"DataMasqueRulesetId": "4f115c49-43bb-4cbc-a5b8-55a5aa9509e0",
"PreferredAZ": "ap-southeast-2b"
}'