@@ -129,75 +129,110 @@ extension ServerlessConfig {
129
129
executable: String ,
130
130
artifact: String
131
131
) throws -> ServerlessConfig {
132
- let keyedPath = " \( httpAPIPath) /{ \( dynamoDBKey) } "
133
- let dynamoResourceName = " \( executable) Table "
134
-
135
- let environmentTableName = " DYNAMO_DB_TABLE_NAME "
136
- let environmentKeyName = " DYNAMO_DB_KEY "
137
-
138
- let iam = Iam (
139
- role: Role (
140
- statements: [ . allowLogAccess( resource: try YAMLContent ( with: " * " ) ) ,
141
- . allowDynamoDBReadWrite( resource: try YAMLContent ( with: [ [ " Fn::GetAtt " : [ dynamoResourceName, " Arn " ] ] ] ) ) ]
142
- )
132
+ let keyedPath = " \( httpAPIPath) /{ \( dynamoDBKey) } "
133
+ let dynamoResourceName = " \( executable) Table "
134
+
135
+ let environmentTableName = " DYNAMO_DB_TABLE_NAME "
136
+ let environmentKeyName = " DYNAMO_DB_KEY "
137
+ let dynamoResource = try YAMLContent ( with: [ [ " Fn::GetAtt " : [ dynamoResourceName, " Arn " ] ] ] )
138
+ let iam = Iam (
139
+ role: Role (
140
+ statements: [ . allowLogAccess( resource: try YAMLContent ( with: " * " ) ) ,
141
+ . allowDynamoDBReadWrite( resource: dynamoResource) ]
143
142
)
144
- let environment = try YAMLContent ( with: [ environmentTableName: " ${self:custom.tableName} " ,
145
- environmentKeyName: " ${self:custom.keyName} " ] )
146
- let provider = Provider (
147
- name: . aws,
148
- region: region,
149
- runtime: runtime,
150
- environment: environment,
151
- architecture: architecture,
152
- httpAPI: . init(
153
- payload: " 2.0 " ,
154
- cors: true ,
155
- authorizers:
143
+ )
144
+ let environment = try YAMLContent ( with: [ environmentTableName: " ${self:custom.tableName} " ,
145
+ environmentKeyName: " ${self:custom.keyName} " ] )
146
+ let provider = Provider (
147
+ name: . aws,
148
+ region: region,
149
+ runtime: runtime,
150
+ environment: environment,
151
+ architecture: architecture,
152
+ httpAPI: . init(
153
+ payload: " 2.0 " ,
154
+ cors: true ,
155
+ authorizers:
156
156
. dictionary( [
157
157
" JWTAuthorizer " : . buildJWTAuthorizer( issuerUrl: " https://appleid.apple.com " ,
158
- audience: [ " com.mydomain.myhost " ] ) ,
158
+ audience: [ " com.mydomain.myhost " ] ) ,
159
159
" customAuthorizer " : . buildCustomAuthorizer( name: " LambdaAuthorizer " ,
160
160
functionName: " lambdaAuthorizer " ,
161
161
identitySource: [ " $request.header.SEC-X-API-KEY " ,
162
162
" $request.header.User-Agent " ] )
163
163
] )
164
- ) ,
165
- iam: iam
166
- )
167
- let custom = try YAMLContent ( with: [ " tableName " : " \( dynamoDBTableNamePrefix) -table-${sls:stage} " ,
168
- " keyName " : dynamoDBKey] )
169
-
170
- let endpoints = [
171
- Endpoint ( handler: " create " , method: . post, path: httpAPIPath) ,
172
- Endpoint ( handler: " read " , method: . get, path: keyedPath) ,
173
- Endpoint ( handler: " update " , method: . put, path: httpAPIPath) ,
174
- Endpoint ( handler: " delete " , method: . delete, path: keyedPath) ,
175
- Endpoint ( handler: " list " , method: . get, path: httpAPIPath)
176
- ]
177
- var functions : [ String : Function ] = [ : ]
178
- for endpoint in endpoints {
179
- let function = try Function . httpApiLambda (
180
- handler: " \( endpoint. handler) " ,
181
- description: nil ,
182
- memorySize: memorySize,
183
- runtime: nil ,
184
- package : nil ,
185
- event: . init( path: endpoint. path, method: endpoint. method)
186
- )
187
- functions [ " \( endpoint. handler) \( executable) " ] = function
188
- }
189
-
190
- let resource = Resource . dynamoDBResource ( tableName: " ${self:custom.tableName} " , key: " ${self:custom.keyName} " )
191
- let resources = Resources . resources ( with: [ dynamoResourceName: resource] )
192
-
193
- return ServerlessConfig (
194
- service: service,
195
- provider: provider,
196
- package : . init( patterns: nil , individually: nil , artifact: artifact) ,
197
- custom: custom,
198
- layers: nil ,
199
- functions: functions,
200
- resources: try YAMLContent ( with: resources)
164
+ ) ,
165
+ iam: iam
166
+ )
167
+ let custom = try YAMLContent ( with: [ " tableName " : " \( dynamoDBTableNamePrefix) -table-${sls:stage} " ,
168
+ " keyName " : dynamoDBKey] )
169
+
170
+ let endpoints = [
171
+ Endpoint ( handler: " create " , method: . post, path: httpAPIPath) ,
172
+ Endpoint ( handler: " read " , method: . get, path: keyedPath) ,
173
+ Endpoint ( handler: " update " , method: . put, path: httpAPIPath) ,
174
+ Endpoint ( handler: " delete " , method: . delete, path: keyedPath) ,
175
+ Endpoint ( handler: " list " , method: . get, path: httpAPIPath)
176
+ ]
177
+ var functions : [ String : Function ] = [ : ]
178
+ for endpoint in endpoints {
179
+ let function = try Function . httpApiLambda (
180
+ handler: " \( endpoint. handler) " ,
181
+ description: nil ,
182
+ memorySize: memorySize,
183
+ runtime: nil ,
184
+ package : nil ,
185
+ event: . init( path: endpoint. path, method: endpoint. method)
201
186
)
187
+ functions [ " \( endpoint. handler) \( executable) " ] = function
202
188
}
189
+
190
+ let resource = Resource . dynamoDBResource ( tableName: " ${self:custom.tableName} " , key: " ${self:custom.keyName} " )
191
+ let resources = Resources . resources ( with: [ dynamoResourceName: resource] )
192
+
193
+ return ServerlessConfig (
194
+ service: service,
195
+ provider: provider,
196
+ package : . init( patterns: nil , individually: nil , artifact: artifact) ,
197
+ custom: custom,
198
+ layers: nil ,
199
+ functions: functions,
200
+ resources: try YAMLContent ( with: resources)
201
+ )
202
+ }
203
+
204
+ static func webhookLambdaAPI(
205
+ service: String ,
206
+ region: Region ,
207
+ runtime: Runtime ,
208
+ architecture: Architecture ,
209
+ memorySize: Int ,
210
+ lambdasParams: [ HttpAPILambdaParams ]
211
+ ) throws -> ServerlessConfig {
212
+ let iam = Iam (
213
+ role: Role (
214
+ statements: [ . allowLogAccess( resource: try YAMLContent ( with: " * " ) ) ]
215
+ )
216
+ )
217
+ let provider = Provider (
218
+ name: . aws,
219
+ region: region,
220
+ runtime: runtime,
221
+ environment: nil ,
222
+ architecture: architecture,
223
+ httpAPI: . init( payload: " 2.0 " , cors: false ) ,
224
+ iam: iam
225
+ )
226
+ let package = Package ( patterns: nil , individually: true )
227
+ let functions = try lambdasParams. buildFunctions ( memorySize: memorySize)
228
+ return ServerlessConfig (
229
+ service: service,
230
+ provider: provider,
231
+ package : package ,
232
+ custom: nil ,
233
+ layers: nil ,
234
+ functions: functions,
235
+ resources: nil
236
+ )
237
+ }
203
238
}
0 commit comments