Skip to content

Commit d70f081

Browse files
committed
add readme + swift-format
1 parent f3ef65e commit d70f081

File tree

3 files changed

+73
-7
lines changed

3 files changed

+73
-7
lines changed

Examples/HelloWorld/Package.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22

33
import PackageDescription
44

5+
// needed for CI to test the local version of the library
6+
import class Foundation.ProcessInfo
7+
import struct Foundation.URL
8+
59
#if os(macOS)
610
let platforms: [PackageDescription.SupportedPlatform]? = [.macOS(.v15)]
711
#else
812
let platforms: [PackageDescription.SupportedPlatform]? = nil
913
#endif
1014

11-
// needed for CI to test the local version of the library
12-
import class Foundation.ProcessInfo
13-
import struct Foundation.URL
14-
1515
let package = Package(
1616
name: "swift-aws-lambda-runtime-example",
1717
platforms: platforms,

Examples/HelloWorld/README.md

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,66 @@
1-
TO BE DONE
1+
# Hello World
2+
3+
This is a simple example of an AWS Lambda function that takes a String as input parameter and returns a String as response.
4+
5+
## Code
6+
7+
The code creates a `LambdaRuntime` struct. In it's simplest form, it takes a function as argument. The function is the Lambda handler that will be invoked when an event triggers the Lambda function.
8+
9+
The handler is `(event: String, context: LambdaContext) -> String`. The function takes two arguments:
10+
- the event argument is a `String`. It is the parameter passed when invoking the function.
11+
- the context argument is a `Lambda Context`. It is a description of the runtime context.
12+
13+
The function must return a String.
14+
15+
## Build & Package
16+
17+
To build & archive the package, type the following commands.
18+
19+
```bash
20+
swift build
21+
swift package archive --disable-sandbox
22+
```
23+
24+
If there is no error, there is a ZIP file ready to deploy.
25+
The ZIP file is located at `.build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager/MyLambda/MyLambda.zip`
26+
27+
## Deploy
28+
29+
Here is how to deploy using the `aws` command line.
30+
31+
```bash
32+
aws lambda create-function \
33+
--function-name MyLambda \
34+
--zip-file fileb://.build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager/MyLambda/MyLambda.zip \
35+
--runtime provided.al2 \
36+
--handler provided \
37+
--architectures arm64 \
38+
--role arn:aws:iam::<YOUR_ACCOUNT_ID>:role/lambda_basic_execution
39+
```
40+
41+
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`.
42+
43+
Be sure to replace <YOUR_ACCOUNT_ID> with your actual AWS account ID (for example: 012345678901).
44+
45+
## Invoke your Lambda function
46+
47+
To invoke the Lambda function, use this `aws` command line.
48+
49+
```bash
50+
aws lambda invoke \
51+
--function-name MyLambda \
52+
--payload $(echo \"Seb\" | base64) \
53+
out.txt && cat out.txt && rm out.txt
54+
```
55+
56+
Note that the payload is expected to be a valid JSON strings, hence the surroundings quotes (`"`).
57+
58+
This should print
59+
60+
```
61+
{
62+
"StatusCode": 200,
63+
"ExecutedVersion": "$LATEST"
64+
}
65+
"Hello Seb"
66+
```

Examples/HelloWorld/Sources/main.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ import AWSLambdaRuntime
1616

1717
// in this example we are receiving and responding with strings
1818

19-
let runtime = LambdaRuntime { (event: String, context: LambdaContext) in
20-
return "Hello \(event)"
19+
let runtime = LambdaRuntime {
20+
(event: String, context: LambdaContext) in
21+
"Hello \(event)"
2122
}
2223

2324
try await runtime.run()

0 commit comments

Comments
 (0)