forked from LouisvilleMetro/WazeCCPProcessor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWazeProcessorStack.yml
More file actions
115 lines (104 loc) · 3.96 KB
/
WazeProcessorStack.yml
File metadata and controls
115 lines (104 loc) · 3.96 KB
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
AWSTemplateFormatVersion: 2010-09-09
# define the variables that user needs to provide
Parameters:
EnvironmentName:
Type: String
Description: 'Enter name of the environment (Dev, Test, Prod, etc)'
AllowedPattern: '^[a-z0-9]*$'
ConstraintDescription: Environment name must be lower case
S3ArtifactsBucket:
Type: String
Description: Enter the bucket name where Lambda function artifacts are stored
WazeDataHttpUrl:
Type: String
Description: Url to your Waze CCP data feed
Resources:
# create a cloudwatch event that will run on a schedule
DataRetrievalTimer:
Type: 'AWS::Events::Rule'
Properties:
Name: !Sub 'scripted-data-retrieval-timer-${EnvironmentName}'
ScheduleExpression: rate(2 minutes)
Targets:
-
Arn: !GetAtt DataRetrievalLambda.Arn
Id: !Sub 'DataRetrievalTimerTarget-${EnvironmentName}'
# create the S3 bucket that will store our data files
WazeDataBucket:
Type: 'AWS::S3::Bucket'
Properties:
BucketName: !Sub 'scripted-waze-data-${AWS::AccountId}-${EnvironmentName}'
# create the SQS queue that will track new data
DataProcessingQueue:
Type: 'AWS::SQS::Queue'
Properties:
QueueName: !Sub 'scripted-waze-data-processing-${EnvironmentName}'
# TODO: create the dead letter queue
# TODO: create a cloudwatch alarm to monitor the dead letter queue
# TODO: create an SNS topic that can send notifications when alarm on dead letter queue fires
# create the lambda function that will get data from waze, store it in S3, notify the queue, and start the first round of processing
DataRetrievalLambda:
Type: 'AWS::Lambda::Function'
Properties:
Runtime: 'nodejs6.10'
Code:
S3Bucket: !Sub '${S3ArtifactsBucket}'
S3Key: 'waze-data-download.zip'
Handler: 'waze-data-download.downloadData'
FunctionName: !Sub 'waze-data-retrieval-${EnvironmentName}'
Description: 'hits the waze API and saves the data as json to an s3 bucket'
Role: !GetAtt DataRetrievalExecutionRole.Arn
Timeout: 120
MemorySize: 128
Environment:
Variables:
'WAZEDATAURL' : !Sub '${WazeDataHttpUrl}'
'WAZEDATABUCKET' : !Ref WazeDataBucket
# have to give cloudwatch permission to actually fire the lambda func
DataRetrievalLambdaPermission:
Type: 'AWS::Lambda::Permission'
Properties:
Action: 'lambda:InvokeFunction'
FunctionName: !GetAtt DataRetrievalLambda.Arn
Principal: 'events.amazonaws.com'
SourceArn: !GetAtt DataRetrievalTimer.Arn
# create a service role for the data retriever lambda function
DataRetrievalExecutionRole:
Type: 'AWS::IAM::Role'
Properties:
RoleName: !Sub 'scripted-data-retrieval-execution-role-${EnvironmentName}'
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Action: 'sts:AssumeRole'
Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
ManagedPolicyArns:
- !Ref DataRetrievalResourceAccess
# create a policy to allow access to the data bucket, the queue, and execution of the data processor
DataRetrievalResourceAccess:
Type: 'AWS::IAM::ManagedPolicy'
Properties:
ManagedPolicyName: !Sub 'scripted-data-and-queue-access-policy-${EnvironmentName}'
PolicyDocument:
Version: 2012-10-17
Statement:
- Action:
- 's3:GetObject'
- 's3:PutObject'
- 's3:DeleteObject'
- 's3:ListBucket'
Effect: Allow
Resource:
- !GetAtt WazeDataBucket.Arn
- !Sub '${WazeDataBucket.Arn}/*'
- Action:
- 'sqs:SendMessage'
- 'sqs:ReceiveMessage'
- 'sqs:DeleteMessage'
Effect: Allow
Resource:
- !GetAtt DataProcessingQueue.Arn
#TODO: Add logging permissions so lambda can log errors to cloudwatch