Fix: Bedrock Profiles (#1751)

* fix-bedrock-profiles

* fix-bedrock-profiles

* resolve linting issue

* fixes

* resolve using var

* add changeset

* update changeset
This commit is contained in:
Shawn Smith 2025-02-12 17:56:20 -08:00 committed by GitHub
parent 0434b5c772
commit a99648884a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 41 additions and 21 deletions

View file

@ -0,0 +1,5 @@
---
"claude-dev": patch
---
Fix AWS Bedrock Profiles. When configuring the AnthropicBedrock Client you must pass AWS credentials in a specific way, otherwise the client will default to reading credentials from the default AWS profile.

View file

@ -8,35 +8,50 @@ import { fromIni } from "@aws-sdk/credential-providers"
// https://docs.anthropic.com/en/api/claude-on-amazon-bedrock
export class AwsBedrockHandler implements ApiHandler {
private options: ApiHandlerOptions
private client: AnthropicBedrock
private client: AnthropicBedrock | any
private initializationPromise: Promise<void>
constructor(options: ApiHandlerOptions) {
this.options = options
this.initializationPromise = this.initializeClient()
}
const clientConfig: any = {
private async initializeClient() {
let clientConfig: any = {
awsRegion: this.options.awsRegion || "us-east-1",
}
if (this.options.awsUseProfile) {
// Use profile-based credentials if enabled
if (this.options.awsProfile) {
clientConfig.credentials = fromIni({
profile: this.options.awsProfile,
})
} else {
// Use default profile if no specific profile is set
clientConfig.credentials = fromIni()
}
} else if (this.options.awsAccessKey && this.options.awsSecretKey) {
// Use direct credentials if provided
clientConfig.awsAccessKey = this.options.awsAccessKey
clientConfig.awsSecretKey = this.options.awsSecretKey
if (this.options.awsSessionToken) {
clientConfig.awsSessionToken = this.options.awsSessionToken
try {
if (this.options.awsUseProfile) {
// Use profile-based credentials if enabled
// Use named profile, defaulting to 'default' if not specified
var credentials: any
if (this.options.awsProfile) {
credentials = await fromIni({
profile: this.options.awsProfile,
ignoreCache: true,
})()
} else {
credentials = await fromIni({
ignoreCache: true,
})()
}
clientConfig.awsAccessKey = credentials.accessKeyId
clientConfig.awsSecretKey = credentials.secretAccessKey
clientConfig.awsSessionToken = credentials.sessionToken
} else if (this.options.awsAccessKey && this.options.awsSecretKey) {
// Use direct credentials if provided
clientConfig.awsAccessKey = this.options.awsAccessKey
clientConfig.awsSecretKey = this.options.awsSecretKey
if (this.options.awsSessionToken) {
clientConfig.awsSessionToken = this.options.awsSessionToken
}
}
} catch (error) {
console.error("Failed to initialize Bedrock client:", error)
throw error
} finally {
this.client = new AnthropicBedrock(clientConfig)
}
this.client = new AnthropicBedrock(clientConfig)
}
async *createMessage(systemPrompt: string, messages: Anthropic.Messages.MessageParam[]): ApiStream {