-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate.yaml
More file actions
264 lines (241 loc) · 7.22 KB
/
template.yaml
File metadata and controls
264 lines (241 loc) · 7.22 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
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: |
API that processes invoices.
Parameters:
AppBucketName:
Description: Bucket name to use for image storage.
Type: String
Default: bucket-api-receipt-processing
AppStageName:
Description: dev | prod
Type: String
Default: dev
Resources:
StateMachineLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: "/aws/vendedlogs/states/PostInvoiceStateMachine-Logs"
RetentionInDays: 30
InvoiceProxy:
Type: AWS::Serverless::Function
Properties:
CodeUri: functions/InvoiceProxy/
Handler: InvoiceProxy.lambda_handler
Runtime: python3.12
Architectures:
- x86_64
Policies:
- S3CrudPolicy:
BucketName: !Ref AppBucketName
- Statement:
- Effect: Allow
Action:
- states:StartSyncExecution
Resource: !GetAtt PostInvoiceStateMachine.Arn
Events:
APIGatewayEvent:
Type: Api
Properties:
Method: post
Path: /api/v1/invoice
RestApiId: !Ref ApiGateway
Timeout: 40
Environment:
Variables:
BUCKET_NAME: !Ref AppBucketName
STATE_MACHINE_ARN: !GetAtt PostInvoiceStateMachine.Arn
ExtractInfo:
Type: AWS::Serverless::Function
Properties:
CodeUri: functions/ExtractInfo/
Handler: ExtractInfo.lambda_handler
Runtime: python3.12
Architectures:
- x86_64
Policies:
- S3CrudPolicy:
BucketName: !Ref AppBucketName
- Statement:
- Effect: Allow
Action:
- textract:AnalyzeExpense
Resource: '*'
Timeout: 20
Environment:
Variables:
BUCKET_NAME: !Ref AppBucketName
NLTKDependenciesLayer:
Type: AWS::Lambda::LayerVersion
Properties:
LayerName: nltk-layer
Description: Layer with necessary NLTK dependencies for RefineData.
Content: ./layers/nltk-dependencies.zip
CompatibleRuntimes:
- python3.12
RefineData:
Type: AWS::Serverless::Function
Properties:
CodeUri: functions/RefineData/
Handler: RefineData.lambda_handler
Runtime: python3.12
Architectures:
- x86_64
Timeout: 50
Layers:
- !Ref NLTKDependenciesLayer
Policies:
- Statement:
- Effect: Allow
Action:
- bedrock:InvokeModel
Resource: '*'
ResultHandler:
Type: AWS::Serverless::Function
Properties:
CodeUri: functions/ResultHandler/
Handler: ResultHandler.lambda_handler
Runtime: python3.12
Architectures:
- x86_64
Policies:
- S3CrudPolicy:
BucketName: !Ref AppBucketName
Environment:
Variables:
BUCKET_NAME: !Ref AppBucketName
Timeout: 10
MainPage:
Type: AWS::Serverless::Function
Properties:
CodeUri: functions/MainPage/
Handler: MainPage.lambda_handler
Runtime: python3.12
Architectures:
- x86_64
Events:
APIGatewayEvent:
Type: Api
Properties:
Method: get
Path: /
RestApiId: !Ref ApiGateway
Timeout: 3
ApiGateway:
Type: AWS::Serverless::Api
Properties:
Name: receipt-api
StageName: !Ref AppStageName
DefinitionBody:
x-amazon-apigateway-binary-media-types:
- multipart~1form-data
- image~1jpg
- image~1jpeg
- image~1png
openapi: 3.0.3
info:
title: API for Receipt Processing
description: API for Receipt/Invoice Processing
version: v1.0
paths:
/api/v1/invoice:
post:
description: Processes invoice.
responses:
'200':
description: Success.
'500':
description: An error ocurred.
x-amazon-apigateway-integration:
type: aws_proxy
contentHandling: CONVERT_TO_BINARY
uri: !Sub arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${InvoiceProxy.Arn}/invocations
httpMethod: POST
/:
get:
description: Web page for invoice processing.
responses:
'200':
description: Success.
'500':
description: An error ocurred.
x-amazon-apigateway-integration:
type: aws_proxy
uri: !Sub arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${MainPage.Arn}/invocations
httpMethod: POST
PostInvoiceStateMachine:
Type: AWS::Serverless::StateMachine
Properties:
Type: EXPRESS
Logging: # Nova seção adicionada
Level: ALL
IncludeExecutionData: false # !Equals [!Ref AppStageName, dev]
Destinations:
- CloudWatchLogsLogGroup:
LogGroupArn: !GetAtt StateMachineLogGroup.Arn
DefinitionSubstitutions:
# variables to be used in .asl.json
ExtractInfoArn: !GetAtt ExtractInfo.Arn
RefineDataArn: !GetAtt RefineData.Arn
ResultHandlerArn: !GetAtt ResultHandler.Arn
Policies:
- Statement:
- Effect: Allow
Action:
- lambda:InvokeFunction
Resource:
- !GetAtt ExtractInfo.Arn
- !GetAtt RefineData.Arn
- !GetAtt ResultHandler.Arn
- Effect: Allow # Novas permissões para logs
Action:
- logs:CreateLogDelivery
- logs:GetLogDelivery
- logs:UpdateLogDelivery
- logs:DeleteLogDelivery
- logs:ListLogDeliveries
- logs:PutResourcePolicy
- logs:DescribeResourcePolicies
- logs:DescribeLogGroups
Resource: "*"
DefinitionUri: PostInvoiceStateMachine/PostInvoiceStateMachine.asl.json
AppBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: !Ref AppBucketName
InvoiceProxyLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Join [ "/", ["/aws/lambda", !Ref InvoiceProxy ] ]
RetentionInDays: 14
ExtractInfoLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Join [ "/", ["/aws/lambda", !Ref ExtractInfo ] ]
RetentionInDays: 14
RefineDataLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Join [ "/", ["/aws/lambda", !Ref RefineData ] ]
RetentionInDays: 14
ResultHandlerLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Join [ "/", ["/aws/lambda", !Ref ResultHandler ] ]
RetentionInDays: 14
MainPageLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Join [ "/", ["/aws/lambda", !Ref MainPage ] ]
RetentionInDays: 14
Outputs:
PostInvoiceStateMachineArn:
Description: Post Invoice State machine ARN
Value: !GetAtt PostInvoiceStateMachine.Arn
Globals:
Function:
LoggingConfig:
LogFormat: JSON
Api:
BinaryMediaTypes:
- '*~1*'