기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.
제어 및 보안을 강화하기 위해 단기 Amazon Bedrock API 키 새로 고침
Amazon Bedrock에 더 익숙해지면 장기 Amazon Bedrock API 키를 사용하지 않는 것이 좋습니다. 대신 임시 보안 자격 증명을 사용하거나 단기 Amazon Bedrock API 키로 전환해야 합니다.
aws-bedrock-token-generator
패키지의 도움을 받아 스크립트를 생성하여 현재 키가 만료될 때마다 새 단기 키를 프로그래밍 방식으로 재생성할 수 있습니다. 먼저에서 사전 조건을 충족했는지 확인합니다API를 사용하여 단기 Amazon Bedrock API 키 생성.
그런 다음 다음 Python 스크립트를 사용할 수 있습니다.
from datetime import datetime, timedelta import os import boto3 from botocore.credentials import Credentials from aws_bedrock_token_generator import BedrockTokenGenerator # Replace the following values as necessary SESSION_DURATION = timedelta(hours=12) # 12 hours is the maximum EFFECTIVE_TOKEN_DURATION = min(SESSION_DURATION, timedelta(hours=12)) # The token can last no longer than 12 hours ROLE_ARN = "arn:aws:iam::111122223333:role/TargetRole" # Ensure that the identity you're authenticating with has permissions to assume this role ROLE_SESSION_NAME = "your-session-name" REGION = "us-east-1" def get_session_from_assume(): sts = boto3.client("sts") response = sts.assume_role( RoleArn=ROLE_ARN, RoleSessionName=ROLE_SESSION_NAME, DurationSeconds=int(SESSION_DURATION.total_seconds()) ) creds = response["Credentials"] return Credentials( access_key=creds["AccessKeyId"], secret_key=creds["SecretAccessKey"], token=creds["SessionToken"] ) # Step 1: Generate initial token and note timestamp generator = BedrockTokenGenerator() creds = get_session_from_assume() token = generator.get_token(creds, region=REGION) token_created_at = datetime.utcnow() # Step 2: Later in your long lived process — before using the token if datetime.utcnow() - token_created_at >= EFFECTIVE_TOKEN_DURATION: creds = get_session_from_assume() token = generator.get_token(creds, region=REGION) token_created_at = datetime.utcnow() else: # Token is still valid pass # Set the token as an environment variable so you can use it in API calls os.environ['AWS_BEARER_TOKEN_BEDROCK'] = token