-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.prod.yaml
182 lines (170 loc) · 5.41 KB
/
template.prod.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
AWSTemplateFormatVersion: "2010-09-09"
Description: >-
shopify-bot
Transform:
- AWS::Serverless-2016-10-31
Globals:
Function:
Environment:
Variables:
Region: 'us-east-1'
Runtime: nodejs18.x
MemorySize: 128 # MB
Timeout: 6 # optional, in seconds, default is 6
Resources:
ConfigFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: lambdas/config
Handler: index.handler
Architectures:
- x86_64
Description: Stores Shopify sites and Discord webhook parameters (excluding the secure API key - that is stored in Secrets Manager)
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref ConfigTable
Environment:
Variables:
# The ConfigTable stores the list of Shopify sites that the ShopifySyncFunction will check
CONFIG_TABLE: !Ref ConfigTable
Events:
Api:
Type: Api
Properties:
Path: /config
Method: POST
ShopifySyncFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: lambdas/shopifySync
Handler: index.handler
Timeout: 15
Architectures:
- x86_64
Description: Scans Shopify sites and writes updates to a DynamoDB table
Policies:
# Give Create/Read/Update/Delete Permissions to the InventoryTable
- DynamoDBCrudPolicy:
TableName: !Ref InventoryTable
- DynamoDBCrudPolicy:
TableName: !Ref ConfigTable
Environment:
Variables:
# The ConfigTable stores the list of Shopify sites that the ShopifySyncFunction will check
CONFIG_TABLE: !Ref ConfigTable
# The InventoryTable stores the product/stock items that were retrieved from the sites
INVENTORY_TABLE: !Ref InventoryTable
Events:
Api:
Type: Api
Properties:
Path: /sync
Method: POST
Schedule:
Type: Schedule
Properties:
Schedule: rate(5 minutes)
Name: ShopifySyncSchedule
Description: Schedules the ShopifySyncFunction to run every 5 minutes
Enabled: true
RetryPolicy:
MaximumEventAgeInSeconds: 86400
MaximumRetryAttempts: 185
QueuePublisherFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: lambdas/queuePublisher
Handler: index.handler
Architectures:
- x86_64
Policies:
- Statement:
# Adds the necessary permissions for the Lambda function to write to the SQS queue
- Effect: Allow
Action:
- sqs:SendMessage
Resource: !GetAtt InventoryUpdateQueue.Arn
Description: |2
Receives DynamoDB event streams from the InventoryTable and decides whether a Discord message should be sent. If
so, then craft notify the SNS Topic. DiscordNotifierFunction subscribes and sends messages from the queue.
Environment:
Variables:
QUEUE_URL: !Ref InventoryUpdateQueue
Events:
StreamEvent:
Type: DynamoDB
Properties:
Stream: !GetAtt InventoryTable.StreamArn
StartingPosition: TRIM_HORIZON
BatchSize: 1
Enabled: true
DiscordNotifierFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: lambdas/discordNotifier
Handler: index.handler
Architectures:
- x86_64
Description: Sends a Discord notification when a product is available
Policies:
- AWSSecretsManagerGetSecretValuePolicy:
# Grants read access to Secrets Manager; used to retrieve Discord API key
SecretArn: !Ref DiscordApiKey
Environment:
Variables:
DISCORD_SECRET_ARN: !Ref DiscordApiKey
QUEUE_URL: !Ref InventoryUpdateQueue
Events:
Api:
Type: Api
Properties:
Path: /
Method: POST
SQSEvent:
Type: SQS
Properties:
Queue: !GetAtt InventoryUpdateQueue.Arn
InventoryUpdateQueue:
Type: AWS::SQS::Queue
Properties:
FifoQueue: true
QueueName: InventoryUpdateQueue.fifo
ContentBasedDeduplication: true
ConfigTable:
Type: AWS::DynamoDB::Table
Properties:
AttributeDefinitions:
- AttributeName: name
AttributeType: S
KeySchema:
- AttributeName: name
KeyType: HASH
StreamSpecification:
StreamViewType: NEW_AND_OLD_IMAGES
ProvisionedThroughput:
ReadCapacityUnits: 2
WriteCapacityUnits: 2
InventoryTable:
Type: AWS::DynamoDB::Table
Properties:
AttributeDefinitions:
- AttributeName: id
AttributeType: S
KeySchema:
- AttributeName: id
KeyType: HASH
StreamSpecification:
StreamViewType: NEW_AND_OLD_IMAGES
ProvisionedThroughput:
ReadCapacityUnits: 9
WriteCapacityUnits: 9
DiscordApiKey:
Type: AWS::SecretsManager::Secret
Properties:
Description: 'This is the sensitive hash value that comes after "api/hooks/" in the Discord API key'
Name: DiscordApiKey
SecretString: '{"discord-api-key":"this-is-not-a-valid-key-please-replace"}'
Outputs:
WebEndpoint:
Description: "API Gateway endpoint URL for Prod stage"
Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/"