|
| 1 | +sam deploy \ |
| 2 | +--resolve-s3 \ |
| 3 | +--template-file template.yaml \ |
| 4 | +--stack-name AWSSDKExample \ |
| 5 | +--capabilities CAPABILITY_IAM |
| 6 | + |
| 7 | +# API Gateway |
| 8 | + |
| 9 | +This is a simple example of an AWS Lambda function that uses the [AWS SDK for Swift](https://github.com/awslabs/aws-sdk-swift) to read data from Amazon S3. |
| 10 | + |
| 11 | +## Code |
| 12 | + |
| 13 | +The Lambda function reads all bucket names from your AWS account and return them as a String. |
| 14 | + |
| 15 | +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. |
| 16 | + |
| 17 | +The handler is `(event: APIGatewayV2Request, context: LambdaContext) -> APIGatewayV2Response`. The function takes two arguments: |
| 18 | +- 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. |
| 19 | +- the context argument is a `Lambda Context`. It is a description of the runtime context. |
| 20 | + |
| 21 | +The function must return a `APIGatewayV2Response`. |
| 22 | + |
| 23 | +`APIGatewayV2Request` and `APIGatewayV2Response` are defined in the [Swift AWS Lambda Events](https://github.com/swift-server/swift-aws-lambda-events) library. |
| 24 | + |
| 25 | +The handler creates an S3 client and `ListBucketsInput` object. It passes the input object to the client and receives an output response. |
| 26 | +It then extract the list of bucket names from the output to create a `\n` separated list of names, as a `String` |
| 27 | + |
| 28 | +## Build & Package |
| 29 | + |
| 30 | +To build the package, type the following commands. |
| 31 | + |
| 32 | +```bash |
| 33 | +swift build |
| 34 | +swift package archive --allow-network-access docker |
| 35 | +``` |
| 36 | + |
| 37 | +If there is no error, there is a ZIP file ready to deploy. |
| 38 | +The ZIP file is located at `.build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager/AWSSDKExample/AWSSDKExample.zip` |
| 39 | + |
| 40 | +## Deploy |
| 41 | + |
| 42 | +The deployment must include the Lambda function and an 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. |
| 43 | + |
| 44 | +**Prerequisites** : Install the [SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/install-sam-cli.html) |
| 45 | + |
| 46 | +The example directory contains a file named `template.yaml` that describes the deployment. |
| 47 | + |
| 48 | +To actually deploy your Lambda function and create the infrastructure, type the following `sam` command. |
| 49 | + |
| 50 | +```bash |
| 51 | +sam deploy \ |
| 52 | +--resolve-s3 \ |
| 53 | +--template-file template.yaml \ |
| 54 | +--stack-name AWSSDKExample \ |
| 55 | +--capabilities CAPABILITY_IAM |
| 56 | +``` |
| 57 | + |
| 58 | +At the end of the deployment, the script lists the API Gateway endpoint. |
| 59 | +The output is similar to this one. |
| 60 | + |
| 61 | +``` |
| 62 | +----------------------------------------------------------------------------------------------------------------------------- |
| 63 | +Outputs |
| 64 | +----------------------------------------------------------------------------------------------------------------------------- |
| 65 | +Key APIGAtewayEndpoint |
| 66 | +Description API Gateway endpoint URL" |
| 67 | +Value https://a5q74es3k2.execute-api.us-east-1.amazonaws.com |
| 68 | +----------------------------------------------------------------------------------------------------------------------------- |
| 69 | +``` |
| 70 | + |
| 71 | +## Invoke your Lambda function |
| 72 | + |
| 73 | +To invoke the Lambda function, use this `curl` command line. |
| 74 | + |
| 75 | +```bash |
| 76 | +curl https://a5q74es3k2.execute-api.us-east-1.amazonaws.com |
| 77 | +``` |
| 78 | + |
| 79 | +Be sure to replace the URL with the API Gateway endpoint returned in the previous step. |
| 80 | + |
| 81 | +This should print text similar to |
| 82 | + |
| 83 | +```bash |
| 84 | +my_bucket_1 |
| 85 | +my_bucket_2 |
| 86 | +``` |
| 87 | + |
| 88 | +## Undeploy |
| 89 | + |
| 90 | +When done testing, you can delete the infrastructure with this command. |
| 91 | + |
| 92 | +```bash |
| 93 | +sam delete |
| 94 | +``` |
0 commit comments