|
| 1 | +# Hello JSON |
| 2 | + |
| 3 | +This is a simple example of an AWS Lambda function that takes a JSON structure as an input parameter and returns a JSON structure as a response. |
| 4 | + |
| 5 | +The runtime takes care of decoding the input and encoding the output. |
| 6 | + |
| 7 | +## Code |
| 8 | + |
| 9 | +The code defines `HelloRequest` and `HelloResponse` data structures to represent the input and output payloads. These structures are typically shared with a client project, such as an iOS application. |
| 10 | + |
| 11 | +The code creates a `LambdaRuntime` struct. In it's simplest form, the initializer takes a function as an argument. The function is the handler that will be invoked when an event triggers the Lambda function. |
| 12 | + |
| 13 | +The handler is `(event: HelloRequest, context: LambdaContext)`. The function takes two arguments: |
| 14 | +- the event argument is a `HelloRequest`. It is the parameter passed when invoking the function. |
| 15 | +- the context argument is a `Lambda Context`. It is a description of the runtime context. |
| 16 | + |
| 17 | +The function return value will be encoded to a `HelloResponse` as your Lambda function response. |
| 18 | + |
| 19 | +## Build & Package |
| 20 | + |
| 21 | +To build & archive the package, type the following commands. |
| 22 | + |
| 23 | +```bash |
| 24 | +swift package archive --allow-network-connections docker |
| 25 | +``` |
| 26 | + |
| 27 | +If there is no error, there is a ZIP file ready to deploy. |
| 28 | +The ZIP file is located at `.build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager/HelloJSON/HelloJSON.zip` |
| 29 | + |
| 30 | +## Deploy |
| 31 | + |
| 32 | +Here is how to deploy using the `aws` command line. |
| 33 | + |
| 34 | +```bash |
| 35 | +# Replace with your AWS Account ID |
| 36 | +AWS_ACCOUNT_ID=012345678901 |
| 37 | + |
| 38 | +aws lambda create-function \ |
| 39 | +--function-name HelloJSON \ |
| 40 | +--zip-file fileb://.build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager/HelloJSON/HelloJSON.zip \ |
| 41 | +--runtime provided.al2 \ |
| 42 | +--handler provided \ |
| 43 | +--architectures arm64 \ |
| 44 | +--role arn:aws:iam::${AWS_ACCOUNT_ID}:role/lambda_basic_execution |
| 45 | +``` |
| 46 | + |
| 47 | +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`. |
| 48 | + |
| 49 | +Be sure to define the `AWS_ACCOUNT_ID` environment variable with your actual AWS account ID (for example: 012345678901). |
| 50 | + |
| 51 | +## Invoke your Lambda function |
| 52 | + |
| 53 | +To invoke the Lambda function, use this `aws` command line. |
| 54 | + |
| 55 | +```bash |
| 56 | +aws lambda invoke \ |
| 57 | +--function-name HelloJSON \ |
| 58 | +--payload $(echo '{ "name" : "Seb", "age" : 50 }' | base64) \ |
| 59 | +out.txt && cat out.txt && rm out.txt |
| 60 | +``` |
| 61 | + |
| 62 | +Note that the payload is expected to be a valid JSON string. |
| 63 | + |
| 64 | +This should output the following result. |
| 65 | + |
| 66 | +``` |
| 67 | +{ |
| 68 | + "StatusCode": 200, |
| 69 | + "ExecutedVersion": "$LATEST" |
| 70 | +} |
| 71 | +{"greetings":"Hello Seb. You look younger than your age."} |
| 72 | +``` |
| 73 | + |
| 74 | +## Undeploy |
| 75 | + |
| 76 | +When done testing, you can delete the Lambda function with this command. |
| 77 | + |
| 78 | +```bash |
| 79 | +aws lambda delete-function --function-name HelloJSON |
| 80 | +``` |
0 commit comments