-
Notifications
You must be signed in to change notification settings - Fork 113
add HelloJSON example + README #424
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 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
fbe7aa2
add HelloJSON example + README
sebsto de1cf69
Merge branch 'main' into sebsto/hellojson
sebsto 942ef18
typo
sebsto b059e4f
typo
sebsto 456cece
typo
sebsto 0e2c2f3
typo
sebsto bec8ea2
typo
sebsto 1cb0d98
typo
sebsto d981e40
typo
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
response.json | ||
samconfig.toml | ||
template.yaml | ||
Makefile |
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,59 @@ | ||
// swift-tools-version:6.0 | ||
|
||
import PackageDescription | ||
|
||
// needed for CI to test the local version of the library | ||
import struct Foundation.URL | ||
|
||
#if os(macOS) | ||
let platforms: [PackageDescription.SupportedPlatform]? = [.macOS(.v15)] | ||
#else | ||
let platforms: [PackageDescription.SupportedPlatform]? = nil | ||
#endif | ||
|
||
let package = Package( | ||
name: "swift-aws-lambda-runtime-example", | ||
platforms: platforms, | ||
products: [ | ||
.executable(name: "HelloJSON", targets: ["HelloJSON"]) | ||
], | ||
dependencies: [ | ||
// during CI, the dependency on local version of swift-aws-lambda-runtime is added dynamically below | ||
.package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", branch: "main") | ||
], | ||
targets: [ | ||
.executableTarget( | ||
name: "HelloJSON", | ||
dependencies: [ | ||
.product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime") | ||
] | ||
) | ||
] | ||
) | ||
|
||
if let localDepsPath = Context.environment["LAMBDA_USE_LOCAL_DEPS"], | ||
localDepsPath != "", | ||
let v = try? URL(fileURLWithPath: localDepsPath).resourceValues(forKeys: [.isDirectoryKey]), | ||
v.isDirectory == true | ||
{ | ||
// when we use the local runtime as deps, let's remove the dependency added above | ||
let indexToRemove = package.dependencies.firstIndex { dependency in | ||
if case .sourceControl( | ||
name: _, | ||
location: "https://github.com/swift-server/swift-aws-lambda-runtime.git", | ||
requirement: _ | ||
) = dependency.kind { | ||
return true | ||
} | ||
return false | ||
} | ||
if let indexToRemove { | ||
package.dependencies.remove(at: indexToRemove) | ||
} | ||
|
||
// then we add the dependency on LAMBDA_USE_LOCAL_DEPS' path (typically ../..) | ||
print("[INFO] Compiling against swift-aws-lambda-runtime located at \(localDepsPath)") | ||
package.dependencies += [ | ||
.package(name: "swift-aws-lambda-runtime", path: localDepsPath) | ||
] | ||
} |
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,80 @@ | ||||||
# Hello JSON | ||||||
|
||||||
This is a simple example of an AWS Lambda function that takes a JSON structure as input parameter and returns a JSON structure as response. | ||||||
|
||||||
The runtime takes care of decoding the input and encoding the output. | ||||||
|
||||||
## Code | ||||||
|
||||||
The code defines a `HelloRequest` and `HelloResponse` data structure to represent the input and outpout payload. These structures are typically shared with a client project, such as an iOS application. | ||||||
sebsto marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
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 an event triggers the Lambda function. | ||||||
sebsto marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
The handler is `(event: HelloRequest, context: LambdaContext)`. The function takes two arguments: | ||||||
- the event argument is a `HelloRequest`. It is the parameter passed when invoking the function. | ||||||
- the context argument is a `Lambda Context`. It is a description of the runtime context. | ||||||
|
||||||
The function return value will be encoded to an `HelloResponse` as your Lambda function response. | ||||||
sebsto marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
## Build & Package | ||||||
|
||||||
To build & archive the package, type the following commands. | ||||||
|
||||||
```bash | ||||||
swift package archive --allow-network-connections 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/HelloJSON/HelloJSON.zip` | ||||||
|
||||||
## Deploy | ||||||
|
||||||
Here is how to deploy using the `aws` command line. | ||||||
|
||||||
```bash | ||||||
# Replace with your AWS Account ID | ||||||
AWS_ACCOUNT_ID=012345678901 | ||||||
|
||||||
aws lambda create-function \ | ||||||
--function-name HelloJSON \ | ||||||
--zip-file fileb://.build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager/HelloJSON/HelloJSON.zip \ | ||||||
--runtime provided.al2 \ | ||||||
--handler provided \ | ||||||
--architectures arm64 \ | ||||||
--role arn:aws:iam::${AWS_ACCOUNT_ID}:role/lambda_basic_execution | ||||||
``` | ||||||
|
||||||
The `--architectures` flag is only required when you build the binary on an Apple Silicon machine (Apple M1 or more recent). It defaults to `x64`. | ||||||
|
||||||
Be sure to define the `AWS_ACCOUNT_ID` environment variable with your actual AWS account ID (for example: 012345678901). | ||||||
|
||||||
## Invoke your Lambda function | ||||||
|
||||||
To invoke the Lambda function, use this `aws` command line. | ||||||
|
||||||
```bash | ||||||
aws lambda invoke \ | ||||||
--function-name HelloJSON \ | ||||||
--payload $(echo '{ "name" : "Seb", "age" : 50 }' | base64) \ | ||||||
out.txt && cat out.txt && rm out.txt | ||||||
``` | ||||||
|
||||||
Note that the payload is expected to be a valid JSON string. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is encoded right?
Suggested change
|
||||||
|
||||||
This should output the following result. | ||||||
|
||||||
``` | ||||||
{ | ||||||
"StatusCode": 200, | ||||||
"ExecutedVersion": "$LATEST" | ||||||
} | ||||||
{"greetings":"Hello Seb. You look younger than your age."} | ||||||
``` | ||||||
|
||||||
## Undeploy | ||||||
|
||||||
When done testing, you can delete the Lambda function with this command. | ||||||
|
||||||
```bash | ||||||
aws lambda delete-function --function-name HelloJSON | ||||||
``` |
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,40 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 AWSLambdaRuntime | ||
|
||
// in this example we are receiving and responding with a JSON structure | ||
sebsto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// the data structure to represent the input parameter | ||
struct HelloRequest: Decodable { | ||
let name: String | ||
let age: Int | ||
} | ||
|
||
// the data structure to represent the output response | ||
struct HelloResponse: Encodable { | ||
let greetings: String | ||
} | ||
|
||
// the Lambda runtime | ||
let runtime = LambdaRuntime { | ||
(event: HelloRequest, context: LambdaContext) in | ||
|
||
HelloResponse( | ||
greetings: "Hello \(event.name). You look \(event.age > 30 ? "younger" : "older") than your age." | ||
) | ||
} | ||
|
||
// start the loop | ||
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
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
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.