-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.yml
269 lines (251 loc) · 7.66 KB
/
template.yml
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
Transform:
- AWS::LanguageExtensions
- AWS::Serverless-2016-10-31
Parameters:
ImageTag:
Type: String
Globals:
Function:
Runtime: python3.9
Resources:
ResourceGroup:
Type: "AWS::ResourceGroups::Group"
Properties:
Name: !Ref AWS::StackName
Description: "Resources for the dart_warehouse POC"
DatabaseSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Allow SQL ingress
VpcId: vpc-27ccbb5d
SecurityGroupIngress:
- IpProtocol: "-1"
CidrIp: 0.0.0.0/0
RDSCluster:
Type: "AWS::RDS::DBCluster"
Properties:
Engine: aurora-postgresql
EngineVersion: 14.6
MasterUsername: postgres
ManageMasterUserPassword: true
ServerlessV2ScalingConfiguration:
MinCapacity: 0.5
MaxCapacity: 1
VpcSecurityGroupIds:
- !Ref DatabaseSecurityGroup
DBInstance:
Type: "AWS::RDS::DBInstance"
Properties:
DBClusterIdentifier: !Ref RDSCluster
DBInstanceClass: db.serverless
Engine: aurora-postgresql
PubliclyAccessible: true
# RedshiftNamespace:
# Type: AWS::RedshiftServerless::Namespace
# Properties:
# NamespaceName: dart-warehouse
# RedshiftWorkgroup:
# Type: AWS::RedshiftServerless::Workgroup
# Properties:
# WorkgroupName: dart-warehouse-wg
# NamespaceName: !Ref RedshiftNamespace
# PubliclyAccessible: true
# BaseCapacity: 32
# SecurityGroupIds:
# - !Ref RedshiftSecurityGroup
# Tags:
# - Key: RedshiftDataFullAccess
# Value: "true"
PollVehiclePositions:
Type: AWS::Serverless::Function
DependsOn:
- DBInstance
Properties:
CodeUri: poll_dart_functions
Handler: poll_vehicle_positions.handler
Layers:
- arn:aws:lambda:us-east-1:177933569100:layer:AWS-Parameters-and-Secrets-Lambda-Extension:4
MemorySize: 256
Timeout: 30
Environment:
Variables:
DATABASE_HOST: !GetAtt RDSCluster.Endpoint.Address
DATABASE_PORT: !GetAtt RDSCluster.Endpoint.Port
DATABASE_SECRET_ARN: !GetAtt RDSCluster.MasterUserSecret.SecretArn
Policies:
- Version: '2012-10-17'
Statement:
- Effect: Allow
Action: "secretsmanager:GetSecretValue"
Resource: !GetAtt RDSCluster.MasterUserSecret.SecretArn
Events:
ScheduleEvent:
Type: ScheduleV2
Properties:
ScheduleExpression: "rate(1 minute)"
DBTTaskRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service: ecs-tasks.amazonaws.com
Action: sts:AssumeRole
Condition:
StringEquals:
"aws:SourceAccount": !Ref AWS::AccountId
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy
- arn:aws:iam::aws:policy/AmazonECS_FullAccess
Policies:
- PolicyName: root
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action: "secretsmanager:GetSecretValue"
Resource: !GetAtt RDSCluster.MasterUserSecret.SecretArn
LogGroup:
Type: AWS::Logs::LogGroup
Properties:
RetentionInDays: 14
Cluster:
Type: AWS::ECS::Cluster
Properties:
CapacityProviders:
- FARGATE
DBTTaskDefinition:
Type: AWS::ECS::TaskDefinition
Properties:
Family: dart-warehouse-dbt
Cpu: 512
Memory: 1024
NetworkMode: awsvpc
RequiresCompatibilities:
- FARGATE
TaskRoleArn: !GetAtt DBTTaskRole.Arn
ExecutionRoleArn: !GetAtt DBTTaskRole.Arn
ContainerDefinitions:
- Name: dbt
Image: !Sub "ghcr.io/sourceallies/dart-warehouse/dbt:${ImageTag}"
Command:
- build
Environment:
- Name: DBT_LOG_FORMAT
Value: json
- Name: DBT_HOST
Value: !GetAtt RDSCluster.Endpoint.Address
Secrets:
- Name: DBT_USER
ValueFrom: !Sub "${RDSCluster.MasterUserSecret.SecretArn}:username::"
- Name: DBT_PASSWORD
ValueFrom: !Sub "${RDSCluster.MasterUserSecret.SecretArn}:password::"
LogConfiguration:
LogDriver: awslogs
Options:
awslogs-group: !Ref LogGroup
awslogs-region: !Ref AWS::Region
awslogs-stream-prefix: dbt
StateMachineRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service: states.amazonaws.com
Action: sts:AssumeRole
Condition:
StringEquals:
"aws:SourceAccount": !Ref AWS::AccountId
Policies:
- PolicyName: runtasks
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- ecs:*
- events:PutTargets
- events:PutRule
- events:DescribeRule
Resource: "*"
- Effect: Allow
Action: iam:PassRole
Resource: !GetAtt DBTTaskRole.Arn
WarehouseBuildStateMachine:
Type: AWS::StepFunctions::StateMachine
Properties:
RoleArn: !GetAtt StateMachineRole.Arn
Definition:
StartAt: RunDBT
States:
RunDBT:
Type: Task
End: true
Resource: "arn:aws:states:::ecs:runTask.sync"
TimeoutSeconds: 1800 # 30min
Retry:
- ErrorEquals: [ "States.ALL" ]
MaxAttempts: 5
Parameters:
Cluster: !GetAtt Cluster.Arn
TaskDefinition: !Ref DBTTaskDefinition
LaunchType: FARGATE
NetworkConfiguration:
AwsvpcConfiguration:
AssignPublicIp: ENABLED
Subnets:
- subnet-d30953b4
- subnet-86c62ccb
- subnet-51001c5e
- subnet-b3092eef
- subnet-29b0de17
- subnet-3b103115
SchedulerRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service: scheduler.amazonaws.com
Action: sts:AssumeRole
Condition:
StringEquals:
"aws:SourceAccount": !Ref AWS::AccountId
Policies:
- PolicyName: runtasks
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- ecs:RunTask
Resource: "*"
- Effect: Allow
Action: states:*
Resource: !Ref WarehouseBuildStateMachine
WarehouseBuildSchedule:
Type: AWS::Scheduler::Schedule
Properties:
Description: Runs the DART Warehouse build process
ScheduleExpression: "cron(0 10 * * ? *)"
ScheduleExpressionTimezone: "America/Chicago"
FlexibleTimeWindow:
Mode: "OFF"
Target:
Arn: !Ref WarehouseBuildStateMachine
RoleArn: !GetAtt SchedulerRole.Arn
Input: "{}"
Outputs:
DatabaseAddress:
Value: !GetAtt RDSCluster.Endpoint.Address
DatabasePort:
Value: !GetAtt RDSCluster.Endpoint.Port
DatabaseSecretArn:
Value: !GetAtt RDSCluster.MasterUserSecret.SecretArn