|
| 1 | +# QuoteAPI ALB Example |
| 2 | + |
| 3 | +This application illustrates how to deploy a Server-Side Swift workload on AWS using an Application Load Balancer (ALB) with Lambda targets. The workload is a simple REST API that returns stock quotes. Requests to the ALB are forwarded to an AWS Lambda Function written in Swift using the OpenAPI Lambda library. |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | + |
| 7 | +To build this sample application, you need: |
| 8 | + |
| 9 | +- [AWS Account](https://console.aws.amazon.com/) |
| 10 | +- [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 |
| 11 | +- [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 |
| 12 | +- [Docker Desktop](https://www.docker.com/products/docker-desktop/) - to compile your Swift code for Linux deployment to AWS Lambda |
| 13 | + |
| 14 | +## Build the application |
| 15 | + |
| 16 | +The **sam build** command uses Docker to compile your Swift Lambda function and package it for deployment to AWS. |
| 17 | + |
| 18 | +```bash |
| 19 | +sam build |
| 20 | +``` |
| 21 | + |
| 22 | +On macOS, you might need to run this command if `sam` doesn't see `docker`: |
| 23 | +```bash |
| 24 | +export DOCKER_HOST=unix://$HOME/.docker/run/docker.sock |
| 25 | +``` |
| 26 | + |
| 27 | +## Deploy the application |
| 28 | + |
| 29 | +The **sam deploy** command creates the Lambda function, Application Load Balancer, and associated VPC resources in your AWS account. |
| 30 | + |
| 31 | +```bash |
| 32 | +sam deploy --guided |
| 33 | +``` |
| 34 | + |
| 35 | +## Use the API |
| 36 | + |
| 37 | +At the end of the deployment, SAM displays the endpoint of your Application Load Balancer: |
| 38 | + |
| 39 | +```bash |
| 40 | +Outputs |
| 41 | +---------------------------------------------------------------------------------------- |
| 42 | +Key QuoteAPILoadBalancerURL |
| 43 | +Description Application Load Balancer URL for QuoteAPI |
| 44 | +Value http://QuoteAPILoadBalancer-123456789.us-east-1.elb.amazonaws.com/stocks/AAPL |
| 45 | +---------------------------------------------------------------------------------------- |
| 46 | +``` |
| 47 | + |
| 48 | +Use cURL or a tool such as [Postman](https://www.postman.com/) to interact with your API. Replace **[your-alb-endpoint]** with the QuoteAPILoadBalancerURL value from the deployment output. |
| 49 | + |
| 50 | +**Invoke the API Endpoint** |
| 51 | + |
| 52 | +```bash |
| 53 | +curl http://[your-alb-endpoint]/stocks/AMZN |
| 54 | +``` |
| 55 | + |
| 56 | +## Test the API Locally |
| 57 | + |
| 58 | +SAM also allows you to execute your Lambda functions locally on your development computer. |
| 59 | + |
| 60 | +**Invoke the Lambda Function Locally** |
| 61 | + |
| 62 | +```bash |
| 63 | +sam local invoke QuoteServiceALB --event events/GetQuote.json |
| 64 | +``` |
| 65 | + |
| 66 | +On macOS, you might need to run this command if `sam` doesn't see `docker`: |
| 67 | +```bash |
| 68 | +export DOCKER_HOST=unix://$HOME/.docker/run/docker.sock |
| 69 | +``` |
| 70 | + |
| 71 | +## Architecture |
| 72 | + |
| 73 | +This example demonstrates: |
| 74 | + |
| 75 | +- **Application Load Balancer**: Routes HTTP requests to Lambda functions |
| 76 | +- **Lambda Target Group**: Configures the ALB to forward requests to Lambda |
| 77 | +- **VPC Setup**: Creates a VPC with public subnets for the ALB |
| 78 | +- **Security Groups**: Controls inbound traffic to the ALB |
| 79 | +- **OpenAPI Integration**: Uses Swift OpenAPI Lambda library with ALB events |
| 80 | + |
| 81 | +## Cleanup |
| 82 | + |
| 83 | +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. |
| 84 | + |
| 85 | +```bash |
| 86 | +sam delete |
| 87 | +``` |
| 88 | + |
| 89 | +> **⚠️ Security and Reliability Notice** |
| 90 | +> |
| 91 | +> This is an example application for demonstration purposes. When deploying such infrastructure in production environments, we strongly encourage you to follow these best practices for improved security and resiliency: |
| 92 | +> |
| 93 | +> - Enable access logging on Application Load Balancer ([documentation](https://docs.aws.amazon.com/elasticloadbalancing/latest/application/load-balancer-access-logs.html)) |
| 94 | +> - Ensure that AWS Lambda function is configured for function-level concurrent execution limit ([concurrency documentation](https://docs.aws.amazon.com/lambda/latest/dg/lambda-concurrency.html), [configuration guide](https://docs.aws.amazon.com/lambda/latest/dg/configuration-concurrency.html)) |
| 95 | +> - Check encryption settings for Lambda environment variables ([documentation](https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars-encryption.html)) |
| 96 | +> - Ensure that AWS Lambda function is configured for a Dead Letter Queue (DLQ) ([documentation](https://docs.aws.amazon.com/lambda/latest/dg/invocation-async-retain-records.html#invocation-dlq)) |
| 97 | +> - Configure HTTPS/TLS termination on the Application Load Balancer ([documentation](https://docs.aws.amazon.com/elasticloadbalancing/latest/application/create-https-listener.html)) |
| 98 | +> |
| 99 | +> **Note:** The `openapi.yaml` file in this example is not suited for production. In real-world scenarios, you must: |
| 100 | +> 1. Ensure that the global security field has rules defined |
| 101 | +> 2. Ensure that security operations is not empty ([OpenAPI Security Specification](https://learn.openapis.org/specification/security.html)) |
| 102 | +> 3. Follow proper authentication, authorization, input validation, and error handling practices |
| 103 | +> |
| 104 | +> As per Checkov CKV_OPENAPI_4 and CKV_OPENAPI_5 security checks. |
0 commit comments