-
Notifications
You must be signed in to change notification settings - Fork 113
Add AWS SDK and Soto examples #396
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 31 commits
Commits
Show all changes
41 commits
Select commit
Hold shift + click to select a range
cb2cdde
fix typos on API GAteway example
sebsto bd07401
add aws sdk example
sebsto 570b416
adjust for local CI
sebsto 5e80824
add soto example
sebsto 0398590
Merge branch 'main' into sebsto/awssdk_example
sebsto c75c999
update soto example readme
sebsto b545738
format
sebsto 903c5d3
Merge branch 'main' into sebsto/awssdk_example
sebsto 6bf8f0f
swift format
sebsto b41810e
Merge branch 'swift-server:main' into sebsto/awssdk_example
sebsto 77b0c86
fix uppercase A
sebsto 83446a8
remove unnecessary license header file
sebsto 1030816
use trailing closure syntax + reuse S3 client
sebsto ba1f39d
remove unnecessary license header
sebsto 33202cd
remove condition on Linux for platform
sebsto 46e5337
swift format
sebsto 683cd55
remove unnecessary license header
sebsto cc3f3c2
add soto client shutdown
sebsto 20f7e51
Merge branch 'main' into sebsto/awssdk_example
sebsto 5dc8371
Merge branch 'main' into sebsto/awssdk_example
sebsto 0e4fa0c
revert platform dance to not break CI
sebsto 39deb51
remove unnecessary init
sebsto bc3fe1a
rename directory to SotoS3
sebsto 4748146
Merge branch 'main' into sebsto/awssdk_example
sebsto 0e9b045
change directory name to focus on use case first, lib second
sebsto 9c91745
fix typos in the readme
sebsto 28ca63e
Merge branch 'main' into sebsto/awssdk_example
sebsto e54297b
Merge branch 'main' into sebsto/awssdk_example
sebsto a3b29c6
add new examples to CI
sebsto 96efb9a
fix CI for examples
sebsto b47d1b8
use amazonlinux docker image instead of ubuntu
sebsto 38ab48a
dynamically add runtime dependency based on env var
sebsto 4e73292
remove import of Foundation.ProcessInfo
sebsto 56b07b5
change log level and add comment in template.yaml
sebsto 156c7e7
downgrade actions/checkout to v3 to workaround dependency on mazonLin…
sebsto b747e5d
Swift format
sebsto 34ec3ed
workaround https://github.com/actions/checkout/issues/1487
sebsto d40d324
fix CI
sebsto 6998a50
fix ci
sebsto c5039ea
fix ci
sebsto f5506f5
fix ci
sebsto File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
.DS_Store | ||
.aws-sam/ | ||
.build | ||
samtemplate.toml | ||
*/build/* | ||
/.build | ||
/Packages | ||
xcuserdata/ | ||
DerivedData/ | ||
.swiftpm/configuration/registries.json | ||
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata | ||
.netrc |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// swift-tools-version: 6.0 | ||
|
||
import PackageDescription | ||
|
||
// needed for CI to test the local version of the library | ||
import class Foundation.ProcessInfo | ||
import struct Foundation.URL | ||
|
||
#if os(macOS) | ||
let platforms: [PackageDescription.SupportedPlatform]? = [.macOS(.v15)] | ||
#else | ||
let platforms: [PackageDescription.SupportedPlatform]? = nil | ||
#endif | ||
|
||
let package = Package( | ||
name: "AWSSDKExample", | ||
platforms: platforms, | ||
products: [ | ||
.executable(name: "AWSSDKExample", targets: ["AWSSDKExample"]) | ||
], | ||
dependencies: [ | ||
// dependency on swift-aws-lambda-runtime is added dynamically below | ||
// .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", branch: "main"), | ||
.package(url: "https://github.com/awslabs/aws-sdk-swift", from: "1.0.0"), | ||
], | ||
targets: [ | ||
.executableTarget( | ||
name: "AWSSDKExample", | ||
dependencies: [ | ||
.product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"), | ||
.product(name: "AWSLambdaEvents", package: "swift-aws-lambda-events"), | ||
.product(name: "AWSS3", package: "aws-sdk-swift"), | ||
] | ||
) | ||
] | ||
) | ||
|
||
if let localDepsPath = ProcessInfo.processInfo.environment["LAMBDA_USE_LOCAL_DEPS"], | ||
localDepsPath != "", | ||
let v = try? URL(fileURLWithPath: localDepsPath).resourceValues(forKeys: [.isDirectoryKey]), | ||
let _ = v.isDirectory | ||
{ | ||
print("[INFO] Compiling against swift-aws-lambda-runtime located at \(localDepsPath)") | ||
package.dependencies += [ | ||
.package(name: "swift-aws-lambda-runtime", path: localDepsPath) | ||
] | ||
|
||
} else { | ||
print("[INFO] LAMBDA_USE_LOCAL_DEPS is not pointing to your local swift-aws-lambda-runtime code") | ||
print("[INFO] This project will compile against the main branch of the Lambda Runtime on GitHub") | ||
package.dependencies += [ | ||
.package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", branch: "main") | ||
] | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# List Amazon S3 Buckets with the AWS SDK for Swift | ||
|
||
This is a simple example of an AWS Lambda function that uses the [AWS SDK for Swift](https://github.com/awslabs/aws-sdk-swift) to read data from Amazon S3. | ||
|
||
## Code | ||
|
||
The Lambda function reads all bucket names from your AWS account and returns them as a String. | ||
|
||
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. | ||
|
||
The handler is `(event: APIGatewayV2Request, context: LambdaContext) -> APIGatewayV2Response`. The function takes two arguments: | ||
- 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. | ||
- the context argument is a `Lambda Context`. It is a description of the runtime context. | ||
|
||
The function must return a `APIGatewayV2Response`. | ||
|
||
`APIGatewayV2Request` and `APIGatewayV2Response` are defined in the [Swift AWS Lambda Events](https://github.com/swift-server/swift-aws-lambda-events) library. | ||
|
||
The handler creates an S3 client and `ListBucketsInput` object. It passes the input object to the client and receives an output response. | ||
It then extracts the list of bucket names from the output and creates a `\n`-separated list of names, as a `String` | ||
|
||
## Build & Package | ||
|
||
To build the package, type the following commands. | ||
|
||
```bash | ||
swift build | ||
swift package archive --allow-network-access docker | ||
``` | ||
|
||
If there is no error, there is a ZIP file ready to deploy. | ||
The ZIP file is located at `.build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager/AWSSDKExample/AWSSDKExample.zip` | ||
|
||
## Deploy | ||
|
||
The deployment must include the Lambda function and an 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. | ||
|
||
**Prerequisites** : Install the [SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/install-sam-cli.html) | ||
|
||
The example directory contains a file named `template.yaml` that describes the deployment. | ||
|
||
To actually deploy your Lambda function and create the infrastructure, type the following `sam` command. | ||
|
||
```bash | ||
sam deploy \ | ||
--resolve-s3 \ | ||
--template-file template.yaml \ | ||
--stack-name AWSSDKExample \ | ||
--capabilities CAPABILITY_IAM | ||
``` | ||
|
||
At the end of the deployment, the script lists the API Gateway endpoint. | ||
The output is similar to this one. | ||
|
||
``` | ||
----------------------------------------------------------------------------------------------------------------------------- | ||
Outputs | ||
----------------------------------------------------------------------------------------------------------------------------- | ||
Key APIGatewayEndpoint | ||
Description API Gateway endpoint URL" | ||
Value https://a5q74es3k2.execute-api.us-east-1.amazonaws.com | ||
----------------------------------------------------------------------------------------------------------------------------- | ||
``` | ||
|
||
## Invoke your Lambda function | ||
|
||
To invoke the Lambda function, use this `curl` command line. | ||
|
||
```bash | ||
curl https://a5q74es3k2.execute-api.us-east-1.amazonaws.com | ||
``` | ||
|
||
Be sure to replace the URL with the API Gateway endpoint returned in the previous step. | ||
|
||
This should print text similar to | ||
|
||
```bash | ||
my_bucket_1 | ||
my_bucket_2 | ||
... | ||
``` | ||
|
||
## Delete the infrastructure | ||
|
||
When done testing, you can delete the infrastructure with this command. | ||
|
||
```bash | ||
sam delete | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the SwiftAWSLambdaRuntime open source project | ||
// | ||
// Copyright (c) 2024 Apple Inc. and the SwiftAWSLambdaRuntime project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import AWSLambdaEvents | ||
import AWSLambdaRuntime | ||
@preconcurrency import AWSS3 | ||
|
||
let client = try await S3Client() | ||
|
||
let runtime = LambdaRuntime { | ||
(event: APIGatewayV2Request, context: LambdaContext) async throws -> APIGatewayV2Response in | ||
|
||
var response: APIGatewayV2Response | ||
do { | ||
// read the list of buckets | ||
context.logger.debug("Reading list of buckets") | ||
let output = try await client.listBuckets(input: ListBucketsInput()) | ||
let bucketList = output.buckets?.compactMap { $0.name } | ||
response = APIGatewayV2Response(statusCode: .ok, body: bucketList?.joined(separator: "\n")) | ||
} catch { | ||
context.logger.error("\(error)") | ||
response = APIGatewayV2Response(statusCode: .internalServerError, body: "[ERROR] \(error)") | ||
} | ||
return response | ||
} | ||
|
||
try await runtime.run() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
AWSTemplateFormatVersion: '2010-09-09' | ||
Transform: AWS::Serverless-2016-10-31 | ||
Description: SAM Template for AWS SDK Example | ||
|
||
Resources: | ||
# Lambda function | ||
AWSSDKExample: | ||
Type: AWS::Serverless::Function | ||
Properties: | ||
CodeUri: .build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager/AWSSDKExample/AWSSDKExample.zip | ||
Timeout: 60 | ||
Handler: swift.bootstrap | ||
sebsto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Runtime: provided.al2 | ||
MemorySize: 512 | ||
Architectures: | ||
- arm64 | ||
Environment: | ||
Variables: | ||
# by default, AWS Lambda runtime produces no log | ||
# use `LOG_LEVEL: debug` for for lifecycle and event handling information | ||
# use `LOG_LEVEL: trace` for detailed input event information | ||
LOG_LEVEL: trace | ||
sebsto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# Handles all methods of the REST API | ||
Events: | ||
Api: | ||
Type: HttpApi | ||
|
||
# Add an IAM policy to this function. | ||
# It grants the function permissions to read the list of buckets in your account. | ||
Policies: | ||
- Statement: | ||
- Sid: ListAllS3BucketsInYourAccount | ||
Effect: Allow | ||
Action: | ||
- s3:ListAllMyBuckets | ||
Resource: '*' | ||
|
||
# print API endpoint | ||
Outputs: | ||
SwiftAPIEndpoint: | ||
Description: "API Gateway endpoint URL for your application" | ||
Value: !Sub "https://${ServerlessHttpApi}.execute-api.${AWS::Region}.amazonaws.com" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
.DS_Store | ||
.aws-sam/ | ||
.build | ||
samtemplate.toml | ||
*/build/* | ||
/.build | ||
/Packages | ||
xcuserdata/ | ||
DerivedData/ | ||
.swiftpm/configuration/registries.json | ||
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata | ||
.netrc |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.