Skip to content

Commit b4673d2

Browse files
authored
Add AWS SDK and Soto examples (#396)
* add aws sdk example * add soto example * use amazonlinux docker image instead of ubuntu * dynamically add runtime dependency based on env var * remove import of Foundation.ProcessInfo * workaround actions/checkout#1487
1 parent bb9ddfb commit b4673d2

File tree

15 files changed

+579
-47
lines changed

15 files changed

+579
-47
lines changed

.github/workflows/examples_matrix.yml

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ on:
1818
matrix_linux_swift_container_image:
1919
type: string
2020
description: "Container image for the matrix job. Defaults to matching latest Swift Ubuntu image."
21-
default: "swift:latest"
21+
default: "swift:amazonlinux2"
2222

2323
## We are cancelling previously triggered workflow runs
2424
concurrency:
@@ -33,28 +33,49 @@ jobs:
3333
fail-fast: false
3434
matrix:
3535
# This should be passed as an argument in input. Can we pass arrays as argument ?
36-
examples : [ "HelloWorld", "APIGateway" ]
36+
examples : [ "HelloWorld", "APIGateway", "S3_AWSSDK", "S3_Soto" ]
3737
# examples: ${{ inputs.examples }}
3838

3939
# We are using only one Swift version
4040
swift:
4141
- image: ${{ inputs.matrix_linux_swift_container_image }}
42-
swift_version: "6.0.1-noble"
42+
swift_version: "6.0.1-amazonlinux2"
4343
container:
4444
image: ${{ matrix.swift.image }}
4545
steps:
46-
- name: Checkout repository
47-
uses: actions/checkout@v4
48-
with:
49-
persist-credentials: false
46+
47+
# GitHub checkout action has a dep on NodeJS 20 which is not running on Amazonlinux2
48+
# workaround is to manually checkout the repository
49+
# https://github.com/actions/checkout/issues/1487
50+
- name: Manually Clone repository and checkout PR
51+
env:
52+
PR_NUMBER: ${{ github.event.pull_request.number }}
53+
run: |
54+
# Clone the repository
55+
git clone https://github.com/${{ github.repository }}
56+
cd ${{ github.event.repository.name }}
57+
58+
# Fetch the pull request
59+
git fetch origin +refs/pull/$PR_NUMBER/merge:
60+
61+
# Checkout the pull request
62+
git checkout -qf FETCH_HEAD
63+
64+
# - name: Checkout repository
65+
# uses: actions/checkout@v4
66+
# with:
67+
# persist-credentials: false
68+
5069
- name: Mark the workspace as safe
51-
# https://github.com/actions/checkout/issues/766
70+
working-directory: ${{ github.event.repository.name }} # until we can use action/checkout@v4
71+
# https://github.com/actions/checkout/issues/766
5272
run: git config --global --add safe.directory ${GITHUB_WORKSPACE}
73+
5374
- name: Run matrix job
75+
working-directory: ${{ github.event.repository.name }} # until we can use action/checkout@v4
5476
env:
5577
SWIFT_VERSION: ${{ matrix.swift.swift_version }}
5678
COMMAND: ${{ inputs.matrix_linux_command }}
5779
EXAMPLE: ${{ matrix.examples }}
5880
run: |
59-
apt-get -qq update && apt-get -qq -y install curl
6081
./scripts/integration_tests.sh

Examples/APIGateway/Package.swift

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import PackageDescription
44

55
// needed for CI to test the local version of the library
6-
import class Foundation.ProcessInfo
76
import struct Foundation.URL
87

98
#if os(macOS)
@@ -16,17 +15,16 @@ let package = Package(
1615
name: "swift-aws-lambda-runtime-example",
1716
platforms: platforms,
1817
products: [
19-
.executable(name: "APIGAtewayLambda", targets: ["APIGAtewayLambda"])
18+
.executable(name: "APIGatewayLambda", targets: ["APIGatewayLambda"])
2019
],
2120
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")
21+
// during CI, the dependency on local version of swift-aws-lambda-runtime is added dynamically below
22+
.package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", branch: "main"),
23+
.package(url: "https://github.com/swift-server/swift-aws-lambda-events.git", branch: "main"),
2624
],
2725
targets: [
2826
.executableTarget(
29-
name: "APIGAtewayLambda",
27+
name: "APIGatewayLambda",
3028
dependencies: [
3129
.product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"),
3230
.product(name: "AWSLambdaEvents", package: "swift-aws-lambda-events"),
@@ -36,20 +34,29 @@ let package = Package(
3634
]
3735
)
3836

39-
if let localDepsPath = ProcessInfo.processInfo.environment["LAMBDA_USE_LOCAL_DEPS"],
37+
if let localDepsPath = Context.environment["LAMBDA_USE_LOCAL_DEPS"],
4038
localDepsPath != "",
4139
let v = try? URL(fileURLWithPath: localDepsPath).resourceValues(forKeys: [.isDirectoryKey]),
42-
let _ = v.isDirectory
40+
v.isDirectory == true
4341
{
42+
// when we use the local runtime as deps, let's remove the dependency added above
43+
let indexToRemove = package.dependencies.firstIndex { dependency in
44+
if case .sourceControl(
45+
name: _,
46+
location: "https://github.com/swift-server/swift-aws-lambda-runtime.git",
47+
requirement: _
48+
) = dependency.kind {
49+
return true
50+
}
51+
return false
52+
}
53+
if let indexToRemove {
54+
package.dependencies.remove(at: indexToRemove)
55+
}
56+
57+
// then we add the dependency on LAMBDA_USE_LOCAL_DEPS' path (typically ../..)
4458
print("[INFO] Compiling against swift-aws-lambda-runtime located at \(localDepsPath)")
4559
package.dependencies += [
4660
.package(name: "swift-aws-lambda-runtime", path: localDepsPath)
4761
]
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-
]
5562
}

Examples/APIGateway/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ swift package archive --allow-network-access docker
2626
```
2727

2828
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`
29+
The ZIP file is located at `.build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager/APIGatewayLambda/APIGatewayLambda.zip`
3030

3131
## Deploy
3232

@@ -40,9 +40,9 @@ To actually deploy your Lambda function and create the infrastructure, type the
4040

4141
```bash
4242
sam deploy \
43-
----resolve-s3 \
43+
--resolve-s3 \
4444
--template-file template.yaml \
45-
--stack-name MyLambda \
45+
--stack-name APIGatewayLambda \
4646
--capabilities CAPABILITY_IAM
4747
```
4848

@@ -53,8 +53,8 @@ The output is similar to this one.
5353
-----------------------------------------------------------------------------------------------------------------------------
5454
Outputs
5555
-----------------------------------------------------------------------------------------------------------------------------
56-
Key APIGAtewayEndpoint
57-
Description API Gateway endpoint UR"
56+
Key APIGatewayEndpoint
57+
Description API Gateway endpoint URL"
5858
Value https://a5q74es3k2.execute-api.us-east-1.amazonaws.com
5959
-----------------------------------------------------------------------------------------------------------------------------
6060
```

Examples/APIGateway/template.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ Description: SAM Template for QuoteService
44

55
Resources:
66
# Lambda function
7-
APIGAtewayLambda:
7+
APIGatewayLambda:
88
Type: AWS::Serverless::Function
99
Properties:
10-
CodeUri: .build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager/APIGAtewayLambda/APIGAtewayLambda.zip
10+
CodeUri: .build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager/APIGatewayLambda/APIGatewayLambda.zip
1111
Timeout: 60
12-
Handler: swift.bootstrap
12+
Handler: swift.bootstrap # ignored by the Swift runtime
1313
Runtime: provided.al2
1414
MemorySize: 512
1515
Architectures:
@@ -19,13 +19,13 @@ Resources:
1919
# by default, AWS Lambda runtime produces no log
2020
# use `LOG_LEVEL: debug` for for lifecycle and event handling information
2121
# use `LOG_LEVEL: trace` for detailed input event information
22-
LOG_LEVEL: trace
22+
LOG_LEVEL: debug
2323
Events:
2424
HttpApiEvent:
2525
Type: HttpApi
2626

2727
Outputs:
2828
# print API Gateway endpoint
29-
APIGAtewayEndpoint:
29+
APIGatewayEndpoint:
3030
Description: API Gateway endpoint UR"
3131
Value: !Sub "https://${ServerlessHttpApi}.execute-api.${AWS::Region}.amazonaws.com"

Examples/HelloWorld/Package.swift

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import PackageDescription
44

55
// needed for CI to test the local version of the library
6-
import class Foundation.ProcessInfo
76
import struct Foundation.URL
87

98
#if os(macOS)
@@ -19,8 +18,8 @@ let package = Package(
1918
.executable(name: "MyLambda", targets: ["MyLambda"])
2019
],
2120
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")
21+
// during CI, the dependency on local version of swift-aws-lambda-runtime is added dynamically below
22+
.package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", branch: "main")
2423
],
2524
targets: [
2625
.executableTarget(
@@ -33,20 +32,29 @@ let package = Package(
3332
]
3433
)
3534

36-
if let localDepsPath = ProcessInfo.processInfo.environment["LAMBDA_USE_LOCAL_DEPS"],
35+
if let localDepsPath = Context.environment["LAMBDA_USE_LOCAL_DEPS"],
3736
localDepsPath != "",
3837
let v = try? URL(fileURLWithPath: localDepsPath).resourceValues(forKeys: [.isDirectoryKey]),
39-
let _ = v.isDirectory
38+
v.isDirectory == true
4039
{
40+
// when we use the local runtime as deps, let's remove the dependency added above
41+
let indexToRemove = package.dependencies.firstIndex { dependency in
42+
if case .sourceControl(
43+
name: _,
44+
location: "https://github.com/swift-server/swift-aws-lambda-runtime.git",
45+
requirement: _
46+
) = dependency.kind {
47+
return true
48+
}
49+
return false
50+
}
51+
if let indexToRemove {
52+
package.dependencies.remove(at: indexToRemove)
53+
}
54+
55+
// then we add the dependency on LAMBDA_USE_LOCAL_DEPS' path (typically ../..)
4156
print("[INFO] Compiling against swift-aws-lambda-runtime located at \(localDepsPath)")
4257
package.dependencies += [
4358
.package(name: "swift-aws-lambda-runtime", path: localDepsPath)
4459
]
45-
46-
} else {
47-
print("[INFO] LAMBDA_USE_LOCAL_DEPS is not pointing to your local swift-aws-lambda-runtime code")
48-
print("[INFO] This project will compile against the main branch of the Lambda Runtime on GitHub")
49-
package.dependencies += [
50-
.package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", branch: "main")
51-
]
5260
}

Examples/S3_AWSSDK/.gitignore

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
.DS_Store
2+
.aws-sam/
3+
.build
4+
samtemplate.toml
5+
*/build/*
6+
/.build
7+
/Packages
8+
xcuserdata/
9+
DerivedData/
10+
.swiftpm/configuration/registries.json
11+
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
12+
.netrc

Examples/S3_AWSSDK/Package.swift

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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 struct Foundation.URL
7+
8+
#if os(macOS)
9+
let platforms: [PackageDescription.SupportedPlatform]? = [.macOS(.v15)]
10+
#else
11+
let platforms: [PackageDescription.SupportedPlatform]? = nil
12+
#endif
13+
14+
let package = Package(
15+
name: "AWSSDKExample",
16+
platforms: platforms,
17+
products: [
18+
.executable(name: "AWSSDKExample", targets: ["AWSSDKExample"])
19+
],
20+
dependencies: [
21+
// during CI, the dependency on local version of swift-aws-lambda-runtime is added dynamically below
22+
.package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", branch: "main"),
23+
.package(url: "https://github.com/swift-server/swift-aws-lambda-events", branch: "main"),
24+
.package(url: "https://github.com/awslabs/aws-sdk-swift", from: "1.0.0"),
25+
],
26+
targets: [
27+
.executableTarget(
28+
name: "AWSSDKExample",
29+
dependencies: [
30+
.product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"),
31+
.product(name: "AWSLambdaEvents", package: "swift-aws-lambda-events"),
32+
.product(name: "AWSS3", package: "aws-sdk-swift"),
33+
]
34+
)
35+
]
36+
)
37+
38+
if let localDepsPath = Context.environment["LAMBDA_USE_LOCAL_DEPS"],
39+
localDepsPath != "",
40+
let v = try? URL(fileURLWithPath: localDepsPath).resourceValues(forKeys: [.isDirectoryKey]),
41+
v.isDirectory == true
42+
{
43+
// when we use the local runtime as deps, let's remove the dependency added above
44+
let indexToRemove = package.dependencies.firstIndex { dependency in
45+
if case .sourceControl(
46+
name: _,
47+
location: "https://github.com/swift-server/swift-aws-lambda-runtime.git",
48+
requirement: _
49+
) = dependency.kind {
50+
return true
51+
}
52+
return false
53+
}
54+
if let indexToRemove {
55+
package.dependencies.remove(at: indexToRemove)
56+
}
57+
58+
// then we add the dependency on LAMBDA_USE_LOCAL_DEPS' path (typically ../..)
59+
print("[INFO] Compiling against swift-aws-lambda-runtime located at \(localDepsPath)")
60+
package.dependencies += [
61+
.package(name: "swift-aws-lambda-runtime", path: localDepsPath)
62+
]
63+
}

0 commit comments

Comments
 (0)