-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcfn.yaml
166 lines (163 loc) · 5.07 KB
/
cfn.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
AWSTemplateFormatVersion: 2010-09-09
Description: Consumes events produced when an app receives a notification
Parameters:
Stack:
Description: Stack name
Type: String
Default: mobile-notifications
App:
Description: Application name
Type: String
Default: eventconsumer
Stage:
Description: Stage name
Type: String
AllowedValues:
- CODE
- PROD
Default: CODE
DeployBucket:
Description: Bucket where RiffRaff uploads artifacts on deploy
Type: String
Default: mobile-notifications-dist
BucketNamePrefix:
Description: Bucket name prefix to use
Type: String
AthenaDatabase:
Description: Athena Database to query
Type: String
Mappings:
StageVariables:
CODE:
BucketStage: code
PROD:
BucketStage: prod
Resources:
ExecutionRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action: sts:AssumeRole
Path: /
Policies:
- PolicyName: logs
PolicyDocument:
Statement:
- Effect: Allow
Action: logs:CreateLogGroup
Resource: !Sub "arn:aws:logs:eu-west-1:${AWS::AccountId}:*"
- Effect: Allow
Action:
- logs:CreateLogStream
- logs:PutLogEvents
Resource:
- !Sub "arn:aws:logs:eu-west-1:${AWS::AccountId}:log-group:/aws/lambda/${Stack}-${App}-athena-${Stage}:*"
- PolicyName: Athena
PolicyDocument:
Statement:
Effect: Allow
Action:
- athena:StartQueryExecution
- athena:GetQueryResultsStream
- athena:GetNamespace
- athena:GetQueryResults
- athena:GetQueryExecutions
- athena:GetNamedQuery
- athena:GetCatalogs
- athena:GetNamespaces
- athena:GetWorkGroup
- athena:GetExecutionEngine
- athena:GetQueryExecution
- athena:GetExecutionEngines
- athena:GetTables
- athena:GetTable
- athena:BatchGetNamedQuery
- athena:BatchGetQueryExecution
- glue:GetTable
- glue:GetPartition
- glue:GetPartitions
- glue:GetDatabase
- glue:BatchCreatePartition
Resource: "*"
- PolicyName: s3
PolicyDocument:
Statement:
Effect: Allow
Action:
- s3:*
Resource:
- !Sub
- arn:aws:s3:::aws-mobile-event-logs-${BucketStage}
- BucketStage: !FindInMap [StageVariables, !Ref Stage, BucketStage]
- !Sub
- arn:aws:s3:::aws-mobile-event-logs-${BucketStage}/*
- BucketStage: !FindInMap [StageVariables, !Ref Stage, BucketStage]
- PolicyName: Dynamo
PolicyDocument:
Statement:
Effect: Allow
Action: dynamodb:*
Resource: !Sub "arn:aws:dynamodb:eu-west-1:${AWS::AccountId}:table/mobile-notifications-reports-${Stage}"
AthenaLambda:
Type: AWS::Lambda::Function
Properties:
FunctionName: !Sub ${Stack}-${App}-athena-${Stage}
Code:
S3Bucket:
Ref: DeployBucket
S3Key: !Sub ${Stack}/${Stage}/${App}/${App}.jar
Environment:
Variables:
Stage: !Ref Stage
Stack: !Ref Stack
App: !Ref App
IngestLocation: !Sub
- s3://${BucketNamePrefix}-${BucketStage}/fastly/notifications/received
- BucketStage: !FindInMap [StageVariables, !Ref Stage, BucketStage]
AthenaOutputLocation: !Sub
- s3://${BucketNamePrefix}-${BucketStage}/athena
- BucketStage: !FindInMap [StageVariables, !Ref Stage, BucketStage]
AthenaDatabase: !Ref AthenaDatabase
Description: Queries Athena to update Notification reports in Dynamodb
Handler: com.gu.notifications.events.AthenaLambda::handleRequest
MemorySize: 1024
Role: !GetAtt ExecutionRole.Arn
Runtime: java11
Timeout: 120
Tags:
- Key: Stage
Value: !Ref Stage
- Key: Stack
Value: !Ref Stack
- Key: App
Value: !Ref App
S3Bucket:
Type: AWS::S3::Bucket
Properties:
AccessControl: Private
BucketName: !Sub
- ${BucketNamePrefix}-${BucketStage}
- BucketStage: !FindInMap [StageVariables, !Ref Stage, BucketStage]
LifecycleConfiguration:
Rules:
- ExpirationInDays: 21
Status: Enabled
AthenaLambdaScheduleEventRule:
Type: AWS::Events::Rule
Properties:
ScheduleExpression: "cron(2/5 * * * ? *)"
Targets:
- Id: AthenaLambdaScheduleEventRuleTarget
Arn: !GetAtt AthenaLambda.Arn
AthenaLambdaScheduleEventPermission:
Type: AWS::Lambda::Permission
Properties:
FunctionName: !GetAtt AthenaLambda.Arn
Action: lambda:InvokeFunction
Principal: events.amazonaws.com
SourceArn: !GetAtt AthenaLambdaScheduleEventRule.Arn