Generate an Amazon Bedrock API key
You can generate an Amazon Bedrock API key using either the AWS Management Console or the AWS API. We recommend that you use the AWS Management Console to easily generate an Amazon Bedrock API key with few steps.
Topics
Generate an Amazon Bedrock API key using the console
To generate an Amazon Bedrock API key using the console, do the following:
-
Sign in to the AWS Management Console with an IAM identity that has permissions to use the Amazon Bedrock console. Then, open the Amazon Bedrock console at https://console.aws.amazon.com/bedrock/
. -
In the left navigation pane, select API keys.
-
Generate one of the following types of keys:
-
Short-term API key – In the Short-term API keys tab, choose Generate short-term API keys. The key expires when your console session expires (and no longer than 12 hours) and lets you make calls to the AWS Region that you generated it from. You can modify the Region directly in the generated key.
-
Long-term API key – In the Long-term API keys tab, choose Generate long-term API keys.
-
In the API key expiration section, choose a time after which the key will expire.
-
(Optional) By default, the AmazonBedrockLimitedAccess AWS-managed policy, which grants access to core Amazon Bedrock API operations, is attached to the IAM user associated with the key. To select more policies to attach to the user, expand the Advanced permissions section and select the policies that you want to add.
-
Choose Generate.
Warning
We strongly recommend restricting the use of long-term keys for exploration of Amazon Bedrock. When you're ready to incorporate Amazon Bedrock into applications with greater security requirements, you should review the following documentation:
-
To learn about preferable alternatives to long-term keys, see Alternatives to long-term access keys in the IAM User Guide.
-
To learn how to monitor long-term keys to prevent security breaches, see Manage access keys for IAM users in the IAM User Guide.
-
-
Generate an Amazon Bedrock API key using the API
We recommend that you use the AWS Management Console to generate Amazon Bedrock API keys for an easy experience. However, you can also generate keys through the API. Expand the section that corresponds to your use case.
The general steps for creating a long-term Amazon Bedrock API key in the API are as follows:
-
Create an IAM user by sending a CreateUser request with an IAM endpoint.
-
Attach the AmazonBedrockLimitedAccess to the IAM user by sending an AttachUserPolicy request with an IAM endpoint. You can repeat this step to attach other managed or custom policies as necessary to the user.
Note
As a best security practice, we strongly recommend that you attach IAM policies to the IAM user to restrict the use of Amazon Bedrock API keys. For examples of time-bounding policies and restricting the IP addresses that can use the key, see Control the use of access keys by attaching an inline policy to an IAM user.
-
Generate the long-term Amazon Bedrock API key by sending a CreateServiceSpecificCredential request with an IAM endpoint and specifying
bedrock.amazonaws.com
as theServiceName
.-
The
ServiceApiKeyValue
returned in the response is your long-term Amazon Bedrock API key. -
The
ServiceSpecificCredentialId
returned in the response can be used to carry out API operations related to the key.
-
To learn how to generate a long-term Amazon Bedrock API key, choose the tab for your preferred method, and then follow the steps:
You can generate a short-term Amazon Bedrock API key that lasts as long as the session used to generate it (and no longer than 12 hours).
Prerequisites
-
Ensure that your setup allows Python to automatically recognize your AWS credentials. To learn more, see Configuring settings for the AWS CLI.
-
Open a terminal and download the Amazon Bedrock token generator with the command that corresponds to the SDK that you're using:
-
Python
python3 -m pip install aws-bedrock-token-generator
-
Javascript
npm install @aws/bedrock-token-generator
-
-
Ensure that the IAM identity that you're using to make API calls minimally has permissions to assume a role and create a role session:
-
The IAM identity must have permissions to assume the role. If the identity has restricted permissions, you can attach the following identity-based policy to the identity (replace
${arn:aws:iam::111122223333:role/SessionRole}
with the actual ARN of the role for the session):{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": "sts:AssumeRole", "Resource": "
${arn:aws:iam::111122223333:role/SessionRole}
" } ] }For more information about granting an identity permissions to assume a role, see Grant a user permissions to switch roles.
-
The IAM role must have a trust policy that allows the IAM identity to assume it. You can attach the following trust policy to an IAM role to allow the principal specified in the
Principal
field to assume the role to create the key. This example specifies an IAM user as the principal. Replace it with the actual ARN of the user.{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "AWS": "
${arn:aws:iam::111122223333:user/UserId}
" }, "Action": "sts:AssumeRole" } ] }For more information about principals, see AWS JSON policy elements: Principal. To learn how to update a trust policy for a role, see Update a role trust policy.
-
Choose the tab that corresponds to the SDK that you're using and run the script to generate a short-term Amazon Bedrock API key from your session credentials:
Note
The permissions of the short-term key will be the intersection of the following:
-
The permissions attached to the session used to generate the key.
-
The permissions granted by the AmazonBedrockLimitedAccess.