-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate.yaml
More file actions
184 lines (170 loc) · 5.16 KB
/
Copy pathtemplate.yaml
File metadata and controls
184 lines (170 loc) · 5.16 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
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
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: LearnIA Chat Assistant Lambda
Parameters:
Environment:
Type: String
Default: dev
AtlasUri:
Type: String
NoEcho: true
PostgresHost:
Type: String
PostgresPassword:
Type: String
NoEcho: true
CorsAllowOrigin:
Type: String
Default: 'https://www.learn-ia.app'
BedrockModelArn:
Type: String
Description: ARN del inference profile de Bedrock a utilizar
Default: arn:aws:bedrock:us-west-2:974724840334:inference-profile/us.amazon.nova-pro-v1:0
Globals:
Api:
Cors:
AllowOrigin: !Sub "'${CorsAllowOrigin}'"
AllowMethods: "'GET,POST,DELETE,OPTIONS'"
AllowHeaders: "'Content-Type,Authorization,X-User-Id'"
Resources:
ChatSessionsTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: !Sub 'learnia-chat-sessions-${Environment}'
BillingMode: PAY_PER_REQUEST
AttributeDefinitions:
- AttributeName: session_id
AttributeType: S
- AttributeName: timestamp
AttributeType: N
- AttributeName: user_id
AttributeType: S
- AttributeName: learning_path_id
AttributeType: S
KeySchema:
- AttributeName: session_id
KeyType: HASH
- AttributeName: timestamp
KeyType: RANGE
TimeToLiveSpecification:
Enabled: true
AttributeName: ttl
GlobalSecondaryIndexes:
- IndexName: UserPathIndex
KeySchema:
- AttributeName: user_id
KeyType: HASH
- AttributeName: learning_path_id
KeyType: RANGE
Projection:
ProjectionType: ALL
Tags:
- Key: Project
Value: LearnIA
- Key: Environment
Value: !Ref Environment
ChatAssistantFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub learnia-chat-assistant-${Environment}
Runtime: python3.12
Handler: chat_handler.lambda_handler
CodeUri: src/
Timeout: 120
MemorySize: 1024
Layers:
- !Ref CertificatesLayer
Environment:
Variables:
# DynamoDB
CHAT_SESSIONS_TABLE: !Ref ChatSessionsTable
SESSION_TTL_DAYS: 30
MAX_HISTORY_MESSAGES: 10
# MongoDB
ATLAS_URI: !Ref AtlasUri
DATABASE_NAME: learnia_db
COLLECTION_NAME: courses
# PostgreSQL
POSTGRES_HOST: !Ref PostgresHost
POSTGRES_PORT: 5432
POSTGRES_DB: postgres
POSTGRES_USER: postgres
POSTGRES_PASSWORD: !Ref PostgresPassword
DB_SSL: "true"
DB_CA_PATH: /opt/certs/rds-us-east-2-bundle.pem
# Bedrock
BEDROCK_REGION: us-west-2
BEDROCK_MODEL_ID: !Ref BedrockModelArn
BEDROCK_TEMPERATURE: 0.7
BEDROCK_MAX_TOKENS: 2048
CORS_ALLOW_ORIGIN: !Ref CorsAllowOrigin
Policies:
- AWSLambdaBasicExecutionRole
- DynamoDBCrudPolicy:
TableName: !Ref ChatSessionsTable
- Statement:
- Effect: Allow
Action:
- dynamodb:Query
Resource: !Sub '${ChatSessionsTable.Arn}/index/*'
- Effect: Allow
Action:
- bedrock:InvokeModel
Resource:
- arn:aws:bedrock:us-west-2::foundation-model/us.amazon.nova-pro-v1:0
- !Ref BedrockModelArn
- Effect: Allow
Action:
- cloudwatch:PutMetricData
Resource: '*'
Events:
SendMessage:
Type: Api
Properties:
Path: /chat/message
Method: POST
RestApiId: !Ref ChatAssistantApi
GetHistory:
Type: Api
Properties:
Path: /chat/history/{session_id}
Method: GET
RestApiId: !Ref ChatAssistantApi
GetSessions:
Type: Api
Properties:
Path: /chat/sessions
Method: GET
RestApiId: !Ref ChatAssistantApi
DeleteSession:
Type: Api
Properties:
Path: /chat/session/{session_id}
Method: DELETE
RestApiId: !Ref ChatAssistantApi
ChatAssistantApi:
Type: AWS::Serverless::Api
Properties:
StageName: Prod
Cors:
AllowOrigin: !Sub "'${CorsAllowOrigin}'"
AllowMethods: "'GET,POST,DELETE,OPTIONS'"
AllowHeaders: "'Content-Type,Authorization,X-User-Id'"
CertificatesLayer:
Type: AWS::Serverless::LayerVersion
Properties:
LayerName: !Sub learnia-rds-ca-chat-${Environment}
Description: RDS CA bundle for SSL connections
ContentUri: layer-certs/certs/
CompatibleRuntimes:
- python3.12
Outputs:
ChatAssistantApiUrl:
Description: Chat Assistant API endpoint
Value: !Sub 'https://${ChatAssistantApi}.execute-api.${AWS::Region}.amazonaws.com/Prod'
ChatAssistantFunctionArn:
Description: Lambda Function ARN
Value: !GetAtt ChatAssistantFunction.Arn
ChatSessionsTableName:
Description: DynamoDB table name
Value: !Ref ChatSessionsTable