|
| 1 | +# QuoteAPI |
| 2 | + |
| 3 | +This application illustrates how to deploy a Server-Side Swift workload on AWS using the [AWS Serverless Application Model (SAM)](https://aws.amazon.com/serverless/sam/) toolkit. The workload is a simple REST API that returns a string from an Amazon API Gateway. Requests to the API Gateway endpoint are handled by an AWS Lambda Function written in Swift. |
| 4 | + |
| 5 | + |
| 6 | +## Prerequisites |
| 7 | + |
| 8 | +To build this sample application, you need: |
| 9 | + |
| 10 | +- [AWS Account](https://console.aws.amazon.com/) |
| 11 | +- [AWS Command Line Interface (AWS CLI)](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html) - install the CLI and [configure](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-quickstart.html) it with credentials to your AWS account |
| 12 | +- [AWS SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/install-sam-cli.html) - a command-line tool used to create serverless workloads on AWS |
| 13 | +- [Docker Desktop](https://www.docker.com/products/docker-desktop/) - to compile your Swift code for Linux deployment to AWS Lambda |
| 14 | + |
| 15 | +## Build the application |
| 16 | + |
| 17 | +The **sam build** command uses Docker to compile your Swift Lambda function and package it for deployment to AWS. |
| 18 | + |
| 19 | +```bash |
| 20 | +sam build |
| 21 | +``` |
| 22 | + |
| 23 | +## Deploy the application |
| 24 | + |
| 25 | +The **sam deploy** command creates the Lambda function and API Gateway in your AWS account. |
| 26 | + |
| 27 | +```bash |
| 28 | +sam deploy --guided |
| 29 | +``` |
| 30 | + |
| 31 | +Accept the default response to every prompt, except the following warning: |
| 32 | + |
| 33 | +```bash |
| 34 | +QuoteService may not have authorization defined, Is this okay? [y/N]: y |
| 35 | +``` |
| 36 | + |
| 37 | +The project creates a publicly accessible API endpoint. This is a warning to inform you the API does not have authorization. If you are interested in adding authorization to the API, please refer to the [SAM Documentation](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-httpapi.html). |
| 38 | + |
| 39 | +## Use the API |
| 40 | + |
| 41 | +At the end of the deployment, SAM displays the endpoint of your API Gateway: |
| 42 | + |
| 43 | +```bash |
| 44 | +Outputs |
| 45 | +---------------------------------------------------------------------------------------- |
| 46 | +Key SwiftAPIEndpoint |
| 47 | +Description API Gateway endpoint URL for your application |
| 48 | +Value https://[your-api-id].execute-api.[your-aws-region].amazonaws.com |
| 49 | +---------------------------------------------------------------------------------------- |
| 50 | +``` |
| 51 | + |
| 52 | +Use cURL or a tool such as [Postman](https://www.postman.com/) to interact with your API. Replace **[your-api-endpoint]** with the SwiftAPIEndpoint value from the deployment output. |
| 53 | + |
| 54 | +**Invoke the API Endpoint** |
| 55 | + |
| 56 | +```bash |
| 57 | +curl https://[your-api-endpoint]/stocks/AMZN |
| 58 | +``` |
| 59 | + |
| 60 | +## Test the API Locally |
| 61 | +SAM also allows you to execute your Lambda functions locally on your development computer. Follow these instructions to execute the Lambda function locally. Further capabilities can be explored in the [SAM Documentation](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-using-invoke.html). |
| 62 | + |
| 63 | +**Event Files** |
| 64 | + |
| 65 | +When a Lambda function is invoked, API Gateway sends an event to the function with all the data packaged with the API call. When running the functions locally, you pass in a json file to the function that simulates the event data. The **events** folder contains a json file for the function. |
| 66 | + |
| 67 | +**Invoke the Lambda Function Locally** |
| 68 | + |
| 69 | +```bash |
| 70 | +sam local invoke QuoteService --event events/GetQuote.json |
| 71 | +``` |
| 72 | + |
| 73 | +## Cleanup |
| 74 | + |
| 75 | +When finished with your application, use SAM to delete it from your AWS account. Answer **Yes (y)** to all prompts. This will delete all of the application resources created in your AWS account. |
| 76 | + |
| 77 | +```bash |
| 78 | +sam delete |
| 79 | +``` |
0 commit comments