-
Notifications
You must be signed in to change notification settings - Fork 0
/
serverless.yml
185 lines (173 loc) · 4.65 KB
/
serverless.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
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
service: connected-vehicles-api
stages:
default:
params:
tableName: ${self:service}-${sls:stage}
custom:
tableName: ${self:service}-${sls:stage}
region: ${opt:region, 'eu-central-1'}
pythonRequirements:
dockerizePip: true
layer:
name: python-deps
description: Python dependencies
offline:
httpPort: 4000
lambdaPort: 4002
websocketPort: 4001
noPrependStageInUrl: true
# DynamoDB Local configuration
stages:
- local
start:
port: 8000
inMemory: true
migrate: true
seed: true
seed:
local:
sources:
- table: ${self:custom.tableName}
sources: [./seeds/connected_vehicles.json]
plugins:
- serverless-python-requirements
- serverless-step-functions
- serverless-offline
provider:
name: aws
region: eu-central-1
runtime: python3.12
layers:
- !GetAtt PythonRequirements.LayerArn
environment:
TABLE_NAME: ${self:custom.tableName}
POWERTOOLS_SERVICE_NAME: ${self:service}
LOG_LEVEL: DEBUG
STATE_MACHINE_ARN: !Ref CreateItemStateMachine
DYNAMODB_ENDPOINT: ${self:custom.dynamodbEndpoint, ''}
AWS_ACCOUNT_ID: ${aws:accountId, 'LOCAL'}
httpApi:
cors: true
iam:
role:
statements:
# DynamoDB Permissions
- Effect: Allow
Action:
- dynamodb:PutItem
- dynamodb:GetItem
- dynamodb:UpdateItem
- dynamodb:DeleteItem
- dynamodb:Query
- dynamodb:Scan
Resource:
- !GetAtt ConnectedVehicleTable.Arn
- !Join ['', [!GetAtt ConnectedVehicleTable.Arn, '/index/*']]
# Step Functions Permissions
- Effect: Allow
Action:
- states:StartExecution
- states:DescribeExecution
- states:StopExecution
Resource: !Ref CreateItemStateMachine
# CloudWatch Permissions
- Effect: Allow
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Resource: 'arn:aws:logs:*:*:*'
package:
individually: true
patterns:
- '!node_modules/**'
- '!tests/**'
- '!.pytest_cache/**'
- '!__pycache__/**'
- '!requirements.txt'
- '!package.json'
- '!package-lock.json'
layers:
pythonRequirements:
name: ${self:service}-python-deps-${sls:stage}
description: Python dependencies including PynamoDB and AWS Lambda Powertools
compatibleRuntimes:
- python3.12
retain: false
functions:
apiHandler:
handler: src/handlers/api_handler.handle
events:
# Vehicle endpoints
- httpApi:
path: /vehicles
method: POST
- httpApi:
path: /vehicles/{vehicle_id}/features
method: POST
# User endpoints
- httpApi:
path: /users
method: POST
- httpApi:
path: /users/{user_id}/preferences
method: POST
environment:
POWERTOOLS_METRICS_NAMESPACE: ${self:service}-api
validateHandler:
handler: src/handlers/validate_handler.handle
environment:
POWERTOOLS_METRICS_NAMESPACE: ${self:service}-validation
processHandler:
handler: src/handlers/process_handler.handle
environment:
POWERTOOLS_METRICS_NAMESPACE: ${self:service}-process
stepFunctions:
validate: true
stateMachines:
createItemWorkflow:
name: ${self:service}-workflow-${sls:stage}
definition:
StartAt: ValidateInput
States:
ValidateInput:
Type: Task
Resource: !GetAtt validateHandler.Arn
Next: ProcessItem
Catch:
- ErrorEquals: ["ValidationError"]
Next: ValidationFailed
ProcessItem:
Type: Task
Resource: !GetAtt processHandler.Arn
Next: Success
Catch:
- ErrorEquals: ["ProcessingError"]
Next: ProcessingFailed
ValidationFailed:
Type: Fail
Cause: "Item validation failed"
ProcessingFailed:
Type: Fail
Cause: "Item processing failed"
Success:
Type: Succeed
resources:
Resources:
ConnectedVehicleTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: ${param:tableName}
KeySchema:
- AttributeName: PK
KeyType: HASH
- AttributeName: SK
KeyType: RANGE
AttributeDefinitions:
- AttributeName: PK
AttributeType: S
- AttributeName: SK
AttributeType: S
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1