Skip to content
177 changes: 87 additions & 90 deletions Examples/SAM/Deploy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,111 +2,108 @@ import AWSLambdaDeploymentDescriptor

// example of a shared resource
let sharedQueue = Queue(
logicalName: "SharedQueue",
physicalName: "swift-lambda-shared-queue")
logicalName: "SharedQueue",
physicalName: "swift-lambda-shared-queue"
)

// example of common environment variables
let sharedEnvironmentVariables = ["LOG_LEVEL": "debug"]

let validEfsArn =
"arn:aws:elasticfilesystem:eu-central-1:012345678901:access-point/fsap-abcdef01234567890"
"arn:aws:elasticfilesystem:eu-central-1:012345678901:access-point/fsap-abcdef01234567890"

// the deployment descriptor
DeploymentDescriptor {

// an optional description
"Description of this deployment descriptor"

// Create a lambda function exposed through a REST API
Function(name: "HttpApiLambda") {

// an optional description
"Description of this function"
"Description of this deployment descriptor"

// Create a lambda function exposed through a REST API
Function(name: "HttpApiLambda") {
// an optional description
"Description of this function"

EventSources {
// example of a catch all api
HttpApi()

// example of an API for a specific HTTP verb and path
// HttpApi(method: .GET, path: "/test")
}

EnvironmentVariables {
[
"NAME1": "VALUE1",
"NAME2": "VALUE2",
]

// shared environment variables declared upfront
sharedEnvironmentVariables
}
}

EventSources {
// Example Function modifiers:

// .autoPublishAlias()
// .ephemeralStorage(2048)
// .eventInvoke(onSuccess: "arn:aws:sqs:eu-central-1:012345678901:lambda-test",
// onFailure: "arn:aws:lambda:eu-central-1:012345678901:lambda-test",
// maximumEventAgeInSeconds: 600,
// maximumRetryAttempts: 3)
// .fileSystem(validEfsArn, mountPoint: "/mnt/path1")
// .fileSystem(validEfsArn, mountPoint: "/mnt/path2")

// Create a Lambda function exposed through an URL
// you can invoke it with a signed request, for example
// curl --aws-sigv4 "aws:amz:eu-central-1:lambda" \
// --user $AWS_ACCESS_KEY_ID:$AWS_SECRET_ACCESS_KEY \
// -H 'content-type: application/json' \
// -d '{ "example": "test" }' \
// "$FUNCTION_URL?param1=value1&param2=value2"
Function(name: "UrlLambda") {
"A Lambda function that is directly exposed as an URL, with IAM authentication"
}
.urlConfig(authType: .iam)

// example of a catch all api
HttpApi()
// Create a Lambda function triggered by messages on SQS
Function(name: "SQSLambda", architecture: .arm64) {
EventSources {
// this will reference an existing queue by its Arn
// Sqs("arn:aws:sqs:eu-central-1:012345678901:swift-lambda-shared-queue")

// example of an API for a specific HTTP verb and path
// HttpApi(method: .GET, path: "/test")
// // this will create a new queue resource
Sqs("swift-lambda-queue-name")

}
// // this will create a new queue resource, with control over physical queue name
// Sqs()
// .queue(logicalName: "LambdaQueueResource", physicalName: "swift-lambda-queue-resource")

EnvironmentVariables {
[
"NAME1": "VALUE1",
"NAME2": "VALUE2",
]
// // this references a shared queue resource created at the top of this deployment descriptor
// // the queue resource will be created automatically, you do not need to add `sharedQueue` as a resource
// Sqs(sharedQueue)
}

// shared environment variables declared upfront
sharedEnvironmentVariables
}
}

// Example Function modifiers:

// .autoPublishAlias()
// .ephemeralStorage(2048)
// .eventInvoke(onSuccess: "arn:aws:sqs:eu-central-1:012345678901:lambda-test",
// onFailure: "arn:aws:lambda:eu-central-1:012345678901:lambda-test",
// maximumEventAgeInSeconds: 600,
// maximumRetryAttempts: 3)
// .fileSystem(validEfsArn, mountPoint: "/mnt/path1")
// .fileSystem(validEfsArn, mountPoint: "/mnt/path2")

// Create a Lambda function exposed through an URL
// you can invoke it with a signed request, for example
// curl --aws-sigv4 "aws:amz:eu-central-1:lambda" \
// --user $AWS_ACCESS_KEY_ID:$AWS_SECRET_ACCESS_KEY \
// -H 'content-type: application/json' \
// -d '{ "example": "test" }' \
// "$FUNCTION_URL?param1=value1&param2=value2"
Function(name: "UrlLambda") {
"A Lambda function that is directly exposed as an URL, with IAM authentication"
}
.urlConfig(authType: .iam)

// Create a Lambda function triggered by messages on SQS
Function(name: "SQSLambda", architecture: .arm64) {

EventSources {

// this will reference an existing queue by its Arn
// Sqs("arn:aws:sqs:eu-central-1:012345678901:swift-lambda-shared-queue")

// // this will create a new queue resource
Sqs("swift-lambda-queue-name")

// // this will create a new queue resource, with control over physical queue name
// Sqs()
// .queue(logicalName: "LambdaQueueResource", physicalName: "swift-lambda-queue-resource")

// // this references a shared queue resource created at the top of this deployment descriptor
// // the queue resource will be created automatically, you do not need to add `sharedQueue` as a resource
// Sqs(sharedQueue)
EnvironmentVariables {
sharedEnvironmentVariables
}
}

EnvironmentVariables {
sharedEnvironmentVariables
}
}

//
// Additional resources
//
// Create a SQS queue
Queue(
logicalName: "TopLevelQueueResource",
physicalName: "swift-lambda-top-level-queue")

// Create a DynamoDB table
Table(
logicalName: "SwiftLambdaTable",
physicalName: "swift-lambda-table",
primaryKeyName: "id",
primaryKeyType: "String")

// example modifiers
// .provisionedThroughput(readCapacityUnits: 10, writeCapacityUnits: 99)
//
// Additional resources
//
// Create a SQS queue
Queue(
logicalName: "TopLevelQueueResource",
physicalName: "swift-lambda-top-level-queue"
)

// Create a DynamoDB table
Table(
logicalName: "SwiftLambdaTable",
physicalName: "swift-lambda-table",
primaryKeyName: "id",
primaryKeyType: "String"
)

// example modifiers
// .provisionedThroughput(readCapacityUnits: 10, writeCapacityUnits: 99)
}
4 changes: 1 addition & 3 deletions Examples/SAM/HttpApiLambda/Lambda.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@ struct HttpApiLambda: LambdaHandler {
init() {}
init(context: LambdaInitializationContext) async throws {
context.logger.info(
"Log Level env var : \(ProcessInfo.processInfo.environment["LOG_LEVEL"] ?? "info" )")
"Log Level env var : \(ProcessInfo.processInfo.environment["LOG_LEVEL"] ?? "info")")
}

// the return value must be either APIGatewayV2Response or any Encodable struct
func handle(_ event: APIGatewayV2Request, context: AWSLambdaRuntimeCore.LambdaContext) async throws -> APIGatewayV2Response {

var header = HTTPHeaders()
do {
context.logger.debug("HTTP API Message received")
Expand All @@ -46,7 +45,6 @@ struct HttpApiLambda: LambdaHandler {
// when the input event is malformed, this function is not even called
header["content-type"] = "text/plain"
return APIGatewayV2Response(statusCode: .badRequest, headers: header, body: "\(error.localizedDescription)")

}
}
}
106 changes: 53 additions & 53 deletions Examples/SAM/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,64 +18,64 @@ import class Foundation.ProcessInfo // needed for CI to test the local version o
import PackageDescription

let package = Package(
name: "swift-aws-lambda-runtime-example",
platforms: [
.macOS(.v12)
],
products: [
.executable(name: "HttpApiLambda", targets: ["HttpApiLambda"]),
.executable(name: "SQSLambda", targets: ["SQSLambda"]),
.executable(name: "UrlLambda", targets: ["UrlLambda"])
],
dependencies: [
.package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", branch: "main"),
.package(url: "https://github.com/swift-server/swift-aws-lambda-events.git", branch: "main")
],
targets: [
.executableTarget(
name: "HttpApiLambda",
dependencies: [
.product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"),
.product(name: "AWSLambdaEvents", package: "swift-aws-lambda-events")
],
path: "./HttpApiLambda"
),
.executableTarget(
name: "UrlLambda",
dependencies: [
.product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"),
.product(name: "AWSLambdaEvents", package: "swift-aws-lambda-events")
],
path: "./UrlLambda"
),
.executableTarget(
name: "SQSLambda",
dependencies: [
.product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"),
.product(name: "AWSLambdaEvents", package: "swift-aws-lambda-events")
],
path: "./SQSLambda"
),
.testTarget(
name: "LambdaTests",
dependencies: [
"HttpApiLambda", "SQSLambda",
.product(name: "AWSLambdaTesting", package: "swift-aws-lambda-runtime"),
],
// testing data
resources: [
.process("data/apiv2.json"),
.process("data/sqs.json")
]
)
]
name: "swift-aws-lambda-runtime-example",
platforms: [
.macOS(.v12),
],
products: [
.executable(name: "HttpApiLambda", targets: ["HttpApiLambda"]),
.executable(name: "SQSLambda", targets: ["SQSLambda"]),
.executable(name: "UrlLambda", targets: ["UrlLambda"]),
],
dependencies: [
.package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", branch: "main"),
.package(url: "https://github.com/swift-server/swift-aws-lambda-events.git", branch: "main"),
],
targets: [
.executableTarget(
name: "HttpApiLambda",
dependencies: [
.product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"),
.product(name: "AWSLambdaEvents", package: "swift-aws-lambda-events"),
],
path: "./HttpApiLambda"
),
.executableTarget(
name: "UrlLambda",
dependencies: [
.product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"),
.product(name: "AWSLambdaEvents", package: "swift-aws-lambda-events"),
],
path: "./UrlLambda"
),
.executableTarget(
name: "SQSLambda",
dependencies: [
.product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"),
.product(name: "AWSLambdaEvents", package: "swift-aws-lambda-events"),
],
path: "./SQSLambda"
),
.testTarget(
name: "LambdaTests",
dependencies: [
"HttpApiLambda", "SQSLambda",
.product(name: "AWSLambdaTesting", package: "swift-aws-lambda-runtime"),
],
// testing data
resources: [
.process("data/apiv2.json"),
.process("data/sqs.json"),
]
),
]
)

// for CI to test the local version of the library
if ProcessInfo.processInfo.environment["LAMBDA_USE_LOCAL_DEPS"] != nil {
package.dependencies = [
.package(name: "swift-aws-lambda-runtime", path: "../.."),
// .package(url: "../../../swift-aws-lambda-runtime", branch: "sebsto/use_local_deps"), // to have the LAMBDA_USE_LOCAL_DEPS env var on plugin archive (temp until https://github.com/swift-server/swift-aws-lambda-runtime/pull/325 is merged)
.package(url: "https://github.com/swift-server/swift-aws-lambda-events.git", branch: "main")
.package(url: "https://github.com/swift-server/swift-aws-lambda-events.git", branch: "main"),
]
}
}
5 changes: 2 additions & 3 deletions Examples/SAM/SQSLambda/Lambda.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,11 @@ struct SQSLambda: LambdaHandler {
init() {}
init(context: LambdaInitializationContext) async throws {
context.logger.info(
"Log Level env var : \(ProcessInfo.processInfo.environment["LOG_LEVEL"] ?? "info" )")
"Log Level env var : \(ProcessInfo.processInfo.environment["LOG_LEVEL"] ?? "info")")
}

func handle(_ event: Event, context: AWSLambdaRuntimeCore.LambdaContext) async throws -> Output {

context.logger.info("Log Level env var : \(ProcessInfo.processInfo.environment["LOG_LEVEL"] ?? "not defined" )" )
context.logger.info("Log Level env var : \(ProcessInfo.processInfo.environment["LOG_LEVEL"] ?? "not defined")")
context.logger.debug("SQS Message received, with \(event.records.count) record")

for msg in event.records {
Expand Down
Loading