|
| 1 | +# Hummingbird Lambda |
| 2 | + |
| 3 | +This is a simple example of an AWS Lambda function using the [Hummingbird](https://github.com/hummingbird-project/hummingbird) web framework, invoked through an Amazon API Gateway. |
| 4 | + |
| 5 | +## Code |
| 6 | + |
| 7 | +The Lambda function uses Hummingbird's router to handle HTTP requests. It defines a simple GET endpoint at `/hello` that returns "Hello". |
| 8 | + |
| 9 | +The code creates a `Router` with `AppRequestContext` (which is a type alias for `BasicLambdaRequestContext<APIGatewayV2Request>`). The router defines HTTP routes using Hummingbird's familiar syntax. |
| 10 | + |
| 11 | +The `APIGatewayV2LambdaFunction` wraps the Hummingbird router to make it compatible with AWS Lambda and API Gateway V2 events. |
| 12 | + |
| 13 | +`APIGatewayV2Request` is defined in the [Swift AWS Lambda Events](https://github.com/swift-server/swift-aws-lambda-events) library, and the Hummingbird Lambda integration is provided by the [Hummingbird Lambda](https://github.com/hummingbird-project/hummingbird-lambda) package. |
| 14 | + |
| 15 | +## Build & Package |
| 16 | + |
| 17 | +To build the package, type the following commands. |
| 18 | + |
| 19 | +```bash |
| 20 | +swift build |
| 21 | +swift package archive --allow-network-connections docker |
| 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/HBLambda/HBLambda.zip` |
| 26 | + |
| 27 | +## Deploy |
| 28 | + |
| 29 | +The deployment must include the Lambda function and the 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. |
| 30 | + |
| 31 | +**Prerequisites** : Install the [SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/install-sam-cli.html) |
| 32 | + |
| 33 | +The example directory contains a file named `template.yaml` that describes the deployment. |
| 34 | + |
| 35 | +To actually deploy your Lambda function and create the infrastructure, type the following `sam` command. |
| 36 | + |
| 37 | +```bash |
| 38 | +sam deploy \ |
| 39 | +--resolve-s3 \ |
| 40 | +--template-file template.yaml \ |
| 41 | +--stack-name HummingbirdLambda \ |
| 42 | +--capabilities CAPABILITY_IAM |
| 43 | +``` |
| 44 | + |
| 45 | +At the end of the deployment, the script lists the API Gateway endpoint. |
| 46 | +The output is similar to this one. |
| 47 | + |
| 48 | +``` |
| 49 | +----------------------------------------------------------------------------------------------------------------------------- |
| 50 | +Outputs |
| 51 | +----------------------------------------------------------------------------------------------------------------------------- |
| 52 | +Key APIGatewayEndpoint |
| 53 | +Description API Gateway endpoint URL" |
| 54 | +Value https://a5q74es3k2.execute-api.us-east-1.amazonaws.com |
| 55 | +----------------------------------------------------------------------------------------------------------------------------- |
| 56 | +``` |
| 57 | + |
| 58 | +## Invoke your Lambda function |
| 59 | + |
| 60 | +To invoke the Lambda function, use this `curl` command line to call the `/hello` endpoint. |
| 61 | + |
| 62 | +```bash |
| 63 | +curl https://a5q74es3k2.execute-api.us-east-1.amazonaws.com/hello |
| 64 | +``` |
| 65 | + |
| 66 | +Be sure to replace the URL with the API Gateway endpoint returned in the previous step. |
| 67 | + |
| 68 | +This should print: |
| 69 | + |
| 70 | +```bash |
| 71 | +Hello |
| 72 | +``` |
| 73 | + |
| 74 | +## Undeploy |
| 75 | + |
| 76 | +When done testing, you can delete the infrastructure with this command. |
| 77 | + |
| 78 | +```bash |
| 79 | +sam delete |
| 80 | +``` |
0 commit comments