|
| 1 | +# API Gateway |
| 2 | + |
| 3 | +This is a simple example of an AWS Lambda function invoked through an Amazon API Gateway. |
| 4 | + |
| 5 | +## Code |
| 6 | + |
| 7 | +The Lambda function takes all HTTP headers it receives as input and returns them as output. |
| 8 | + |
| 9 | +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 the API Gateway receives an HTTP request. |
| 10 | + |
| 11 | +The handler is `(event: APIGatewayV2Request, context: LambdaContext) -> APIGatewayV2Response`. The function takes two arguments: |
| 12 | +- the event argument is a `APIGatewayV2Request`. It is the parameter passed by the API Gateway. It contains all data passed in the HTTP request and some meta data. |
| 13 | +- the context argument is a `Lambda Context`. It is a description of the runtime context. |
| 14 | + |
| 15 | +The function must return a `APIGatewayV2Response`. |
| 16 | + |
| 17 | +`APIGatewayV2Request` and `APIGatewayV2Response` are defined in the [Swift AWS Lambda Events](https://github.com/swift-server/swift-aws-lambda-events) library. |
| 18 | + |
| 19 | +## Build & Package |
| 20 | + |
| 21 | +To build the package, type the following commands. |
| 22 | + |
| 23 | +```bash |
| 24 | +swift build |
| 25 | +swift package archive --allow-network-access docker |
| 26 | +``` |
| 27 | + |
| 28 | +If there is no error, there is a ZIP file ready to deploy. |
| 29 | +The ZIP file is located at `.build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager/MyLambda/MyLambda.zip` |
| 30 | + |
| 31 | +## Deploy |
| 32 | + |
| 33 | +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. |
| 34 | + |
| 35 | +**Prerequisites** : Install the [SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/install-sam-cli.html) |
| 36 | + |
| 37 | +The example directory contains a file named `template.yaml` that describes the deployment. |
| 38 | + |
| 39 | +To actually deploy your Lambda function and create the infrastructure, type the following `sam` command. |
| 40 | + |
| 41 | +```bash |
| 42 | +sam deploy \ |
| 43 | +----resolve-s3 \ |
| 44 | +--template-file template.yaml \ |
| 45 | +--stack-name MyLambda \ |
| 46 | +--capabilities CAPABILITY_IAM |
| 47 | +``` |
| 48 | + |
| 49 | +At the end of the deployment, the script lists the API Gateway endpoint. |
| 50 | +The output is similar to this one. |
| 51 | + |
| 52 | +``` |
| 53 | +----------------------------------------------------------------------------------------------------------------------------- |
| 54 | +Outputs |
| 55 | +----------------------------------------------------------------------------------------------------------------------------- |
| 56 | +Key APIGAtewayEndpoint |
| 57 | +Description API Gateway endpoint UR" |
| 58 | +Value https://a5q74es3k2.execute-api.us-east-1.amazonaws.com |
| 59 | +----------------------------------------------------------------------------------------------------------------------------- |
| 60 | +``` |
| 61 | + |
| 62 | +## Invoke your Lambda function |
| 63 | + |
| 64 | +To invoke the Lambda function, use this `curl` command line. |
| 65 | + |
| 66 | +```bash |
| 67 | +curl https://a5q74es3k2.execute-api.us-east-1.amazonaws.com |
| 68 | +``` |
| 69 | + |
| 70 | +Be sure to replace the URL with the API Gateway endpoint returned in the previous step. |
| 71 | + |
| 72 | +This should print a JSON similar to |
| 73 | + |
| 74 | +```bash |
| 75 | +{"version":"2.0","rawPath":"\/","isBase64Encoded":false,"rawQueryString":"","headers":{"user-agent":"curl\/8.7.1","accept":"*\/*","host":"a5q74es3k2.execute-api.us-east-1.amazonaws.com","content-length":"0","x-amzn-trace-id":"Root=1-66fb0388-691f744d4bd3c99c7436a78d","x-forwarded-port":"443","x-forwarded-for":"81.0.0.43","x-forwarded-proto":"https"},"requestContext":{"requestId":"e719cgNpoAMEcwA=","http":{"sourceIp":"81.0.0.43","path":"\/","protocol":"HTTP\/1.1","userAgent":"curl\/8.7.1","method":"GET"},"stage":"$default","apiId":"a5q74es3k2","time":"30\/Sep\/2024:20:01:12 +0000","timeEpoch":1727726472922,"domainPrefix":"a5q74es3k2","domainName":"a5q74es3k2.execute-api.us-east-1.amazonaws.com","accountId":"012345678901"} |
| 76 | +``` |
| 77 | + |
| 78 | +If you have `jq` installed, you can use it to pretty print the output. |
| 79 | + |
| 80 | +```bash |
| 81 | +curl -s https://a5q74es3k2.execute-api.us-east-1.amazonaws.com | jq |
| 82 | +{ |
| 83 | + "version": "2.0", |
| 84 | + "rawPath": "/", |
| 85 | + "requestContext": { |
| 86 | + "domainPrefix": "a5q74es3k2", |
| 87 | + "stage": "$default", |
| 88 | + "timeEpoch": 1727726558220, |
| 89 | + "http": { |
| 90 | + "protocol": "HTTP/1.1", |
| 91 | + "method": "GET", |
| 92 | + "userAgent": "curl/8.7.1", |
| 93 | + "path": "/", |
| 94 | + "sourceIp": "81.0.0.43" |
| 95 | + }, |
| 96 | + "apiId": "a5q74es3k2", |
| 97 | + "accountId": "012345678901", |
| 98 | + "requestId": "e72KxgsRoAMEMSA=", |
| 99 | + "domainName": "a5q74es3k2.execute-api.us-east-1.amazonaws.com", |
| 100 | + "time": "30/Sep/2024:20:02:38 +0000" |
| 101 | + }, |
| 102 | + "rawQueryString": "", |
| 103 | + "routeKey": "$default", |
| 104 | + "headers": { |
| 105 | + "x-forwarded-for": "81.0.0.43", |
| 106 | + "user-agent": "curl/8.7.1", |
| 107 | + "host": "a5q74es3k2.execute-api.us-east-1.amazonaws.com", |
| 108 | + "accept": "*/*", |
| 109 | + "x-amzn-trace-id": "Root=1-66fb03de-07533930192eaf5f540db0cb", |
| 110 | + "content-length": "0", |
| 111 | + "x-forwarded-proto": "https", |
| 112 | + "x-forwarded-port": "443" |
| 113 | + }, |
| 114 | + "isBase64Encoded": false |
| 115 | +} |
| 116 | +``` |
| 117 | + |
| 118 | +## Undeploy |
| 119 | + |
| 120 | +When done testing, you can delete the infrastructure with this command. |
| 121 | + |
| 122 | +```bash |
| 123 | +sam delete |
| 124 | +``` |
0 commit comments