forked from jdub233/page-capture-s3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.yml
110 lines (103 loc) · 3.65 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
AWSTemplateFormatVersion: 2010-09-09
Description: >-
page-capture-s3
Transform:
- AWS::Serverless-2016-10-31
Parameters:
CaptureUrl:
Type: String
Description: URL of page to capture
Default: https://www.bu.edu
S3BucketName:
Type: String
Description: Name of the S3 bucket to deploy captured files
Default: bu-static-page-bucket
S3Path:
Type: String
Description: Path within the S3 bucket for deployed files
SubdirPrefix:
Type: String
Description: Prefix of the S3 location (optional)
Default: home
Resources:
# SQS Queue to buffer page capture requests from the API Gateway.
# Page captures can be long running jobs, and require a queue to receive and process API Gateway requests within their 30 second timeout.
CaptureQueue:
Type: AWS::SQS::Queue
Properties:
VisibilityTimeout: 230 # Set a high visibility timeout to give page captures at least a minute or so to complete
PageCaptureFunction:
Type: AWS::Serverless::Function
Properties:
Description: Lambda function to run the page capture and upload to S3. Runs when a request comes into the Capture Queue.
Runtime: nodejs14.x
Handler: src/page-capture.pageCapture
Environment:
Variables:
CAPTURE_URL: !Ref CaptureUrl
S3_BUCKET_NAME: !Ref S3BucketName
S3_PATH: !Ref S3Path
SUBDIR_PREFIX: !Ref SubdirPrefix
Events:
# This property associates this Lambda function with the SQS queue defined above, so that whenever the queue
# receives a message, the Lambda function is invoked
SQSQueueEvent:
Type: SQS
Properties:
Queue: !GetAtt CaptureQueue.Arn
BatchSize: 1
# This event defines an hourly CloudWatch schedule
CaptureHourlySchedule:
Type: Schedule
Properties:
Schedule: 'rate(1 hour)'
Description: Runs a capture every hour once enabled
Enabled: false
MemorySize: 512
Timeout: 200 # must be within the SQS Visibility Timeout
Policies:
- AWSLambdaBasicExecutionRole
- S3CrudPolicy:
BucketName: !Ref S3BucketName
# API Gateway endpoint secured with an API Key. Anyone with the URL and API key can request new page captures.
TriggerEndpoint:
Type: AWS::Serverless::Api
Properties:
StageName: Prod
Auth:
ApiKeyRequired: true
UsagePlan:
CreateUsagePlan: PER_API
UsagePlanName: GatewayAuthorization
# Add an upper bound to monthly invocations just in case.
Quota:
Limit: 1000
Period: MONTH
# Simple Lambda function that relays requests from the API Gateway endpoint to SQS
TriggerFunction:
Type: AWS::Serverless::Function
Properties:
Description: Trigger to pass the API gateway event into SQS
Runtime: nodejs14.x
Handler: src/trigger.captureTriggerHandler
Policies:
SQSSendMessagePolicy:
QueueName: !GetAtt CaptureQueue.QueueName
Environment:
Variables:
SQSqueueName: !Ref CaptureQueue
Events:
ApiKey:
Type: Api
Properties:
RestApiId:
Ref: TriggerEndpoint
Path: /
Method: get
Outputs:
ApiGateway:
Description: "The trigger URL is:"
Value: !Sub "https://${TriggerEndpoint}.execute-api.${AWS::Region}.amazonaws.com/Prod/"
ApiKey:
Description: "You can find your API Key in the AWS console: (Put in the request HEADER as 'x-api-key')"
Value: !Sub "https://console.aws.amazon.com/apigateway/home?region=${AWS::Region}#/api-keys/${TriggerEndpointApiKey}"