|
| 1 | +# API Gateway Function Queue Async |
| 2 | + |
| 3 | +This project demonstrates an asynchronous messaging pattern using Oracle Cloud Infrastructure (OCI) services: |
| 4 | + |
| 5 | +- **API Gateway**: Receives HTTP requests and routes them to functions |
| 6 | +- **OCI Functions**: Serverless functions for processing requests |
| 7 | +- **OCI Queue**: Asynchronous message queue for decoupling services |
| 8 | + |
| 9 | +## Architecture |
| 10 | + |
| 11 | +The application follows an asynchronous messaging pattern: |
| 12 | + |
| 13 | +1. HTTP request arrives at API Gateway → `/order` endpoint |
| 14 | +2. API Gateway routes to **place-order OCI Function** |
| 15 | +3. Function validates and enqueues order to **OCI Queue** |
| 16 | +4. Function returns immediately (async response) |
| 17 | +5. **process-order Container Instance** continuously polls the queue |
| 18 | +6. Container Instance processes messages and inserts into **NoSQL Database** |
| 19 | + |
| 20 | +``` |
| 21 | +HTTP Request → API Gateway → place-order Function → Queue → process-order Container Instance → NoSQL Table |
| 22 | + ↓ |
| 23 | + (Async Processing) |
| 24 | +``` |
| 25 | + |
| 26 | +## Components |
| 27 | + |
| 28 | +### API Gateway |
| 29 | +- Receives HTTP requests at `/order` endpoint |
| 30 | +- Routes requests to the `place-order` function |
| 31 | + |
| 32 | +### Functions |
| 33 | +- **place-order**: OCI Function that receives order requests via API Gateway and enqueues them to the queue |
| 34 | +- **process-order**: Container Instance (not an OCI Function) that continuously polls the OCI Queue and processes orders by inserting them into the NoSQL table |
| 35 | + |
| 36 | +### Queue |
| 37 | +- **OrderQueue**: Asynchronous message queue for order processing |
| 38 | +- Provides reliable message delivery and processing |
| 39 | + |
| 40 | +## Directory Structure |
| 41 | + |
| 42 | +``` |
| 43 | +api-gateway-function-queue-async/ |
| 44 | +├── README.md # Project documentation |
| 45 | +├── functions/ # Application components |
| 46 | +│ ├── place-order/ # OCI Function for receiving orders |
| 47 | +│ └── process-order/ # Container Instance for queue polling |
| 48 | +└── terraform/ # Infrastructure as Code |
| 49 | + └── modules/ # Reusable Terraform modules |
| 50 | + ├── apigateway/ # API Gateway module |
| 51 | + ├── container_repository/ # OCI Container Registry module |
| 52 | + ├── functions/ # OCI Functions module |
| 53 | + ├── queue/ # OCI Queue module |
| 54 | + └── vcn/ # Virtual Cloud Network module |
| 55 | +``` |
| 56 | + |
| 57 | +## Prerequisites |
| 58 | + |
| 59 | +Before you begin, ensure you have the following: |
| 60 | + |
| 61 | +- **OCI Account**: Active Oracle Cloud Infrastructure account with appropriate compartment access |
| 62 | +- **Terraform**: Version 1.10.0 or higher installed on your local machine |
| 63 | +- **Docker**: Docker Desktop or equivalent for building and pushing container images |
| 64 | +- **OCI CLI**: Configured with your OCI credentials for authentication |
| 65 | +- **Network**: A public subnet in your VPC for the API Gateway and Container Instance |
| 66 | +- **IAM Permissions**: Your OCI user must have permissions to: |
| 67 | + - Create API Gateways |
| 68 | + - Create and manage OCI Functions |
| 69 | + - Create and manage OCI Queues |
| 70 | + - Create NoSQL tables |
| 71 | + - Create Container Instances |
| 72 | + - Create Container Registries |
| 73 | + - Manage IAM policies and dynamic groups |
| 74 | +- **Authentication**: OCI auth token for Docker Container Registry access |
| 75 | + |
| 76 | +## Getting Started |
| 77 | + |
| 78 | +1. **Configure OCI Variables**: |
| 79 | + |
| 80 | + Update `terraform/terraform.tfvars` with your OCI configuration: |
| 81 | + ```hcl |
| 82 | + region = "us-ashburn-1" # Your OCI region |
| 83 | + compartment_ocid = "ocid1.compartment.oc1..." # Your compartment OCID |
| 84 | + subnet_ocid = "ocid1.subnet.oc1..." # Your public subnet OCID |
| 85 | + tenancy_ocid = "ocid1.tenancy.oc1..." # Your tenancy OCID |
| 86 | + queue_name = "OrderQueue" |
| 87 | + post_order_container_repository_name = "queue_async_repo" |
| 88 | + process_order_container_repository_name = "queue_async_process_repo" |
| 89 | + application_display_name = "queue_async_app" |
| 90 | + nosql_table_name = "orders" |
| 91 | + ``` |
| 92 | + |
| 93 | +2. **Deploy Infrastructure**: |
| 94 | + ```bash |
| 95 | + cd terraform |
| 96 | + terraform init |
| 97 | + terraform apply -var-file=terraform.tfvars |
| 98 | + ``` |
| 99 | + |
| 100 | + **Note**: After successful deployment, note the OCIR repository addresses from the Terraform outputs: |
| 101 | + - `post_order_repository_path`: Container registry path for place-order function |
| 102 | + - `process_order_repository_path`: Container registry path for process-order function |
| 103 | + |
| 104 | + These will be used in the next step for building and pushing Docker images. |
| 105 | + |
| 106 | +3. **Build and Deploy Place-Order Image**: |
| 107 | + ```bash |
| 108 | + cd functions/place-order |
| 109 | + docker build -t place-order:1 . |
| 110 | + docker tag place-order:1 <post_order_repository_path>:1 |
| 111 | + docker push <post_order_repository_path>:1 |
| 112 | + ``` |
| 113 | + |
| 114 | +4. **Build and Deploy Process-Order Image**: |
| 115 | + ```bash |
| 116 | + cd ../process-order |
| 117 | + docker build -t process-order:1 . |
| 118 | + docker tag process-order:1 <process_order_repository_path>:1 |
| 119 | + docker push <process_order_repository_path>:1 |
| 120 | + ``` |
| 121 | + |
| 122 | +5. **Update Terraform Variables with Image Tags**: |
| 123 | + |
| 124 | + Update `terraform/terraform.tfvars` to add the function image URIs you just pushed: |
| 125 | + ```hcl |
| 126 | + # Add these to your existing terraform.tfvars |
| 127 | + functions = { |
| 128 | + "place-order" = { |
| 129 | + source_image = "<post_order_repository_path>:1" |
| 130 | + path = "place-order" |
| 131 | + config = {} |
| 132 | + } |
| 133 | + } |
| 134 | + queue_poller_image = "<process_order_repository_path>:1" |
| 135 | + ``` |
| 136 | + |
| 137 | +6. **Re-apply Terraform Configuration**: |
| 138 | + |
| 139 | + Apply the updated configuration with the new image tags: |
| 140 | + ```bash |
| 141 | + cd terraform |
| 142 | + terraform apply -var-file=terraform.tfvars |
| 143 | + ``` |
| 144 | + |
| 145 | + This will deploy the OCI Function and Container Instance with the container images you just built and pushed. |
| 146 | + |
| 147 | +7. **Get API Gateway Endpoint**: |
| 148 | + |
| 149 | + After the Terraform apply completes, retrieve the API Gateway endpoint URL: |
| 150 | + ```bash |
| 151 | + terraform output api_gateway_endpoint |
| 152 | + ``` |
| 153 | + |
| 154 | + Save this URL for testing the API in the next step. |
| 155 | + |
| 156 | +8. **Test the API**: |
| 157 | + ```bash |
| 158 | + curl -X POST https://<api_gateway_endpoint>/order \ |
| 159 | + -H "Content-Type: application/json" \ |
| 160 | + -d '{ |
| 161 | + "data": { |
| 162 | + "order_id": "ORD-001", |
| 163 | + "customer_id": "CUST123", |
| 164 | + "amount": 99.99 |
| 165 | + } |
| 166 | + }' |
| 167 | + ``` |
| 168 | + |
| 169 | +9. **Verify Records in NoSQL Table**: |
| 170 | + |
| 171 | + After submitting a few orders, verify that the records have been processed and inserted into the NoSQL table: |
| 172 | + ```bash |
| 173 | + # Query the NoSQL table for all orders |
| 174 | + oci nosql query execute --statement "SELECT * FROM order_info" --compartment-id <compartment_ocid> --region <region> |
| 175 | + ``` |
| 176 | + |
| 177 | + You should see the orders you submitted through the API in the results. |
| 178 | + |
| 179 | +## Benefits |
| 180 | + |
| 181 | +- **Scalability**: Queue decouples request handling from processing |
| 182 | +- **Reliability**: Messages are persisted and can be retried |
| 183 | +- **Performance**: API responds immediately while processing happens asynchronously |
| 184 | +- **Monitoring**: Queue provides visibility into message processing |
0 commit comments