|
| 1 | +# API Gateway |
| 2 | + |
| 3 | +This is a simple example of an AWS Lambda function invoked through an Amazon API Gateway V1. |
| 4 | + |
| 5 | +> [!NOTE] |
| 6 | +> This example uses the API Gateway V1 `Api` endpoint type, whereas the [API Gateway V2](https://github.com/swift-server/swift-aws-lambda-runtime/tree/main/Examples/APIGateway) example uses the `HttpApi` endpoint type. For more information, see [Choose between REST AIs and HTTP APIs](https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-vs-rest.html). |
| 7 | +
|
| 8 | +## Code |
| 9 | + |
| 10 | +The Lambda function takes all HTTP headers it receives as input and returns them as output. |
| 11 | + |
| 12 | +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. |
| 13 | + |
| 14 | +The handler is `(event: APIGatewayRequest, context: LambdaContext) -> APIGatewayResponse`. The function takes two arguments: |
| 15 | +- the event argument is a `APIGatewayRequest`. It is the parameter passed by the API Gateway. It contains all data passed in the HTTP request and some meta data. |
| 16 | +- the context argument is a `Lambda Context`. It is a description of the runtime context. |
| 17 | + |
| 18 | +The function must return a `APIGatewayResponse`. |
| 19 | + |
| 20 | +`APIGatewayRequest` and `APIGatewayResponse` are defined in the [Swift AWS Lambda Events](https://github.com/swift-server/swift-aws-lambda-events) library. |
| 21 | + |
| 22 | +## Build & Package |
| 23 | + |
| 24 | +To build the package, type the following commands. |
| 25 | + |
| 26 | +```bash |
| 27 | +swift build |
| 28 | +swift package archive --allow-network-connections docker |
| 29 | +``` |
| 30 | + |
| 31 | +If there is no error, there is a ZIP file ready to deploy. |
| 32 | +The ZIP file is located at `.build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager/APIGatewayLambda/APIGatewayLambda.zip` |
| 33 | + |
| 34 | +## Deploy |
| 35 | + |
| 36 | +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. |
| 37 | + |
| 38 | +**Prerequisites** : Install the [SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/install-sam-cli.html) |
| 39 | + |
| 40 | +The example directory contains a file named `template.yaml` that describes the deployment. |
| 41 | + |
| 42 | +To actually deploy your Lambda function and create the infrastructure, type the following `sam` command. |
| 43 | + |
| 44 | +```bash |
| 45 | +sam deploy \ |
| 46 | +--resolve-s3 \ |
| 47 | +--template-file template.yaml \ |
| 48 | +--stack-name APIGatewayLambda \ |
| 49 | +--capabilities CAPABILITY_IAM |
| 50 | +``` |
| 51 | + |
| 52 | +At the end of the deployment, the script lists the API Gateway endpoint. |
| 53 | +The output is similar to this one. |
| 54 | + |
| 55 | +``` |
| 56 | +----------------------------------------------------------------------------------------------------------------------------- |
| 57 | +Outputs |
| 58 | +----------------------------------------------------------------------------------------------------------------------------- |
| 59 | +Key APIGatewayEndpoint |
| 60 | +Description API Gateway endpoint URL" |
| 61 | +Value https://a5q74es3k2.execute-api.us-east-1.amazonaws.com |
| 62 | +----------------------------------------------------------------------------------------------------------------------------- |
| 63 | +``` |
| 64 | + |
| 65 | +## Invoke your Lambda function |
| 66 | + |
| 67 | +To invoke the Lambda function, use this `curl` command line. |
| 68 | + |
| 69 | +```bash |
| 70 | +curl https://a5q74es3k2.execute-api.us-east-1.amazonaws.com |
| 71 | +``` |
| 72 | + |
| 73 | +Be sure to replace the URL with the API Gateway endpoint returned in the previous step. |
| 74 | + |
| 75 | +This should print a JSON similar to |
| 76 | + |
| 77 | +```bash |
| 78 | +{"httpMethod":"GET","queryStringParameters":{},"isBase64Encoded":false,"resource":"\/","path":"\/","headers":{"X-Forwarded-Port":"3000","X-Forwarded-Proto":"http","User-Agent":"curl\/8.7.1","Host":"localhost:3000","Accept":"*\/*"},"requestContext":{"resourcePath":"\/","identity":{"sourceIp":"127.0.0.1","userAgent":"Custom User Agent String"},"httpMethod":"GET","resourceId":"123456","accountId":"123456789012","apiId":"1234567890","requestId":"a9d2db08-8364-4da4-8237-8912bf8148c8","domainName":"localhost:3000","stage":"Prod","path":"\/"},"multiValueQueryStringParameters":{},"pathParameters":{},"multiValueHeaders":{"Accept":["*\/*"],"Host":["localhost:3000"],"X-Forwarded-Port":["3000"],"User-Agent":["curl\/8.7.1"],"X-Forwarded-Proto":["http"]},"stageVariables":{}} |
| 79 | +``` |
| 80 | + |
| 81 | +If you have `jq` installed, you can use it to pretty print the output. |
| 82 | + |
| 83 | +```bash |
| 84 | +curl -s https://a5q74es3k2.execute-api.us-east-1.amazonaws.com | jq |
| 85 | +{ |
| 86 | + "stageVariables": {}, |
| 87 | + "queryStringParameters": {}, |
| 88 | + "multiValueHeaders": { |
| 89 | + "Accept": [ |
| 90 | + "*/*" |
| 91 | + ], |
| 92 | + "User-Agent": [ |
| 93 | + "curl/8.7.1" |
| 94 | + ], |
| 95 | + "X-Forwarded-Proto": [ |
| 96 | + "http" |
| 97 | + ], |
| 98 | + "Host": [ |
| 99 | + "localhost:3000" |
| 100 | + ], |
| 101 | + "X-Forwarded-Port": [ |
| 102 | + "3000" |
| 103 | + ] |
| 104 | + }, |
| 105 | + "pathParameters": {}, |
| 106 | + "isBase64Encoded": false, |
| 107 | + "path": "/", |
| 108 | + "requestContext": { |
| 109 | + "apiId": "1234567890", |
| 110 | + "stage": "Prod", |
| 111 | + "httpMethod": "GET", |
| 112 | + "domainName": "localhost:3000", |
| 113 | + "requestId": "a9d2db08-8364-4da4-8237-8912bf8148c8", |
| 114 | + "identity": { |
| 115 | + "userAgent": "Custom User Agent String", |
| 116 | + "sourceIp": "127.0.0.1" |
| 117 | + }, |
| 118 | + "resourceId": "123456", |
| 119 | + "path": "/", |
| 120 | + "resourcePath": "/", |
| 121 | + "accountId": "123456789012" |
| 122 | + }, |
| 123 | + "multiValueQueryStringParameters": {}, |
| 124 | + "resource": "/", |
| 125 | + "headers": { |
| 126 | + "Accept": "*/*", |
| 127 | + "X-Forwarded-Proto": "http", |
| 128 | + "X-Forwarded-Port": "3000", |
| 129 | + "Host": "localhost:3000", |
| 130 | + "User-Agent": "curl/8.7.1" |
| 131 | + }, |
| 132 | + "httpMethod": "GET" |
| 133 | +} |
| 134 | +``` |
| 135 | + |
| 136 | +## Undeploy |
| 137 | + |
| 138 | +When done testing, you can delete the infrastructure with this command. |
| 139 | + |
| 140 | +```bash |
| 141 | +sam delete |
| 142 | +``` |
0 commit comments