new·Earn with mozg — 20% of every monthSend somebody here and take a fifth of every plan payment they make, for as long as they keep paying — not a bounty on the first invoice. Your handle is the link, the window is thirty days, and the commission lands on your balance the second they pay. Free to join: if you have signed in, you already have the link. mozg.sh/earnall news →
mozg.beta
Sign in

AI SDK · Providers · all subjects

bedrock/setup

12 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

Amazon Bedrock provider installation

The Amazon Bedrock provider is available in the `@ai-sdk/amazon-bedrock` module and can be installed via npm or yarn.

Amazon Bedrock model access prerequisites

Access to Amazon Bedrock foundation models is not granted by default. An IAM user with sufficient permissions must request access to a model through the console. Once access is provided to a model, it is available for all users in the account.

Amazon Bedrock authentication methods

The Amazon Bedrock provider supports two authentication methods with automatic fallback: API key authentication (recommended) and AWS SigV4 authentication using IAM credentials or the AWS SDK credentials chain. API key authentication takes precedence when supplied.

Amazon Bedrock API key authentication setup

To use API key authentication, generate a Bedrock API key from the AWS console, then either set it as the `AWS_BEARER_TOKEN_BEDROCK` environment variable or pass it directly to `createAmazonBedrock` with the `apiKey` property. When `apiKey` is omitted, the provider falls back to the `AWS_BEARER_TOKEN_BEDROCK` environment variable, then to SigV4 authentication.

Amazon Bedrock IAM credentials setup

Set AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, and AWS_REGION environment variables. The IAM user account needs the `AmazonBedrockFullAccess` policy attached. When both `accessKeyId` and `secretAccessKey` are provided as strings, `sessionToken` is only used if explicitly passed; `AWS_SESSION_TOKEN` from the environment is not used to avoid mixing static keys with workload tokens.

How to import and instantiate the Bedrock provider

Import the default provider instance `amazonBedrock` from `@ai-sdk/amazon-bedrock`, or import `createAmazonBedrock` to create a customized instance. Example: `import { amazonBedrock } from '@ai-sdk/amazon-bedrock';` or `import { createAmazonBedrock } from '@ai-sdk/amazon-bedrock'; const amazonBedrock = createAmazonBedrock({ region: 'us-east-1', accessKeyId: 'xxxxxxxxx', secretAccessKey: 'xxxxxxxxx', sessionToken: 'xxxxxxxxx' });`

Amazon Bedrock AWS SDK credentials chain setup

To use AWS SDK credentials chain (instance profiles, instance roles, ECS roles, EKS Service Accounts), install `@aws-sdk/credential-providers` and pass a `credentialProvider` property to `createAmazonBedrock`. Example: `import { fromNodeProviderChain } from '@aws-sdk/credential-providers'; const amazonBedrock = createAmazonBedrock({ region: 'us-east-1', credentialProvider: fromNodeProviderChain() });`

Amazon Bedrock provider instance options

Provider instance options include: region (string, defaults to AWS_REGION), accessKeyId (string, defaults to AWS_ACCESS_KEY_ID), secretAccessKey (string, defaults to AWS_SECRET_ACCESS_KEY), sessionToken (string, optional, for temporary credentials), credentialProvider (function returning Promise with accessKeyId, secretAccessKey, and optional sessionToken), apiKey (string, optional, defaults to AWS_BEARER_TOKEN_BEDROCK), baseURL (string, optional, for custom endpoints or proxy), headers (Record<string, string>, optional, custom headers), and fetch (function, optional, custom fetch implementation).

How to import and instantiate Bedrock Anthropic provider

Import the default provider instance bedrockAnthropic from '@ai-sdk/amazon-bedrock/anthropic'. Or import createBedrockAnthropic from '@ai-sdk/amazon-bedrock/anthropic' and call createBedrockAnthropic({ region, accessKeyId, secretAccessKey, sessionToken }) to create a customized instance.

Bedrock Anthropic provider settings

The createBedrockAnthropic function accepts the following optional settings: region (string, defaults to AWS_REGION environment variable), accessKeyId (string, defaults to AWS_ACCESS_KEY_ID environment variable), secretAccessKey (string, defaults to AWS_SECRET_ACCESS_KEY environment variable), sessionToken (string, optional for temporary credentials, when both access keys are set as strings pass the token here if needed; if either key comes from environment, omitting sessionToken allows AWS_SESSION_TOKEN), apiKey (string, for Bearer token authentication instead of AWS SigV4, defaults to AWS_BEARER_TOKEN_BEDROCK environment variable), baseURL (string, useful for custom endpoints or proxy configurations), headers (Resolvable<Record<string, string | undefined>>), fetch (custom fetch implementation for middleware or testing), credentialProvider (() => PromiseLike<BedrockCredentials>, AWS credential provider for dynamic credentials).

Bedrock Anthropic language models

You can create Bedrock Anthropic models using bedrockAnthropic(modelId) where modelId is the model identifier like 'us.anthropic.claude-3-5-sonnet-20241022-v2:0'.

Example: Bedrock Anthropic basic usage

import { bedrockAnthropic } from '@ai-sdk/amazon-bedrock/anthropic'; import { generateText } from 'ai'; const { text } = await generateText({ model: bedrockAnthropic('us.anthropic.claude-3-5-sonnet-20241022-v2:0'), prompt: 'Write a vegetarian lasagna recipe for 4 people.', });

Give your agent this brain