-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.yaml
234 lines (224 loc) · 7.47 KB
/
template.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
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
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
ask-me-anything
# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
Function:
Timeout: 30
MemorySize: 4096
Parameters:
# Skill
DefaultLlmServiceId:
Type: String
Default: "OpenAI"
DefaultLlmModel:
Type: String
Default: "gpt-4o"
OpenAiApiKey:
Type: String
DefaultOpenAiModel:
Type: String
Default: "gpt-4o"
# Skill + Backend
LogLevel:
Type: String
Default: error
AllowedValues:
- error
- warn
- info
- debug
# DynamoDbEndpointOverride:
# Type: String
# Default: ""
DynamoDbRegion:
Type: String
Default: "eu-west-1"
DynamoDbUserDataTableName:
Type: String
Default: "AskMeAnything-UserData"
DynamoDbUserDataTablePartitionKeyName:
Type: String
Default: "id"
DynamoDbAccountMappingsTableName:
Type: String
Default: "AskMeAnything-AccountMappings"
DynamoDbAccountMappingsTablePartitionKeyName:
Type: String
Default: "id"
UsernameSalt:
Type: String
PasswordSalt:
Type: String
EncryptedUserIdSalt:
Type: String
EncryptedApiKeySalt:
Type: String
GooglePseSearchEngineId:
Type: String
GooglePseApiKey:
Type: String
CustomSearchEngineBlocklist:
Type: String
Resources:
############
# Lambda for Alexa Skill
############
AskMeAnything:
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
Properties:
CodeUri: ./
Handler: src/index-ama.handler
Runtime: nodejs20.x
Architectures:
- x86_64
Environment:
Variables:
LOG_LEVEL: !Ref LogLevel
LOG_LEVEL_LOCALE_SERVICE: "warn"
DEFAULT_LLM_SERVICE_ID: !Ref DefaultLlmServiceId
DEFAULT_LLM_MODEL: !Ref DefaultLlmModel
OPEN_AI_API_KEY: !Ref OpenAiApiKey
DEFAULT_OPEN_AI_MODEL: !Ref DefaultOpenAiModel
# DYNAMO_DB_ENDPOINT_OVERRIDE: !Ref DynamoDbEndpointOverride
DYNAMO_DB_REGION: !Ref DynamoDbRegion
DYNAMO_DB_USER_DATA_TABLE_NAME: !Ref DynamoDbUserDataTableName
DYNAMO_DB_USER_DATA_TABLE_PARTITION_KEY_NAME: !Ref DynamoDbUserDataTablePartitionKeyName
DYNAMO_DB_ACCOUNT_MAPPINGS_TABLE_NAME: !Ref DynamoDbAccountMappingsTableName
DYNAMO_DB_ACCOUNT_MAPPINGS_TABLE_PARTITION_KEY_NAME: !Ref DynamoDbAccountMappingsTablePartitionKeyName
USERNAME_SALT: !Ref UsernameSalt
PASSWORD_SALT: !Ref PasswordSalt
ENCRYPTED_USER_ID_SALT: !Ref EncryptedUserIdSalt
ENCRYPTED_API_KEY_SALT: !Ref EncryptedApiKeySalt
GOOGLE_PSE_SEARCH_ENGINE_ID: !Ref GooglePseSearchEngineId
GOOGLE_PSE_API_KEY: !Ref GooglePseApiKey
CUSTOM_SEARCH_ENGINE_BLOCKLIST: !Ref CustomSearchEngineBlocklist
AskMeAnythingLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Sub "/aws/lambda/${AskMeAnything}"
RetentionInDays: 1
AskMeAnythingUserDataDynamoDbTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: !Ref DynamoDbUserDataTableName
AttributeDefinitions:
- AttributeName: !Ref DynamoDbUserDataTablePartitionKeyName
AttributeType: S
KeySchema:
- AttributeName: !Ref DynamoDbUserDataTablePartitionKeyName
KeyType: HASH
BillingMode: PAY_PER_REQUEST
AskMeAnythingAccountMappingsDynamoDbTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: !Ref DynamoDbAccountMappingsTableName
AttributeDefinitions:
- AttributeName: !Ref DynamoDbAccountMappingsTablePartitionKeyName
AttributeType: S
KeySchema:
- AttributeName: !Ref DynamoDbAccountMappingsTablePartitionKeyName
KeyType: HASH
BillingMode: PAY_PER_REQUEST
############
# Backend
############
AmaBackendLambdaUsagePlan:
Type: AWS::ApiGateway::UsagePlan
Properties:
ApiStages:
- ApiId: !Ref AmaBackendApiGateway
Stage: production
Description: "Usage plan for AskMeAnything Lambda function"
Quota:
Limit: 5000
Period: MONTH
Throttle:
BurstLimit: 1000
RateLimit: 100
AmaBackendLambdaApiKey:
Type: AWS::ApiGateway::ApiKey
Properties:
Description: "API Key for AskMeAnything Lambda function to access AmaBackend"
Enabled: true
AmaBackendLambdaUsagePlanKey:
Type: AWS::ApiGateway::UsagePlanKey
Properties:
KeyId: !Ref AmaBackendLambdaApiKey
KeyType: API_KEY
UsagePlanId: !Ref AmaBackendLambdaUsagePlan
AmaBackendApiGateway:
Type: AWS::Serverless::Api
Properties:
StageName: production
AmaBackend:
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
Properties:
CodeUri: ./
Handler: src/index-ama-backend.handler
Runtime: nodejs20.x
Architectures:
- x86_64
Events:
UpdateAccountApiKeyEvent:
Type: Api
Properties:
Path: /accounts/{username}/apiKey
Method: put
Auth:
ApiKeyRequired: true
RestApiId:
Ref: AmaBackendApiGateway
Environment:
Variables:
LOG_LEVEL: !Ref LogLevel
LOG_LEVEL_LOCALE_SERVICE: "warn"
DYNAMO_DB_REGION: !Ref DynamoDbRegion
DYNAMO_DB_USER_DATA_TABLE_NAME: !Ref DynamoDbUserDataTableName
DYNAMO_DB_USER_DATA_TABLE_PARTITION_KEY_NAME: !Ref DynamoDbUserDataTablePartitionKeyName
DYNAMO_DB_ACCOUNT_MAPPINGS_TABLE_NAME: !Ref DynamoDbAccountMappingsTableName
DYNAMO_DB_ACCOUNT_MAPPINGS_TABLE_PARTITION_KEY_NAME: !Ref DynamoDbAccountMappingsTablePartitionKeyName
USERNAME_SALT: !Ref UsernameSalt
PASSWORD_SALT: !Ref PasswordSalt
ENCRYPTED_USER_ID_SALT: !Ref EncryptedUserIdSalt
ENCRYPTED_API_KEY_SALT: !Ref EncryptedApiKeySalt
AmaBackendLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Sub "/aws/lambda/${AmaBackend}"
RetentionInDays: 1
AmaBackendPurgeInactiveUsersRule:
Type: AWS::Events::Rule
Properties:
ScheduleExpression: "rate(1 day)"
State: ENABLED
Targets:
- Arn: !GetAtt AmaBackend.Arn
Id: "PurgeInactiveUsers"
Input: '{"command": "PurgeInactiveUsers"}'
AmaBackendPurgeInactiveUsersPermission:
Type: AWS::Lambda::Permission
Properties:
FunctionName: !GetAtt AmaBackend.Arn
Action: "lambda:InvokeFunction"
Principal: "events.amazonaws.com"
SourceArn: !GetAtt AmaBackendPurgeInactiveUsersRule.Arn
Outputs:
# Skill
AskMeAnything:
Description: AskMeAnything Lambda Function ARN
Value: !GetAtt AskMeAnything.Arn
AskMeAnythingIamRole:
Description: Implicit IAM Role created for AskMeAnything Lambda Function
Value: !GetAtt AskMeAnythingRole.Arn
# Backend
AmaBackend:
Description: AmaBackend Lambda Function ARN
Value: !GetAtt AmaBackend.Arn
AmaBackendIamRole:
Description: Implicit IAM Role created for AMA Backend Lambda Function
Value: !GetAtt AmaBackendRole.Arn
AmaBackendEndpoint:
Description: "AmaBackendEndpoint"
Value: !Sub "https://${AmaBackendApiGateway}.execute-api.${AWS::Region}.amazonaws.com/production"