|
1 | 1 | # Hello World, with no traits
|
2 | 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. |
| 3 | +This is an example of a low-level AWS Lambda function that takes a `ByteBuffer` as input parameter and writes its response on the provided `LambdaResponseStreamWriter`. |
4 | 4 |
|
5 |
| -This function disables all the default traits: the support for JSON from Foundation, for Swift Service Lifecycle, and for the local server for testing. |
| 5 | +This function disables all the default traits: the support for JSON Encoder and Decoder from Foundation, the support for Swift Service Lifecycle, and for the local tetsing server. |
6 | 6 |
|
7 | 7 | The main reasons of the existence of this example are
|
8 | 8 |
|
9 |
| -1. to show you how to disable traits when using the Lambda Runtime Library |
10 |
| -2. to add an integration test to our continous integration pipeline to make sure the library compiles with no traits enabled. |
| 9 | +1. to show how to write a low-level Lambda function that doesn't rely on JSON encodinga and decoding. |
| 10 | +2. to show you how to disable traits when using the Lambda Runtime Library. |
| 11 | +3. to add an integration test to our continous integration pipeline to make sure the library compiles with no traits enabled. |
11 | 12 |
|
12 |
| -For more details about this example, refer to the example in `Examples/HelloWorld`. |
| 13 | +## Disabling all traits |
| 14 | + |
| 15 | +Traits are functions of the AWS Lambda Runtime that you can disable at compile time to reduce the size of your binary, and therefore reduce the cold start time of your Lambda function. |
| 16 | + |
| 17 | +The library supports three traits: |
| 18 | + |
| 19 | +- "FoundationJSONSupport": adds the required API to encode and decode payloads with Foundation's `JSONEncoder` and `JSONDecoder`. |
| 20 | + |
| 21 | +- "ServiceLifecycleSupport": adds support for the Swift Service Lifecycle library. |
| 22 | + |
| 23 | +- "LocalServerSupport": adds support for testing your function locally with a built-in HTTP server. |
| 24 | + |
| 25 | +This example disables all the traits. To disable one or several traits, modify `Package.swift`: |
| 26 | + |
| 27 | +```swift |
| 28 | + dependencies: [ |
| 29 | + .package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", from: "2.0.0-beta", traits: []) |
| 30 | + ], |
| 31 | +``` |
| 32 | + |
| 33 | +## Code |
| 34 | + |
| 35 | +The code creates a `LambdaRuntime` struct. In its 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. |
| 36 | + |
| 37 | +The handler signature is `(event: ByteBuffer, response: LambdaResponseStreamWriter, context: LambdaContext)`. |
| 38 | + |
| 39 | +The function takes three arguments: |
| 40 | +- the event argument is a `ByteBuffer`. It's the parameter passed when invoking the function. You are responsible of decoding this parameter, if necessary. |
| 41 | +- the response writer provides you with functions to write the response stream back. |
| 42 | +- the context argument is a `Lambda Context`. It is a description of the runtime context. |
| 43 | + |
| 44 | +The function return value will be encoded as your Lambda function response. |
| 45 | + |
| 46 | +## Test locally |
| 47 | + |
| 48 | +You cannot test this function locally, because the "LocalServer" trait is disabled. |
| 49 | + |
| 50 | +## Build & Package |
| 51 | + |
| 52 | +To build & archive the package, type the following commands. |
| 53 | + |
| 54 | +```bash |
| 55 | +swift build |
| 56 | +swift package archive --allow-network-connections docker |
| 57 | +``` |
| 58 | + |
| 59 | +If there is no error, there is a ZIP file ready to deploy. |
| 60 | +The ZIP file is located at `.build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager/MyLambda/MyLambda.zip` |
| 61 | + |
| 62 | +## Deploy |
| 63 | + |
| 64 | +Here is how to deploy using the `aws` command line. |
| 65 | + |
| 66 | +```bash |
| 67 | +aws lambda create-function \ |
| 68 | +--function-name MyLambda \ |
| 69 | +--zip-file fileb://.build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager/MyLambda/MyLambda.zip \ |
| 70 | +--runtime provided.al2 \ |
| 71 | +--handler provided \ |
| 72 | +--architectures arm64 \ |
| 73 | +--role arn:aws:iam::<YOUR_ACCOUNT_ID>:role/lambda_basic_execution |
| 74 | +``` |
| 75 | + |
| 76 | +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`. |
| 77 | + |
| 78 | +Be sure to replace <YOUR_ACCOUNT_ID> with your actual AWS account ID (for example: 012345678901). |
| 79 | + |
| 80 | +## Invoke your Lambda function |
| 81 | + |
| 82 | +To invoke the Lambda function, use this `aws` command line. |
| 83 | + |
| 84 | +```bash |
| 85 | +aws lambda invoke \ |
| 86 | +--function-name MyLambda \ |
| 87 | +--payload $(echo "Seb" | base64) \ |
| 88 | +out.txt && cat out.txt && rm out.txt |
| 89 | +``` |
| 90 | + |
| 91 | +This should output the following result. |
| 92 | + |
| 93 | +``` |
| 94 | +{ |
| 95 | + "StatusCode": 200, |
| 96 | + "ExecutedVersion": "$LATEST" |
| 97 | +} |
| 98 | +"Hello World!" |
| 99 | +``` |
| 100 | + |
| 101 | +## Undeploy |
| 102 | + |
| 103 | +When done testing, you can delete the Lambda function with this command. |
| 104 | + |
| 105 | +```bash |
| 106 | +aws lambda delete-function --function-name MyLambda |
| 107 | +``` |
0 commit comments