Skip to content

Commit 5b1ee85

Browse files
author
gitName
committed
draft complete
1 parent 136330e commit 5b1ee85

File tree

2 files changed

+22
-33
lines changed

2 files changed

+22
-33
lines changed

articles/api-management/amazon-bedrock-passthrough-llm-api.md

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ ms.service: azure-api-management
55
author: dlepow
66
ms.author: danlep
77
ms.topic: how-to
8-
ms.date: 07/06/2025
8+
ms.date: 07/07/2025
99
ms.update-cycle: 180-days
1010
ms.collection: ce-skilling-ai-copilot
1111
ms.custom: template-how-to, build-2024
@@ -32,17 +32,6 @@ Learn more about Amazon Bedrock:
3232
- An existing API Management instance. [Create one if you haven't already](get-started-create-service-instance.md).
3333
- An Amazon Web Services (AWS) account with access to Amazon Bedrock, and access to one or more Amazon Bedrock foundation models. [Learn more](https://docs.aws.amazon.com/bedrock/latest/userguide/getting-started-console.html)
3434

35-
36-
<!--
37-
Outline from Andrei:
38-
1) Passtrough API (not auth configuration, even URL is not used, but should be setup in the UI anyway)
39-
2) Named values for aws access key and secret key
40-
41-
3) policy for signing on API level that uses secret and access keys
42-
43-
4) do a couple of modification to the code (.NET SDK sample Ethan also shared in the same threaed)
44-
-->
45-
4635
## Create IAM user access keys
4736

4837
To authenticate your API Management instance to Amazon API Gateway, you need access keys for an AWS IAM user.
@@ -64,9 +53,9 @@ Securely store the two IAM user access keys as secret [named values](api-managem
6453
| Access key | *accesskey* | Access key ID retrieved from AWS |
6554
| Secret access key | *secretkey* | Secret access key retrieved from AWS |
6655

67-
## Import a passthrough language model API using the portal
56+
## Import a Bedrock API using the portal
6857

69-
To import an Amazon Bedrock language model API to API Management:
58+
To import an Amazon Bedrock API to API Management:
7059

7160
1. In the [Azure portal](https://portal.azure.com), navigate to your API Management instance.
7261
1. In the left menu, under **APIs**, select **APIs** > **+ Add API**.
@@ -84,7 +73,7 @@ To import an Amazon Bedrock language model API to API Management:
8473
1. In **Type**, select **Create a passthrough API**.
8574
1. Leave values in **Access key** blank.
8675

87-
:::image type="content" source="media/openai-compatible-llm-api/configure-api.png" alt-text="Screenshot of language model API configuration in the portal.":::
76+
:::image type="content" source="media/amazon-bedrock-passthrough-llm-api/configure-api.png" alt-text="Screenshot of language model API configuration in the portal.":::
8877

8978
1. On the remaining tabs, optionally configure policies to manage token consumption, semantic caching, and AI content safety. For details, see [Import an OpenAI-compatible language model API](openai-compatible-llm-api.md).
9079
1. Select **Review**.
@@ -94,9 +83,9 @@ API Management creates the API and (optionally) policies to help you monitor and
9483

9584
## Configure policies to authenticate requests to the Amazon Bedrock API
9685

97-
Configure API Management policies to sign requests to the Amazon Bedrock API. [Learn more about signing AWS API requests](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_sigv.html).
86+
Configure API Management policies to sign requests to the Amazon Bedrock API. [Learn more about signing AWS API requests](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_sigv.html)
9887

99-
The following example uses the *accesskey* and *secretkey* named values you created earlier for the AWS access key and secret key. Set the `region` variable to the appropriate values for your Amazon Bedrock API. The example uses `us-east-1` for the region.
88+
The following example uses the *accesskey* and *secretkey* named values you created previously for the AWS access key and secret key. Set the `region` variable to the appropriate value for your Amazon Bedrock API. The example uses `us-east-1` for the region.
10089

10190
1. In the [Azure portal](https://portal.azure.com), navigate to your API Management instance.
10291
1. In the left menu, under **APIs**, select **APIs**.
@@ -135,7 +124,7 @@ The following example uses the *accesskey* and *secretkey* named values you crea
135124
var uri = context.Request.Url;
136125
var host = uri.Host;
137126

138-
// create canonical path
127+
// Create canonical path
139128
var path = uri.Path;
140129
var modelSplit = path.Split(new[] { "model/" }, 2, StringSplitOptions.None);
141130
var afterModel = modelSplit.Length > 1 ? modelSplit[1] : "";
@@ -147,7 +136,7 @@ The following example uses the *accesskey* and *secretkey* named values you crea
147136
var amzDate = ((DateTime)context.Variables["now"]).ToString("yyyyMMddTHHmmssZ");
148137
var dateStamp = ((DateTime)context.Variables["now"]).ToString("yyyyMMdd");
149138

150-
// hash the payload
139+
// Hash the payload
151140
var body = context.Request.Body.As<string>(preserveContent: true);
152141
string hashedPayload;
153142
using (var sha256 = System.Security.Cryptography.SHA256.Create())
@@ -156,7 +145,7 @@ The following example uses the *accesskey* and *secretkey* named values you crea
156145
hashedPayload = BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
157146
}
158147

159-
// create canonical query string
148+
// Create canonical query string
160149
var queryDict = context.Request.Url.Query;
161150
var canonicalQueryString = "";
162151
if (queryDict != null && queryDict.Count > 0)
@@ -171,7 +160,7 @@ The following example uses the *accesskey* and *secretkey* named values you crea
171160
canonicalQueryString = string.Join("&", encodedParams.OrderBy(p => p));
172161
}
173162

174-
// create signed headers and canonical headers
163+
// Create signed headers and canonical headers
175164
var headers = context.Request.Headers;
176165
var canonicalHeaderList = new List<string[]>();
177166

@@ -207,7 +196,7 @@ The following example uses the *accesskey* and *secretkey* named values you crea
207196
var canonicalHeaders = string.Join("\n", canonicalHeadersOrdered.Select(h => $"{h[0]}:{h[1].Trim()}")) + "\n";
208197
var signedHeaders = string.Join(";", canonicalHeadersOrdered.Select(h => h[0]));
209198

210-
// create and hash the canonical request
199+
// Create and hash the canonical request
211200
var canonicalRequest = $"{method}\n{canonicalPath}\n{canonicalQueryString}\n{canonicalHeaders}\n{signedHeaders}\n{hashedPayload}";
212201
string hashedCanonicalRequest = "";
213202
using (var sha256 = System.Security.Cryptography.SHA256.Create())
@@ -216,11 +205,11 @@ The following example uses the *accesskey* and *secretkey* named values you crea
216205
hashedCanonicalRequest = BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
217206
}
218207

219-
// build string to sign
208+
// Build string to sign
220209
var credentialScope = $"{dateStamp}/{region}/{service}/aws4_request";
221210
var stringToSign = $"AWS4-HMAC-SHA256\n{amzDate}\n{credentialScope}\n{hashedCanonicalRequest}";
222211

223-
// sign it using secret key
212+
// Sign it using secret key
224213
byte[] kSecret = System.Text.Encoding.UTF8.GetBytes("AWS4" + secretKey);
225214
byte[] kDate, kRegion, kService, kSigning;
226215
using (var h1 = new System.Security.Cryptography.HMACSHA256(kSecret))
@@ -240,8 +229,8 @@ The following example uses the *accesskey* and *secretkey* named values you crea
240229
kSigning = h4.ComputeHash(System.Text.Encoding.UTF8.GetBytes("aws4_request"));
241230
}
242231

243-
// auth header
244-
string signature;
232+
// Auth header
233+
string signature;
245234
using (var hmac = new System.Security.Cryptography.HMACSHA256(kSigning))
246235
{
247236
var sigBytes = hmac.ComputeHash(System.Text.Encoding.UTF8.GetBytes(stringToSign));
@@ -268,11 +257,11 @@ The following example uses the *accesskey* and *secretkey* named values you crea
268257
```
269258

270259

271-
## Call the LLM API
260+
## Call the Bedrock API
272261

273-
To call the LLM API through API Management, you can use the AWS Bedrock SDK. This example uses the .NET SDK, but you can use any language that supports the AWS Bedrock API.
262+
To call the Bedrock API through API Management, you can use the AWS Bedrock SDK. This example uses the .NET SDK, but you can use any language that supports the AWS Bedrock API.
274263

275-
The following example uses a custom HTTP client that instantiates classes defined in the accompanying file `BedrockHttpClientFactory.cs`. The custom HTTP client routes requests to the API Management endpoint and includes the API Management subscription key in the request headers.
264+
The following example uses a custom HTTP client that instantiates classes defined in the accompanying file `BedrockHttpClientFactory.cs`. The custom HTTP client routes requests to the API Management endpoint and includes the API Management subscription key (if necessary) in the request headers.
276265

277266
```csharp
278267
using Amazon;
@@ -281,9 +270,9 @@ using Amazon.BedrockRuntime.Model;
281270
using Amazon.Runtime;
282271
using BedrockClient;
283272

284-
// Replace with your AWS access key and secret key.
285-
var accessKey = "<your-access-key>";
286-
var secretKey = "<your-secret-key>";
273+
// Leave accessKey and secretKey values as empty strings.
274+
var accessKey = "";
275+
var secretKey = "";
287276
var credentials = new BasicAWSCredentials(accessKey, secretKey);
288277

289278
// Create custom configuration to route requests through API Management
@@ -345,7 +334,7 @@ catch (AmazonBedrockRuntimeException e)
345334

346335
### BedrockHttpClientFactory.cs
347336

348-
The following code implements classes to create a custom HTTP client that routes requests to the Bedrock API through API Management, including the necessary subscription key in the headers.
337+
The following code implements classes to create a custom HTTP client that routes requests to the Bedrock API through API Management, including an API Management subscription key in the headers.
349338

350339
```csharp
351340
using Amazon.Runtime;
82.4 KB
Loading

0 commit comments

Comments
 (0)