Skip to content

Commit

Permalink
Add environment parameter to httpApiLambda
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrea-Scuderi committed Nov 28, 2023
1 parent afb5b2f commit 291c739
Show file tree
Hide file tree
Showing 4 changed files with 320 additions and 60 deletions.
21 changes: 21 additions & 0 deletions Sources/SLSAdapter/Function+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,25 @@ public extension Function {
events: [.init(httpAPI: event)]
)
}

static func httpApiLambda(
handler: String,
description: String?,
memorySize: Int?,
environment: YAMLContent?,
runtime: Runtime?,
package: Package?,
event: EventHTTPAPI
) throws -> Function {
Function(
handler: handler,
runtime: runtime,
memorySize: memorySize,
environment: environment,
description: description ?? "[${sls:stage}] \(event.method) \(event.path)",
package: package,
layers: nil,
events: [.init(httpAPI: event)]
)
}
}
59 changes: 59 additions & 0 deletions Tests/SLSAdapterTests/Fixtures/serverless_webhook.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
service: swift-webhook
frameworkVersion: '3'
configValidationMode: warn
useDotenv: false
provider:
name: aws
region: us-east-1
disableRollback: false
runtime: provided.al2
httpApi:
payload: '2.0'
cors: false
architecture: arm64
versionFunctions: true
iam:
role:
statements:
- Effect: Allow
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Resource: '*'

package:
individually: true
functions:
postWebHook:
handler: post-webhook
memorySize: 256
description: '[${sls:stage}] post /webhook'
package:
artifact: build/WebHook/WebHook.zip
events:
- httpApi:
path: /webhook
method: post
getWebHook:
handler: get-webhook
memorySize: 256
description: '[${sls:stage}] get /webhook'
package:
artifact: build/WebHook/WebHook.zip
events:
- httpApi:
path: /webhook
method: get
githubWebHook:
handler: github-webhook
memorySize: 256
description: '[${sls:stage}] get /github-webhook'
package:
artifact: build/GitHubWebHook/GitHubWebHook.zip
environment:
WEBHOOK_SECRET: '${ssm:/dev/swift-webhook/webhook_secret}'
events:
- httpApi:
path: /github-webhook
method: post
180 changes: 120 additions & 60 deletions Tests/SLSAdapterTests/ServerlessConfig+Exensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -129,75 +129,135 @@ extension ServerlessConfig {
executable: String,
artifact: String
) throws -> ServerlessConfig {
let keyedPath = "\(httpAPIPath)/{\(dynamoDBKey)}"
let dynamoResourceName = "\(executable)Table"

let environmentTableName = "DYNAMO_DB_TABLE_NAME"
let environmentKeyName = "DYNAMO_DB_KEY"

let iam = Iam(
role: Role(
statements: [.allowLogAccess(resource: try YAMLContent(with: "*")),
.allowDynamoDBReadWrite(resource: try YAMLContent(with: [["Fn::GetAtt": [dynamoResourceName, "Arn"]]]))]
)
let keyedPath = "\(httpAPIPath)/{\(dynamoDBKey)}"
let dynamoResourceName = "\(executable)Table"

let environmentTableName = "DYNAMO_DB_TABLE_NAME"
let environmentKeyName = "DYNAMO_DB_KEY"
let dynamoResource = try YAMLContent(with: [["Fn::GetAtt": [dynamoResourceName, "Arn"]]])
let iam = Iam(
role: Role(
statements: [.allowLogAccess(resource: try YAMLContent(with: "*")),
.allowDynamoDBReadWrite(resource: dynamoResource)]
)
let environment = try YAMLContent(with: [environmentTableName: "${self:custom.tableName}",
environmentKeyName: "${self:custom.keyName}"])
let provider = Provider(
name: .aws,
region: region,
runtime: runtime,
environment: environment,
architecture: architecture,
httpAPI: .init(
payload: "2.0",
cors: true,
authorizers:
)
let environment = try YAMLContent(with: [environmentTableName: "${self:custom.tableName}",
environmentKeyName: "${self:custom.keyName}"])
let provider = Provider(
name: .aws,
region: region,
runtime: runtime,
environment: environment,
architecture: architecture,
httpAPI: .init(
payload: "2.0",
cors: true,
authorizers:
.dictionary([
"JWTAuthorizer": .buildJWTAuthorizer(issuerUrl: "https://appleid.apple.com",
audience: ["com.mydomain.myhost"]),
audience: ["com.mydomain.myhost"]),
"customAuthorizer": .buildCustomAuthorizer(name: "LambdaAuthorizer",
functionName: "lambdaAuthorizer",
identitySource: ["$request.header.SEC-X-API-KEY",
"$request.header.User-Agent"])
])
),
iam: iam
),
iam: iam
)
let custom = try YAMLContent(with: ["tableName": "\(dynamoDBTableNamePrefix)-table-${sls:stage}",
"keyName": dynamoDBKey])

let endpoints = [
Endpoint(handler: "create", method: .post, path: httpAPIPath),
Endpoint(handler: "read", method: .get, path: keyedPath),
Endpoint(handler: "update", method: .put, path: httpAPIPath),
Endpoint(handler: "delete", method: .delete, path: keyedPath),
Endpoint(handler: "list", method: .get, path: httpAPIPath)
]
var functions: [String: Function] = [:]
for endpoint in endpoints {
let function = try Function.httpApiLambda(
handler: "\(endpoint.handler)",
description: nil,
memorySize: memorySize,
runtime: nil,
package: nil,
event: .init(path: endpoint.path, method: endpoint.method)
)
let custom = try YAMLContent(with: ["tableName": "\(dynamoDBTableNamePrefix)-table-${sls:stage}",
"keyName": dynamoDBKey])

let endpoints = [
Endpoint(handler: "create", method: .post, path: httpAPIPath),
Endpoint(handler: "read", method: .get, path: keyedPath),
Endpoint(handler: "update", method: .put, path: httpAPIPath),
Endpoint(handler: "delete", method: .delete, path: keyedPath),
Endpoint(handler: "list", method: .get, path: httpAPIPath)
]
var functions: [String: Function] = [:]
for endpoint in endpoints {
let function = try Function.httpApiLambda(
handler: "\(endpoint.handler)",
description: nil,
memorySize: memorySize,
runtime: nil,
package: nil,
event: .init(path: endpoint.path, method: endpoint.method)
)
functions["\(endpoint.handler)\(executable)"] = function
}

let resource = Resource.dynamoDBResource(tableName: "${self:custom.tableName}", key: "${self:custom.keyName}")
let resources = Resources.resources(with: [dynamoResourceName: resource])

return ServerlessConfig(
service: service,
provider: provider,
package: .init(patterns: nil, individually: nil, artifact: artifact),
custom: custom,
layers: nil,
functions: functions,
resources: try YAMLContent(with: resources)
functions["\(endpoint.handler)\(executable)"] = function
}

let resource = Resource.dynamoDBResource(tableName: "${self:custom.tableName}", key: "${self:custom.keyName}")
let resources = Resources.resources(with: [dynamoResourceName: resource])

return ServerlessConfig(
service: service,
provider: provider,
package: .init(patterns: nil, individually: nil, artifact: artifact),
custom: custom,
layers: nil,
functions: functions,
resources: try YAMLContent(with: resources)
)
}

static func webhookLambdaAPI(
service: String,
httpAPIPath: String,
region: Region,
runtime: Runtime,
architecture: Architecture,
memorySize: Int,
executable: String,
artifact: String
) throws -> ServerlessConfig {
let iam = Iam(
role: Role(
statements: [.allowLogAccess(resource: try YAMLContent(with: "*"))]
)
)
let environment = try YAMLContent(with: ["WEBHOOK_SECRET": "${ssm:/dev/swift-webhook/webhook_secret}"])
let provider = Provider(
name: .aws,
region: region,
runtime: runtime,
environment: environment,
architecture: architecture,
httpAPI: .init(
payload: "2.0",
cors: false
),
iam: iam
)
let endpoints = [
Endpoint(handler: "postWebHook", method: .post, path: "/webhook"),
Endpoint(handler: "getWebHook", method: .get, path: "/webhook"),
Endpoint(handler: "githubWebHook", method: .post, path: "/github-webhook")
]
var functions: [String: Function] = [:]
for endpoint in endpoints {
let function = try Function.httpApiLambda(
handler: "\(endpoint.handler)",
description: nil,
memorySize: memorySize,
environment: environment,
runtime: nil,
package: .init(patterns: nil,
individually: nil,
artifact: "build/GitHubWebHook/GitHubWebHook.zip"),
event: .init(path: endpoint.path, method: endpoint.method)
)
functions["\(endpoint.handler)\(executable)"] = function
}

return ServerlessConfig(
service: service,
provider: provider,
package: .init(patterns: nil, individually: nil, artifact: artifact),
custom: nil,
layers: nil,
functions: functions,
resources: nil
)
}
}
Loading

0 comments on commit 291c739

Please sign in to comment.