|  | 
|  | 1 | +# API Gateway and Cloud Development Kit | 
|  | 2 | + | 
|  | 3 | +This is a simple example of an AWS Lambda function invoked through an Amazon API Gateway and deployed with the Cloud Development Kit (CDK). | 
|  | 4 | + | 
|  | 5 | +## Code  | 
|  | 6 | + | 
|  | 7 | +The Lambda function takes all HTTP headers it receives as input and returns them as output. See the [API Gateway example](Examples/APIGateway/README.md) for a complete description of the code. | 
|  | 8 | + | 
|  | 9 | +## Build & Package  | 
|  | 10 | + | 
|  | 11 | +To build the package, type the following commands. | 
|  | 12 | + | 
|  | 13 | +```bash | 
|  | 14 | +swift build | 
|  | 15 | +swift package archive --allow-network-connections docker | 
|  | 16 | +``` | 
|  | 17 | + | 
|  | 18 | +If there is no error, there is a ZIP file ready to deploy.  | 
|  | 19 | +The ZIP file is located at `.build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager/APIGatewayLambda/APIGatewayLambda.zip` | 
|  | 20 | + | 
|  | 21 | +## Deploy | 
|  | 22 | + | 
|  | 23 | +>[NOTE] | 
|  | 24 | +>Before deploying the infrastructure, you need to have NodeJS and the AWS CDK installed and configured. | 
|  | 25 | +>For more information, see the [AWS CDK documentation](https://docs.aws.amazon.com/cdk/v2/guide/getting_started.html). | 
|  | 26 | +
 | 
|  | 27 | +To deploy the infrastructure, type the following commands. | 
|  | 28 | + | 
|  | 29 | +```sh | 
|  | 30 | +# Change to the infra directory | 
|  | 31 | +cd infra | 
|  | 32 | + | 
|  | 33 | +# Install the dependencies (only before the first deployment) | 
|  | 34 | +npm install  | 
|  | 35 | + | 
|  | 36 | +cdk deploy | 
|  | 37 | + | 
|  | 38 | +✨  Synthesis time: 2.88s | 
|  | 39 | +... redacted for brevity ... | 
|  | 40 | +Do you wish to deploy these changes (y/n)? y | 
|  | 41 | +... redacted for brevity ... | 
|  | 42 | + ✅  LambdaApiStack | 
|  | 43 | + | 
|  | 44 | +✨  Deployment time: 42.96s | 
|  | 45 | + | 
|  | 46 | +Outputs: | 
|  | 47 | +LambdaApiStack.ApiUrl = https://tyqnjcawh0.execute-api.eu-central-1.amazonaws.com/ | 
|  | 48 | +Stack ARN: | 
|  | 49 | +arn:aws:cloudformation:eu-central-1:401955065246:stack/LambdaApiStack/e0054390-be05-11ef-9504-065628de4b89 | 
|  | 50 | + | 
|  | 51 | +✨  Total time: 45.84s | 
|  | 52 | +``` | 
|  | 53 | + | 
|  | 54 | +## Invoke your Lambda function | 
|  | 55 | + | 
|  | 56 | +To invoke the Lambda function, use this `curl` command line. | 
|  | 57 | + | 
|  | 58 | +```bash | 
|  | 59 | +curl https://tyqnjcawh0.execute-api.eu-central-1.amazonaws.com | 
|  | 60 | +``` | 
|  | 61 | + | 
|  | 62 | +Be sure to replace the URL with the API Gateway endpoint returned in the previous step. | 
|  | 63 | + | 
|  | 64 | +This should print a JSON similar to  | 
|  | 65 | + | 
|  | 66 | +```bash  | 
|  | 67 | +{"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"} | 
|  | 68 | +``` | 
|  | 69 | + | 
|  | 70 | +If you have `jq` installed, you can use it to pretty print the output. | 
|  | 71 | + | 
|  | 72 | +```bash | 
|  | 73 | +curl -s  https://tyqnjcawh0.execute-api.eu-central-1.amazonaws.com | jq    | 
|  | 74 | +{ | 
|  | 75 | +  "version": "2.0", | 
|  | 76 | +  "rawPath": "/", | 
|  | 77 | +  "requestContext": { | 
|  | 78 | +    "domainPrefix": "a5q74es3k2", | 
|  | 79 | +    "stage": "$default", | 
|  | 80 | +    "timeEpoch": 1727726558220, | 
|  | 81 | +    "http": { | 
|  | 82 | +      "protocol": "HTTP/1.1", | 
|  | 83 | +      "method": "GET", | 
|  | 84 | +      "userAgent": "curl/8.7.1", | 
|  | 85 | +      "path": "/", | 
|  | 86 | +      "sourceIp": "81.0.0.43" | 
|  | 87 | +    }, | 
|  | 88 | +    "apiId": "a5q74es3k2", | 
|  | 89 | +    "accountId": "012345678901", | 
|  | 90 | +    "requestId": "e72KxgsRoAMEMSA=", | 
|  | 91 | +    "domainName": "a5q74es3k2.execute-api.us-east-1.amazonaws.com", | 
|  | 92 | +    "time": "30/Sep/2024:20:02:38 +0000" | 
|  | 93 | +  }, | 
|  | 94 | +  "rawQueryString": "", | 
|  | 95 | +  "routeKey": "$default", | 
|  | 96 | +  "headers": { | 
|  | 97 | +    "x-forwarded-for": "81.0.0.43", | 
|  | 98 | +    "user-agent": "curl/8.7.1", | 
|  | 99 | +    "host": "a5q74es3k2.execute-api.us-east-1.amazonaws.com", | 
|  | 100 | +    "accept": "*/*", | 
|  | 101 | +    "x-amzn-trace-id": "Root=1-66fb03de-07533930192eaf5f540db0cb", | 
|  | 102 | +    "content-length": "0", | 
|  | 103 | +    "x-forwarded-proto": "https", | 
|  | 104 | +    "x-forwarded-port": "443" | 
|  | 105 | +  }, | 
|  | 106 | +  "isBase64Encoded": false | 
|  | 107 | +} | 
|  | 108 | +``` | 
|  | 109 | + | 
|  | 110 | +## Undeploy | 
|  | 111 | + | 
|  | 112 | +When done testing, you can delete the infrastructure with this command. | 
|  | 113 | + | 
|  | 114 | +```bash | 
|  | 115 | +cdk destroy | 
|  | 116 | + | 
|  | 117 | +Are you sure you want to delete: LambdaApiStack (y/n)? y | 
|  | 118 | +LambdaApiStack: destroying... [1/1] | 
|  | 119 | +... redacted for brevity ... | 
|  | 120 | + ✅  LambdaApiStack: destroyed | 
|  | 121 | +``` | 
0 commit comments