|
| 1 | +# Set up REST API Gateway |
| 2 | + |
| 3 | +_WARNING: you are on the master branch, please refer to the docs on the branch that matches your `cortex version`_ |
| 4 | + |
| 5 | +When `api_gateway: public` is set in your API's `networking` configuration (which is the default setting), Cortex will create an "HTTP" API Gateway in AWS for your API (see the [networking docs](../deployments/networking.md) for more information). |
| 6 | + |
| 7 | +However, there may be situations where you need to use AWS's "REST" API Gateway, e.g. to enforce IAM-based auth. Until [#1197](https://github.com/cortexlabs/cortex/issues/1197) is resolved, a REST API Gateway can be used by following these steps. |
| 8 | + |
| 9 | +If your API load balancer is internet-facing (which is the default, or you explicitly set `api_load_balancer_scheme: internet-facing` in your cluster configuration file before creating your cluster), use the [first section](#if-your-api-load-balancer-is-internet-facing) of this guide. |
| 10 | + |
| 11 | +If your API load balancer is internal (i.e. you set `api_load_balancer_scheme: internal` in your cluster configuration file before creating your cluster), use the [second section](#if-your-api-load-balancer-is-internal) of this guide. |
| 12 | + |
| 13 | +## If your API load balancer is internet-facing |
| 14 | + |
| 15 | +### Step 1 |
| 16 | + |
| 17 | +Disable the default API Gateway: |
| 18 | + |
| 19 | +* If you haven't created your cluster yet, you can set `api_gateway: none` in your [cluster configuration file](../cluster-management/config.md) before creating your cluster. |
| 20 | +* If you have already created your cluster, you can set `api_gateway: none` in the `networking` field of your [Realtime API configuration](../deployments/realtime-api/api-configuration.md) and/or [Batch API configuration](../deployments/batch-api/api-configuration.md), and then re-deploy your API. |
| 21 | + |
| 22 | +### Step 2 |
| 23 | + |
| 24 | +Go to the [API Gateway console](https://console.aws.amazon.com/apigateway/home), select "REST API" under "Choose an API type", and click "Build" |
| 25 | + |
| 26 | + |
| 27 | + |
| 28 | +### Step 3 |
| 29 | + |
| 30 | +Select "REST" and "New API", name your API (e.g. "cortex"), select either "Regional" or "Edge optimized" (depending on your preference), and click "Create API" |
| 31 | + |
| 32 | + |
| 33 | + |
| 34 | +### Step 4 |
| 35 | + |
| 36 | +Select "Actions" > "Create Resource" |
| 37 | + |
| 38 | + |
| 39 | + |
| 40 | +### Step 5 |
| 41 | + |
| 42 | +Select "Configure as proxy resource" and "Enable API Gateway CORS", and click "Create Resource" |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | +### Step 6 |
| 47 | + |
| 48 | +Select "HTTP Proxy" and set "Endpoint URL" to "http://<BASE_API_ENDPOINT>/{proxy}". You can get your base API endpoint via `cortex cluster info`; make sure to prepend `http://` and append `/{proxy}`. For example, mine is: `http://a9eaf69fd125947abb1065f62de59047-81cdebc0275f7d96.elb.us-west-2.amazonaws.com/{proxy}`. |
| 49 | + |
| 50 | +Leave "Content Handling" set to "Passthrough" and Click "Save". |
| 51 | + |
| 52 | + |
| 53 | + |
| 54 | +### Step 7 |
| 55 | + |
| 56 | +Select "Actions" > "Deploy API" |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | +### Step 8 |
| 61 | + |
| 62 | +Create a new stage (e.g. "dev") and click "Deploy" |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | +### Step 9 |
| 67 | + |
| 68 | +Copy your "Invoke URL" |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | +### Using your new endpoint |
| 73 | + |
| 74 | +You may now use the "Invoke URL" in place of your APIs endpoint in your client. For example, this curl request: |
| 75 | + |
| 76 | +```bash |
| 77 | +curl http://a9eaf69fd125947abb1065f62de59047-81cdebc0275f7d96.elb.us-west-2.amazonaws.com/iris-classifier -X POST -H "Content-Type: application/json" -d @sample.json |
| 78 | +``` |
| 79 | + |
| 80 | +Would become: |
| 81 | + |
| 82 | +```bash |
| 83 | +curl https://31qjv48rs6.execute-api.us-west-2.amazonaws.com/dev/iris-classifier -X POST -H "Content-Type: application/json" -d @sample.json |
| 84 | +``` |
| 85 | + |
| 86 | +### Cleanup |
| 87 | + |
| 88 | +Delete the API Gateway before spinning down your Cortex cluster: |
| 89 | + |
| 90 | + |
| 91 | + |
| 92 | +## If your API load balancer is internal |
| 93 | + |
| 94 | +### Step 1 |
| 95 | + |
| 96 | +Disable the default API Gateway: |
| 97 | + |
| 98 | +* If you haven't created your cluster yet, you can set `api_gateway: none` in your [cluster configuration file](../cluster-management/config.md) before creating your cluster. |
| 99 | +* If you have already created your cluster, you can set `api_gateway: none` in the `networking` field of your [Realtime API configuration](../deployments/realtime-api/api-configuration.md) and/or [Batch API configuration](../deployments/batch-api/api-configuration.md), and then re-deploy your API. |
| 100 | + |
| 101 | +### Step 2 |
| 102 | + |
| 103 | +Navigate to AWS's EC2 Load Balancer dashboard and locate the Cortex API load balancer. You can determine which is the API load balancer by inspecting the `kubernetes.io/service-name` tag: |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | +Take note of the load balancer's name. |
| 108 | + |
| 109 | +### Step 3 |
| 110 | + |
| 111 | +Go to the [API Gateway console](https://console.aws.amazon.com/apigateway/home), click "VPC Links" on the left sidebar, and click "Create" |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | +### Step 4 |
| 116 | + |
| 117 | +Select "VPC link for REST APIs", name your VPC link (e.g. "cortex"), select the API load balancer (identified in Step 1), and click "Create" |
| 118 | + |
| 119 | + |
| 120 | + |
| 121 | +### Step 5 |
| 122 | + |
| 123 | +Wait for the VPC link to be created (it will take a few minutes) |
| 124 | + |
| 125 | + |
| 126 | + |
| 127 | +### Step 6 |
| 128 | + |
| 129 | +Go to the [API Gateway console](https://console.aws.amazon.com/apigateway/home), select "REST API" under "Choose an API type", and click "Build" |
| 130 | + |
| 131 | + |
| 132 | + |
| 133 | +### Step 7 |
| 134 | + |
| 135 | +Select "REST" and "New API", name your API (e.g. "cortex"), select either "Regional" or "Edge optimized" (depending on your preference), and click "Create API" |
| 136 | + |
| 137 | + |
| 138 | + |
| 139 | +### Step 8 |
| 140 | + |
| 141 | +Select "Actions" > "Create Resource" |
| 142 | + |
| 143 | + |
| 144 | + |
| 145 | +### Step 9 |
| 146 | + |
| 147 | +Select "Configure as proxy resource" and "Enable API Gateway CORS", and click "Create Resource" |
| 148 | + |
| 149 | + |
| 150 | + |
| 151 | +### Step 10 |
| 152 | + |
| 153 | +Select "VPC Link", select "Use Proxy Integration", choose your newly-created VPC Link, and set "Endpoint URL" to "http://<BASE_API_ENDPOINT>/{proxy}". You can get your base API endpoint via `cortex cluster info`; make sure to prepend `http://` and append `/{proxy}`. For example, mine is: `http://a5044e34a352d44b0945adcd455c7fa3-32fa161d3e5bcbf9.elb.us-west-2.amazonaws.com/{proxy}`. Click "Save" |
| 154 | + |
| 155 | + |
| 156 | + |
| 157 | +### Step 11 |
| 158 | + |
| 159 | +Select "Actions" > "Deploy API" |
| 160 | + |
| 161 | + |
| 162 | + |
| 163 | +### Step 12 |
| 164 | + |
| 165 | +Create a new stage (e.g. "dev") and click "Deploy" |
| 166 | + |
| 167 | + |
| 168 | + |
| 169 | +### Step 13 |
| 170 | + |
| 171 | +Copy your "Invoke URL" |
| 172 | + |
| 173 | + |
| 174 | + |
| 175 | +### Using your new endpoint |
| 176 | + |
| 177 | +You may now use the "Invoke URL" in place of your APIs endpoint in your client. For example, this curl request: |
| 178 | + |
| 179 | +```bash |
| 180 | +curl http://a5044e34a352d44b0945adcd455c7fa3-32fa161d3e5bcbf9.elb.us-west-2.amazonaws.com/iris-classifier -X POST -H "Content-Type: application/json" -d @sample.json |
| 181 | +``` |
| 182 | + |
| 183 | +Would become: |
| 184 | + |
| 185 | +```bash |
| 186 | +curl https://lrivodooqh.execute-api.us-west-2.amazonaws.com/dev/iris-classifier -X POST -H "Content-Type: application/json" -d @sample.json |
| 187 | +``` |
| 188 | + |
| 189 | +### Cleanup |
| 190 | + |
| 191 | +Delete the API Gateway and VPC Link before spinning down your Cortex cluster: |
| 192 | + |
| 193 | + |
| 194 | + |
| 195 | + |
0 commit comments