|
| 1 | +--- |
| 2 | +title: Deploying your OpenAI Application to AWS Bedrock or GCP Vertex AI |
| 3 | +sidebar_position: 50 |
| 4 | +--- |
| 5 | + |
| 6 | +# Deploying your OpenAI Application to AWS Bedrock or GCP Vertex AI |
| 7 | + |
| 8 | +Let's assume you have an app that uses an OpenAI client library and you want to deploy it to the cloud, either on **AWS Bedrock** or **GCP Vertex AI**. |
| 9 | + |
| 10 | +This tutorial shows you how **Defang** makes it easy. |
| 11 | + |
| 12 | +Suppose you start with a compose file like this: |
| 13 | + |
| 14 | +```yaml |
| 15 | +services: |
| 16 | + app: |
| 17 | + build: |
| 18 | + context: . |
| 19 | + ports: |
| 20 | + - 3000:3000 |
| 21 | + environment: |
| 22 | + OPENAI_API_KEY: |
| 23 | + healthcheck: |
| 24 | + test: ["CMD", "curl", "-f", "http://localhost:3000/"] |
| 25 | +``` |
| 26 | +
|
| 27 | +--- |
| 28 | +
|
| 29 | +## Add an LLM Service to Your Compose File |
| 30 | +
|
| 31 | +You need to add a new service that acts as a proxy between your app and the backend LLM provider (Bedrock or Vertex). |
| 32 | +
|
| 33 | +Add **Defang's openai-access-gateway** service: |
| 34 | +
|
| 35 | +```diff |
| 36 | ++ llm: |
| 37 | ++ image: defangio/openai-access-gateway |
| 38 | ++ x-defang-llm: true |
| 39 | ++ ports: |
| 40 | ++ - target: 80 |
| 41 | ++ published: 80 |
| 42 | ++ mode: host |
| 43 | ++ environment: |
| 44 | ++ - OPENAI_API_KEY |
| 45 | ++ - GCP_PROJECT_ID |
| 46 | ++ - GCP_REGION |
| 47 | +``` |
| 48 | + |
| 49 | +### Notes: |
| 50 | + |
| 51 | +- The container image is based on [aws-samples/bedrock-access-gateway](https://github.com/aws-samples/bedrock-access-gateway), with enhancements. |
| 52 | +- `x-defang-llm: true` signals to **Defang** that this service should be configured to use target platform AI services. |
| 53 | +- New environment variables: |
| 54 | + - `GCP_PROJECT_ID` and `GCP_REGION` are needed if using **Vertex AI**. (e.g.` GCP_PROJECT_ID` = my-project-456789 and `GCP_REGION` = us-central1) |
| 55 | + |
| 56 | +:::tip |
| 57 | +**OpenAI Key** |
| 58 | + |
| 59 | +You no longer need your original OpenAI API Key. |
| 60 | +We recommend generating a random secret for authentication with the gateway: |
| 61 | + |
| 62 | +```bash |
| 63 | +defang config set OPENAI_API_KEY --random |
| 64 | +``` |
| 65 | +::: |
| 66 | + |
| 67 | +--- |
| 68 | + |
| 69 | +## Redirect Application Traffic |
| 70 | + |
| 71 | +Modify your `app` service to send API calls to the `openai-access-gateway`: |
| 72 | + |
| 73 | +```diff |
| 74 | + services: |
| 75 | + app: |
| 76 | + ports: |
| 77 | + - 3000:3000 |
| 78 | + environment: |
| 79 | + OPENAI_API_KEY: |
| 80 | ++ OPENAI_BASE_URL: "http://llm/api/v1" |
| 81 | + healthcheck: |
| 82 | + test: ["CMD", "curl", "-f", "http://localhost:3000/"] |
| 83 | +``` |
| 84 | + |
| 85 | +Now, all OpenAI traffic will route through your gateway service. |
| 86 | + |
| 87 | +--- |
| 88 | + |
| 89 | +## Selecting a Model |
| 90 | + |
| 91 | +You should configure your application to specify the model you want to use. |
| 92 | + |
| 93 | +```diff |
| 94 | + services: |
| 95 | + app: |
| 96 | + ports: |
| 97 | + - 3000:3000 |
| 98 | + environment: |
| 99 | + OPENAI_API_KEY: |
| 100 | + OPENAI_BASE_URL: "http://llm/api/v1" |
| 101 | ++ MODEL: "anthropic.claude-3-sonnet-20240229-v1:0" # for Bedrock |
| 102 | ++ # MODEL: "google/gemini-2.5-pro-preview-03-25" # for Vertex AI |
| 103 | + healthcheck: |
| 104 | + test: ["CMD", "curl", "-f", "http://localhost:3000/"] |
| 105 | +``` |
| 106 | + |
| 107 | +Choose the correct `MODEL` depending on which cloud provider you are using. |
| 108 | + |
| 109 | +:::info |
| 110 | +**Choosing the Right Model** |
| 111 | + |
| 112 | +- For **AWS Bedrock**, use a Bedrock model ID (e.g., `anthropic.claude-3-sonnet-20240229-v1:0`). |
| 113 | +- For **GCP Vertex AI**, use a full model path (e.g., `google/gemini-2.5-pro-preview-03-25`). |
| 114 | +[See available Vertex models here.](https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/call-vertex-using-openai-library#client-setup) |
| 115 | + |
| 116 | +# Complete Example Compose File |
| 117 | + |
| 118 | +```yaml |
| 119 | +services: |
| 120 | + app: |
| 121 | + build: |
| 122 | + context: . |
| 123 | + ports: |
| 124 | + - 3000:3000 |
| 125 | + environment: |
| 126 | + OPENAI_API_KEY: |
| 127 | + OPENAI_BASE_URL: "http://llm/api/v1" |
| 128 | + MODEL: "anthropic.claude-3-sonnet-20240229-v1:0" # or your Vertex AI model path |
| 129 | + healthcheck: |
| 130 | + test: ["CMD", "curl", "-f", "http://localhost:3000/"] |
| 131 | + |
| 132 | + llm: |
| 133 | + image: defangio/openai-access-gateway |
| 134 | + x-defang-llm: true |
| 135 | + ports: |
| 136 | + - target: 80 |
| 137 | + published: 80 |
| 138 | + mode: host |
| 139 | + environment: |
| 140 | + - OPENAI_API_KEY |
| 141 | + - GCP_PROJECT_ID # required if using Vertex AI |
| 142 | + - GCP_REGION # required if using Vertex AI |
| 143 | +``` |
| 144 | +
|
| 145 | +--- |
| 146 | +
|
| 147 | +# Environment Variable Matrix |
| 148 | +
|
| 149 | +| Variable | AWS Bedrock | GCP Vertex AI | |
| 150 | +|--------------------|-------------|---------------| |
| 151 | +| `GCP_PROJECT_ID` | _(not used)_| Required | |
| 152 | +| `GCP_REGION` | _(not used)_| Required | |
| 153 | +| `MODEL` | Bedrock model ID | Vertex model path | |
| 154 | + |
| 155 | +--- |
| 156 | + |
| 157 | +You now have a single app that can: |
| 158 | + |
| 159 | +- Talk to **AWS Bedrock** or **GCP Vertex AI** |
| 160 | +- Use the same OpenAI-compatible client code |
| 161 | +- Easily switch cloud providers by changing a few environment variables |
| 162 | + |
0 commit comments