Skip to content

Commit 81c0693

Browse files
authored
Merge branch 'main' into ff-dont-use-NIOFoundationCompat
2 parents 76ff30e + 600e48c commit 81c0693

File tree

74 files changed

+631
-3195
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+631
-3195
lines changed

.github/workflows/pull_request.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ jobs:
1111
with:
1212
license_header_check_project_name: "SwiftAWSLambdaRuntime"
1313
shell_check_enabled: false
14-
api_breakage_check_container_image: "swiftlang/swift:nightly-6.0-jammy"
15-
docs_check_container_image: "swiftlang/swift:nightly-6.0-jammy"
14+
api_breakage_check_container_image: "swift:6.0-noble"
15+
docs_check_container_image: "swift:6.0-noble"
1616

1717
unit-tests:
1818
name: Unit tests

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ xcuserdata
99
Package.resolved
1010
.serverless
1111
.vscode
12+
Makefile

.licenseignore

+2
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,5 @@ Package.resolved
3030
**/ResourcePackaging/hello.txt
3131
.mailmap
3232
.swiftformat
33+
*.yaml
34+
*.yml

Examples/APIGateway/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
samconfig.toml
2+
Makefile

Examples/APIGateway/Package.swift

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// swift-tools-version:6.0
2+
3+
import PackageDescription
4+
5+
// needed for CI to test the local version of the library
6+
import class Foundation.ProcessInfo
7+
import struct Foundation.URL
8+
9+
#if os(macOS)
10+
let platforms: [PackageDescription.SupportedPlatform]? = [.macOS(.v15)]
11+
#else
12+
let platforms: [PackageDescription.SupportedPlatform]? = nil
13+
#endif
14+
15+
let package = Package(
16+
name: "swift-aws-lambda-runtime-example",
17+
platforms: platforms,
18+
products: [
19+
.executable(name: "APIGAtewayLambda", targets: ["APIGAtewayLambda"])
20+
],
21+
dependencies: [
22+
// dependency on swift-aws-lambda-runtime is added dynamically below
23+
// .package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", branch: "main")
24+
25+
.package(url: "https://github.com/swift-server/swift-aws-lambda-events.git", branch: "main")
26+
],
27+
targets: [
28+
.executableTarget(
29+
name: "APIGAtewayLambda",
30+
dependencies: [
31+
.product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"),
32+
.product(name: "AWSLambdaEvents", package: "swift-aws-lambda-events"),
33+
],
34+
path: "."
35+
)
36+
]
37+
)
38+
39+
if let localDepsPath = ProcessInfo.processInfo.environment["LAMBDA_USE_LOCAL_DEPS"],
40+
localDepsPath != "",
41+
let v = try? URL(fileURLWithPath: localDepsPath).resourceValues(forKeys: [.isDirectoryKey]),
42+
let _ = v.isDirectory
43+
{
44+
print("[INFO] Compiling against swift-aws-lambda-runtime located at \(localDepsPath)")
45+
package.dependencies += [
46+
.package(name: "swift-aws-lambda-runtime", path: localDepsPath)
47+
]
48+
49+
} else {
50+
print("[INFO] LAMBDA_USE_LOCAL_DEPS is not pointing to your local swift-aws-lambda-runtime code")
51+
print("[INFO] This project will compile against the main branch of the Lambda Runtime on GitHub")
52+
package.dependencies += [
53+
.package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", branch: "main")
54+
]
55+
}

Examples/APIGateway/README.md

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
# API Gateway
2+
3+
This is a simple example of an AWS Lambda function invoked through an Amazon API Gateway.
4+
5+
## Code
6+
7+
The Lambda function takes all HTTP headers it receives as input and returns them as output.
8+
9+
The code creates a `LambdaRuntime` struct. In it's simplest form, the initializer takes a function as argument. The function is the handler that will be invoked when the API Gateway receives an HTTP request.
10+
11+
The handler is `(event: APIGatewayV2Request, context: LambdaContext) -> APIGatewayV2Response`. The function takes two arguments:
12+
- the event argument is a `APIGatewayV2Request`. It is the parameter passed by the API Gateway. It contains all data passed in the HTTP request and some meta data.
13+
- the context argument is a `Lambda Context`. It is a description of the runtime context.
14+
15+
The function must return a `APIGatewayV2Response`.
16+
17+
`APIGatewayV2Request` and `APIGatewayV2Response` are defined in the [Swift AWS Lambda Events](https://github.com/swift-server/swift-aws-lambda-events) library.
18+
19+
## Build & Package
20+
21+
To build the package, type the following commands.
22+
23+
```bash
24+
swift build
25+
swift package archive --allow-network-access docker
26+
```
27+
28+
If there is no error, there is a ZIP file ready to deploy.
29+
The ZIP file is located at `.build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager/MyLambda/MyLambda.zip`
30+
31+
## Deploy
32+
33+
The deployment must include the Lambda function and the API Gateway. We use the [Serverless Application Model (SAM)](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/what-is-sam.html) to deploy the infrastructure.
34+
35+
**Prerequisites** : Install the [SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/install-sam-cli.html)
36+
37+
The example directory contains a file named `template.yaml` that describes the deployment.
38+
39+
To actually deploy your Lambda function and create the infrastructure, type the following `sam` command.
40+
41+
```bash
42+
sam deploy \
43+
----resolve-s3 \
44+
--template-file template.yaml \
45+
--stack-name MyLambda \
46+
--capabilities CAPABILITY_IAM
47+
```
48+
49+
At the end of the deployment, the script lists the API Gateway endpoint.
50+
The output is similar to this one.
51+
52+
```
53+
-----------------------------------------------------------------------------------------------------------------------------
54+
Outputs
55+
-----------------------------------------------------------------------------------------------------------------------------
56+
Key APIGAtewayEndpoint
57+
Description API Gateway endpoint UR"
58+
Value https://a5q74es3k2.execute-api.us-east-1.amazonaws.com
59+
-----------------------------------------------------------------------------------------------------------------------------
60+
```
61+
62+
## Invoke your Lambda function
63+
64+
To invoke the Lambda function, use this `curl` command line.
65+
66+
```bash
67+
curl https://a5q74es3k2.execute-api.us-east-1.amazonaws.com
68+
```
69+
70+
Be sure to replace the URL with the API Gateway endpoint returned in the previous step.
71+
72+
This should print a JSON similar to
73+
74+
```bash
75+
{"version":"2.0","rawPath":"\/","isBase64Encoded":false,"rawQueryString":"","headers":{"user-agent":"curl\/8.7.1","accept":"*\/*","host":"a5q74es3k2.execute-api.us-east-1.amazonaws.com","content-length":"0","x-amzn-trace-id":"Root=1-66fb0388-691f744d4bd3c99c7436a78d","x-forwarded-port":"443","x-forwarded-for":"81.0.0.43","x-forwarded-proto":"https"},"requestContext":{"requestId":"e719cgNpoAMEcwA=","http":{"sourceIp":"81.0.0.43","path":"\/","protocol":"HTTP\/1.1","userAgent":"curl\/8.7.1","method":"GET"},"stage":"$default","apiId":"a5q74es3k2","time":"30\/Sep\/2024:20:01:12 +0000","timeEpoch":1727726472922,"domainPrefix":"a5q74es3k2","domainName":"a5q74es3k2.execute-api.us-east-1.amazonaws.com","accountId":"012345678901"}
76+
```
77+
78+
If you have `jq` installed, you can use it to pretty print the output.
79+
80+
```bash
81+
curl -s https://a5q74es3k2.execute-api.us-east-1.amazonaws.com | jq
82+
{
83+
"version": "2.0",
84+
"rawPath": "/",
85+
"requestContext": {
86+
"domainPrefix": "a5q74es3k2",
87+
"stage": "$default",
88+
"timeEpoch": 1727726558220,
89+
"http": {
90+
"protocol": "HTTP/1.1",
91+
"method": "GET",
92+
"userAgent": "curl/8.7.1",
93+
"path": "/",
94+
"sourceIp": "81.0.0.43"
95+
},
96+
"apiId": "a5q74es3k2",
97+
"accountId": "012345678901",
98+
"requestId": "e72KxgsRoAMEMSA=",
99+
"domainName": "a5q74es3k2.execute-api.us-east-1.amazonaws.com",
100+
"time": "30/Sep/2024:20:02:38 +0000"
101+
},
102+
"rawQueryString": "",
103+
"routeKey": "$default",
104+
"headers": {
105+
"x-forwarded-for": "81.0.0.43",
106+
"user-agent": "curl/8.7.1",
107+
"host": "a5q74es3k2.execute-api.us-east-1.amazonaws.com",
108+
"accept": "*/*",
109+
"x-amzn-trace-id": "Root=1-66fb03de-07533930192eaf5f540db0cb",
110+
"content-length": "0",
111+
"x-forwarded-proto": "https",
112+
"x-forwarded-port": "443"
113+
},
114+
"isBase64Encoded": false
115+
}
116+
```
117+
118+
## Undeploy
119+
120+
When done testing, you can delete the infrastructure with this command.
121+
122+
```bash
123+
sam delete
124+
```
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the SwiftAWSLambdaRuntime open source project
4+
//
5+
// Copyright (c) 2024 Apple Inc. and the SwiftAWSLambdaRuntime project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
import AWSLambdaEvents
16+
import AWSLambdaRuntime
17+
18+
#if canImport(FoundationEssentials)
19+
import FoundationEssentials
20+
#else
21+
import Foundation
22+
#endif
23+
24+
let encoder = JSONEncoder()
25+
let runtime = LambdaRuntime {
26+
(event: APIGatewayV2Request, context: LambdaContext) -> APIGatewayV2Response in
27+
28+
var header = HTTPHeaders()
29+
context.logger.debug("HTTP API Message received")
30+
31+
header["content-type"] = "application/json"
32+
33+
// echo the request in the response
34+
let data = try encoder.encode(event)
35+
let response = String(decoding: data, as: Unicode.UTF8.self)
36+
37+
return APIGatewayV2Response(statusCode: .ok, headers: header, body: response)
38+
}
39+
40+
try await runtime.run()

Examples/APIGateway/template.yaml

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
AWSTemplateFormatVersion: '2010-09-09'
2+
Transform: AWS::Serverless-2016-10-31
3+
Description: SAM Template for QuoteService
4+
5+
Resources:
6+
# Lambda function
7+
APIGAtewayLambda:
8+
Type: AWS::Serverless::Function
9+
Properties:
10+
CodeUri: .build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager/APIGAtewayLambda/APIGAtewayLambda.zip
11+
Timeout: 60
12+
Handler: swift.bootstrap
13+
Runtime: provided.al2
14+
MemorySize: 512
15+
Architectures:
16+
- arm64
17+
Environment:
18+
Variables:
19+
# by default, AWS Lambda runtime produces no log
20+
# use `LOG_LEVEL: debug` for for lifecycle and event handling information
21+
# use `LOG_LEVEL: trace` for detailed input event information
22+
LOG_LEVEL: trace
23+
Events:
24+
HttpApiEvent:
25+
Type: HttpApi
26+
27+
Outputs:
28+
# print API Gateway endpoint
29+
APIGAtewayEndpoint:
30+
Description: API Gateway endpoint UR"
31+
Value: !Sub "https://${ServerlessHttpApi}.execute-api.${AWS::Region}.amazonaws.com"

Examples/Benchmark/BenchmarkHandler.swift

-32
This file was deleted.

Examples/Benchmark/Package.swift

-34
This file was deleted.

Examples/Deployment/.dockerignore

-1
This file was deleted.

Examples/Deployment/Dockerfile

-3
This file was deleted.

Examples/Deployment/Package.swift

-43
This file was deleted.

0 commit comments

Comments
 (0)